KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > ftp > FtpRandomAccessContent


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.ftp;
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.DataInputStream JavaDoc;
23 import java.io.FilterInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 class FtpRandomAccessContent extends AbstractRandomAccessContent
28 {
29     private final FtpFileObject fileObject;
30
31     protected long filePointer = 0;
32     private DataInputStream JavaDoc dis = null;
33     private FtpFileObject.FtpInputStream mis = null;
34
35     FtpRandomAccessContent(final FtpFileObject fileObject, RandomAccessMode mode)
36     {
37         super(mode);
38
39         this.fileObject = fileObject;
40         // fileSystem = (FtpFileSystem) this.fileObject.getFileSystem();
41
}
42
43     public long getFilePointer() throws IOException JavaDoc
44     {
45         return filePointer;
46     }
47
48     public void seek(long pos) throws IOException JavaDoc
49     {
50         if (pos == filePointer)
51         {
52             // no change
53
return;
54         }
55
56         if (pos < 0)
57         {
58             throw new FileSystemException("vfs.provider/random-access-invalid-position.error",
59                 new Object JavaDoc[]
60                 {
61                     new Long JavaDoc(pos)
62                 });
63         }
64         if (dis != null)
65         {
66             close();
67         }
68
69         filePointer = pos;
70     }
71
72     private void createStream() throws IOException JavaDoc
73     {
74         if (dis != null)
75         {
76             return;
77         }
78
79         // FtpClient client = fileSystem.getClient();
80
mis = fileObject.getInputStream(filePointer);
81         dis = new DataInputStream JavaDoc(new FilterInputStream JavaDoc(mis)
82         {
83             public int read() throws IOException JavaDoc
84             {
85                 int ret = super.read();
86                 if (ret > -1)
87                 {
88                     filePointer++;
89                 }
90                 return ret;
91             }
92
93             public int read(byte b[]) throws IOException JavaDoc
94             {
95                 int ret = super.read(b);
96                 if (ret > -1)
97                 {
98                     filePointer+=ret;
99                 }
100                 return ret;
101             }
102
103             public int read(byte b[], int off, int len) throws IOException JavaDoc
104             {
105                 int ret = super.read(b, off, len);
106                 if (ret > -1)
107                 {
108                     filePointer+=ret;
109                 }
110                 return ret;
111             }
112
113             public void close() throws IOException JavaDoc
114             {
115                 FtpRandomAccessContent.this.close();
116             }
117         });
118     }
119
120
121     public void close() throws IOException JavaDoc
122     {
123         if (dis != null)
124         {
125             mis.abort();
126
127             // this is to avoid recursive close
128
DataInputStream JavaDoc oldDis = dis;
129             dis = null;
130             oldDis.close();
131             mis = null;
132         }
133     }
134
135     public long length() throws IOException JavaDoc
136     {
137         return fileObject.getContent().getSize();
138     }
139
140     public byte readByte() throws IOException JavaDoc
141     {
142         createStream();
143         byte data = dis.readByte();
144         return data;
145     }
146
147     public char readChar() throws IOException JavaDoc
148     {
149         createStream();
150         char data = dis.readChar();
151         return data;
152     }
153
154     public double readDouble() throws IOException JavaDoc
155     {
156         createStream();
157         double data = dis.readDouble();
158         return data;
159     }
160
161     public float readFloat() throws IOException JavaDoc
162     {
163         createStream();
164         float data = dis.readFloat();
165         return data;
166     }
167
168     public int readInt() throws IOException JavaDoc
169     {
170         createStream();
171         int data = dis.readInt();
172         return data;
173     }
174
175     public int readUnsignedByte() throws IOException JavaDoc
176     {
177         createStream();
178         int data = dis.readUnsignedByte();
179         return data;
180     }
181
182     public int readUnsignedShort() throws IOException JavaDoc
183     {
184         createStream();
185         int data = dis.readUnsignedShort();
186         return data;
187     }
188
189     public long readLong() throws IOException JavaDoc
190     {
191         createStream();
192         long data = dis.readLong();
193         return data;
194     }
195
196     public short readShort() throws IOException JavaDoc
197     {
198         createStream();
199         short data = dis.readShort();
200         return data;
201     }
202
203     public boolean readBoolean() throws IOException JavaDoc
204     {
205         createStream();
206         boolean data = dis.readBoolean();
207         return data;
208     }
209
210     public int skipBytes(int n) throws IOException JavaDoc
211     {
212         createStream();
213         int data = dis.skipBytes(n);
214         return data;
215     }
216
217     public void readFully(byte b[]) throws IOException JavaDoc
218     {
219         createStream();
220         dis.readFully(b);
221     }
222
223     public void readFully(byte b[], int off, int len) throws IOException JavaDoc
224     {
225         createStream();
226         dis.readFully(b, off, len);
227     }
228
229     public String JavaDoc readUTF() throws IOException JavaDoc
230     {
231         createStream();
232         String JavaDoc data = dis.readUTF();
233         return data;
234     }
235
236     public InputStream JavaDoc getInputStream() throws IOException JavaDoc
237     {
238         createStream();
239         return dis;
240     }
241 }
Popular Tags