1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.local;
18
19 import java.io.EOFException;
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.RandomAccessFile;
25
26 import org.apache.commons.vfs2.FileSystemException;
27 import org.apache.commons.vfs2.provider.AbstractRandomAccessContent;
28 import org.apache.commons.vfs2.util.RandomAccessMode;
29
30
31
32
33 final class LocalFileRandomAccessContent extends AbstractRandomAccessContent {
34
35 private final static int BYTE_VALUE_MASK = 0xFF;
36
37 private final RandomAccessFile raf;
38
39 private final InputStream rafis;
40
41 LocalFileRandomAccessContent(final File localFile, final RandomAccessMode mode) throws FileSystemException {
42 super(mode);
43
44 try {
45 raf = new RandomAccessFile(localFile, mode.getModeString());
46 rafis = new InputStream() {
47 @Override
48 public int available() throws IOException {
49 final long available = raf.length() - raf.getFilePointer();
50 if (available > Integer.MAX_VALUE) {
51 return Integer.MAX_VALUE;
52 }
53
54 return (int) available;
55 }
56
57 @Override
58 public void close() throws IOException {
59 raf.close();
60 }
61
62 @Override
63 public int read() throws IOException {
64 try {
65 return raf.readByte() & BYTE_VALUE_MASK;
66 } catch (final EOFException e) {
67 return -1;
68 }
69 }
70
71 @Override
72 public int read(final byte[] b) throws IOException {
73 return raf.read(b);
74 }
75
76 @Override
77 public int read(final byte[] b, final int off, final int len) throws IOException {
78 return raf.read(b, off, len);
79 }
80
81 @Override
82 public long skip(final long n) throws IOException {
83 raf.seek(raf.getFilePointer() + n);
84 return n;
85 }
86 };
87 } catch (final FileNotFoundException e) {
88 throw new FileSystemException("vfs.provider/random-access-open-failed.error", localFile);
89 }
90 }
91
92 @Override
93 public void close() throws IOException {
94 raf.close();
95 }
96
97 @Override
98 public long getFilePointer() throws IOException {
99 return raf.getFilePointer();
100 }
101
102 @Override
103 public InputStream getInputStream() throws IOException {
104 return rafis;
105 }
106
107 @Override
108 public long length() throws IOException {
109 return raf.length();
110 }
111
112 @Override
113 public boolean readBoolean() throws IOException {
114 return raf.readBoolean();
115 }
116
117 @Override
118 public byte readByte() throws IOException {
119 return raf.readByte();
120 }
121
122 @Override
123 public char readChar() throws IOException {
124 return raf.readChar();
125 }
126
127 @Override
128 public double readDouble() throws IOException {
129 return raf.readDouble();
130 }
131
132 @Override
133 public float readFloat() throws IOException {
134 return raf.readFloat();
135 }
136
137 @Override
138 public void readFully(final byte[] b) throws IOException {
139 raf.readFully(b);
140 }
141
142 @Override
143 public void readFully(final byte[] b, final int off, final int len) throws IOException {
144 raf.readFully(b, off, len);
145 }
146
147 @Override
148 public int readInt() throws IOException {
149 return raf.readInt();
150 }
151
152 @Override
153 public long readLong() throws IOException {
154 return raf.readLong();
155 }
156
157 @Override
158 public short readShort() throws IOException {
159 return raf.readShort();
160 }
161
162 @Override
163 public int readUnsignedByte() throws IOException {
164 return raf.readUnsignedByte();
165 }
166
167 @Override
168 public int readUnsignedShort() throws IOException {
169 return raf.readUnsignedShort();
170 }
171
172 @Override
173 public String readUTF() throws IOException {
174 return raf.readUTF();
175 }
176
177 @Override
178 public void seek(final long pos) throws IOException {
179 raf.seek(pos);
180 }
181
182 @Override
183 public void setLength(final long newLength) throws IOException {
184 raf.setLength(newLength);
185 }
186
187 @Override
188 public int skipBytes(final int n) throws IOException {
189 return raf.skipBytes(n);
190 }
191
192 @Override
193 public void write(final byte[] b) throws IOException {
194 raf.write(b);
195 }
196
197 @Override
198 public void write(final byte[] b, final int off, final int len) throws IOException {
199 raf.write(b, off, len);
200 }
201
202 @Override
203 public void write(final int b) throws IOException {
204 raf.write(b);
205 }
206
207 @Override
208 public void writeBoolean(final boolean v) throws IOException {
209 raf.writeBoolean(v);
210 }
211
212 @Override
213 public void writeByte(final int v) throws IOException {
214 raf.writeByte(v);
215 }
216
217 @Override
218 public void writeBytes(final String s) throws IOException {
219 raf.writeBytes(s);
220 }
221
222 @Override
223 public void writeChar(final int v) throws IOException {
224 raf.writeChar(v);
225 }
226
227 @Override
228 public void writeChars(final String s) throws IOException {
229 raf.writeChars(s);
230 }
231
232 @Override
233 public void writeDouble(final double v) throws IOException {
234 raf.writeDouble(v);
235 }
236
237 @Override
238 public void writeFloat(final float v) throws IOException {
239 raf.writeFloat(v);
240 }
241
242 @Override
243 public void writeInt(final int v) throws IOException {
244 raf.writeInt(v);
245 }
246
247 @Override
248 public void writeLong(final long v) throws IOException {
249 raf.writeLong(v);
250 }
251
252 @Override
253 public void writeShort(final int v) throws IOException {
254 raf.writeShort(v);
255 }
256
257 @Override
258 public void writeUTF(final String str) throws IOException {
259 raf.writeUTF(str);
260 }
261 }