KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > local > LocalFileRandomAccessContent


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider.local;
17
18 import org.apache.commons.vfs.FileSystemException;
19 import org.apache.commons.vfs.provider.AbstractRandomAccessContent;
20 import org.apache.commons.vfs.util.RandomAccessMode;
21
22 import java.io.EOFException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.RandomAccessFile JavaDoc;
28
29 /**
30  * RandomAccess for local files
31  *
32  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
33  */

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