KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > http > HttpRandomAccesContent


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