View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs.provider.http;
18  
19  import org.apache.commons.httpclient.methods.GetMethod;
20  import org.apache.commons.vfs.FileSystemException;
21  import org.apache.commons.vfs.provider.AbstractRandomAccessStreamContent;
22  import org.apache.commons.vfs.util.MonitorInputStream;
23  import org.apache.commons.vfs.util.RandomAccessMode;
24  
25  import java.io.DataInputStream;
26  import java.io.FilterInputStream;
27  import java.io.IOException;
28  import java.net.HttpURLConnection;
29  
30  class HttpRandomAccesContent extends AbstractRandomAccessStreamContent
31  {
32      private final HttpFileObject fileObject;
33      private final HttpFileSystem fileSystem;
34  
35      protected long filePointer = 0;
36      private DataInputStream 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
48      {
49          return filePointer;
50      }
51  
52      public void seek(long pos) throws IOException
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[]
64                  {
65                      new Long(pos)
66                  });
67          }
68          if (dis != null)
69          {
70              close();
71          }
72  
73          filePointer = pos;
74      }
75  
76  	protected DataInputStream getDataInputStream() throws IOException
77  	{
78          if (dis != null)
79          {
80              return dis;
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[]
90              {
91                  fileObject.getName(),
92                  new Long(filePointer)
93              });
94          }
95  
96          mis = new HttpFileObject.HttpInputStream(getMethod);
97          dis = new DataInputStream(new FilterInputStream(mis)
98          {
99              public int read() throws IOException
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
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
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 		return dis;
131 	}
132 
133 
134     public void close() throws IOException
135     {
136         if (dis != null)
137         {
138             dis.close();
139             dis = null;
140             mis = null;
141         }
142     }
143 
144     public long length() throws IOException
145     {
146         return fileObject.getContent().getSize();
147     }
148 }