View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.common.io;
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.io.RandomAccessFile;
18  import java.io.Reader;
19  
20  /**
21   * File reader that can uses random access file.
22   *
23   * @author Daniel Sendula
24   */
25  public class AdvancedFileReader extends Reader {
26  
27      /** Size */
28      protected long size;
29  
30      /** Mark */
31      protected long mark;
32  
33      /** Random access file */
34      protected RandomAccessFile file;
35  
36      /**
37       * Constructor
38       * @param file file
39       * @param from starting offset
40       * @param size size
41       * @throws IOException
42       */
43      public AdvancedFileReader(File file, long from, long size) throws IOException {
44          this.file = new RandomAccessFile(file, "r");
45          this.file.seek(from);
46          this.size = size+from;
47          if (size > this.file.length()-from) {
48              size = this.file.length()-from;
49          }
50          if (size < 0) {
51              size = 0;
52          }
53          mark = from;
54      }
55  
56      @Override
57      public int read(char[] cbuf, int off, int len) throws IOException {
58          if (len == 0) {
59              return 0;
60          }
61  
62          long ptr = file.getFilePointer();
63          if (ptr >= size) {
64              return -1;
65          }
66  
67          if (len > size-ptr) {
68              len = (int)(size-ptr);
69          }
70  
71          if (len == 0) {
72              return -1;
73          }
74  
75          byte[] buf = new byte[len];
76          len = file.read(buf);
77          if (len == 0) {
78              return 0;
79          }
80          for (int i=0; i<len; i++) { // TODO: this is slow...
81              cbuf[off] = (char)buf[i];
82              off = off + 1;
83          }
84          return len;
85      }
86  
87      @Override
88      public void close() throws IOException {
89          file.close();
90      }
91  
92      @Override
93      public boolean markSupported() {
94          return true;
95      }
96  
97      /**
98       * Marks position in a file
99       * @throws IOException
100      */
101     public void mark() throws IOException {
102         mark = file.getFilePointer();
103     }
104 
105     @Override
106     public void reset() throws IOException {
107         file.seek(mark);
108     }
109 
110     @Override
111     public boolean ready() {
112         return true;
113     }
114 
115     @Override
116     public long skip(long n) throws IOException {
117         long ptr = file.getFilePointer();
118         if (n > size - ptr) {
119             n = size - ptr;
120         }
121         ptr = ptr + (int)n;
122         return n;
123     }
124 }