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