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.vfs2.provider.http;
18  
19  import java.io.DataInputStream;
20  import java.io.FilterInputStream;
21  import java.io.IOException;
22  import java.net.HttpURLConnection;
23  
24  import org.apache.commons.httpclient.methods.GetMethod;
25  import org.apache.commons.vfs2.FileSystemException;
26  import org.apache.commons.vfs2.provider.AbstractRandomAccessStreamContent;
27  import org.apache.commons.vfs2.util.MonitorInputStream;
28  import org.apache.commons.vfs2.util.RandomAccessMode;
29  
30  /**
31   * RandomAccess content using HTTP.
32   */
33  class HttpRandomAccessContent<FS extends HttpFileSystem> extends AbstractRandomAccessStreamContent {
34  
35      protected long filePointer = 0;
36  
37      private final HttpFileObject<FS> fileObject;
38      private final HttpFileSystem fileSystem;
39  
40      private DataInputStream dis = null;
41      private MonitorInputStream mis = null;
42  
43      HttpRandomAccessContent(final HttpFileObject<FS> fileObject, final RandomAccessMode mode) {
44          super(mode);
45  
46          this.fileObject = fileObject;
47          fileSystem = (HttpFileSystem) this.fileObject.getFileSystem();
48      }
49  
50      @Override
51      public long getFilePointer() throws IOException {
52          return filePointer;
53      }
54  
55      @Override
56      public void seek(final long pos) throws IOException {
57          if (pos == filePointer) {
58              // no change
59              return;
60          }
61  
62          if (pos < 0) {
63              throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos));
64          }
65          if (dis != null) {
66              close();
67          }
68  
69          filePointer = pos;
70      }
71  
72      @Override
73      protected DataInputStream getDataInputStream() throws IOException {
74          if (dis != null) {
75              return dis;
76          }
77  
78          final GetMethod getMethod = new GetMethod();
79          fileObject.setupMethod(getMethod);
80          getMethod.setRequestHeader("Range", "bytes=" + filePointer + "-");
81          final int status = fileSystem.getClient().executeMethod(getMethod);
82          if (status != HttpURLConnection.HTTP_PARTIAL && status != HttpURLConnection.HTTP_OK) {
83              throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(),
84                      Long.valueOf(filePointer), Integer.valueOf(status));
85          }
86  
87          mis = new HttpFileObject.HttpInputStream(getMethod);
88          // If the range request was ignored
89          if (status == HttpURLConnection.HTTP_OK) {
90              final long skipped = mis.skip(filePointer);
91              if (skipped != filePointer) {
92                  throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(),
93                          Long.valueOf(filePointer), Integer.valueOf(status));
94              }
95          }
96          dis = new DataInputStream(new FilterInputStream(mis) {
97              @Override
98              public int read() throws IOException {
99                  final int ret = super.read();
100                 if (ret > -1) {
101                     filePointer++;
102                 }
103                 return ret;
104             }
105 
106             @Override
107             public int read(final byte[] b) throws IOException {
108                 final int ret = super.read(b);
109                 if (ret > -1) {
110                     filePointer += ret;
111                 }
112                 return ret;
113             }
114 
115             @Override
116             public int read(final byte[] b, final int off, final int len) throws IOException {
117                 final int ret = super.read(b, off, len);
118                 if (ret > -1) {
119                     filePointer += ret;
120                 }
121                 return ret;
122             }
123         });
124 
125         return dis;
126     }
127 
128     @Override
129     public void close() throws IOException {
130         if (dis != null) {
131             dis.close();
132             dis = null;
133             mis = null;
134         }
135     }
136 
137     @Override
138     public long length() throws IOException {
139         return fileObject.getContent().getSize();
140     }
141 }