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.http4;
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.vfs2.FileSystemException;
25  import org.apache.commons.vfs2.provider.AbstractRandomAccessStreamContent;
26  import org.apache.commons.vfs2.util.MonitorInputStream;
27  import org.apache.commons.vfs2.util.RandomAccessMode;
28  import org.apache.http.HttpResponse;
29  import org.apache.http.client.methods.HttpGet;
30  
31  /**
32   * RandomAccess content using {@code Http4FileObject}.
33   *
34   * @param <FS> Type of HttpFileSystem.
35   * @deprecated Use {@link org.apache.commons.vfs2.provider.http5}.
36   */
37  @Deprecated
38  final class Http4RandomAccessContent<FS extends Http4FileSystem> extends AbstractRandomAccessStreamContent {
39  
40      protected long filePointer;
41  
42      private final Http4FileObject<FS> fileObject;
43  
44      private DataInputStream dataInputStream;
45      private MonitorInputStream monitorInputStream;
46  
47      Http4RandomAccessContent(final Http4FileObject<FS> fileObject, final RandomAccessMode mode) {
48          super(mode);
49          this.fileObject = fileObject;
50      }
51  
52      @Override
53      public void close() throws IOException {
54          if (dataInputStream != null) {
55              dataInputStream.close();
56              dataInputStream = null;
57              monitorInputStream = null;
58          }
59      }
60  
61      @Override
62      protected DataInputStream getDataInputStream() throws IOException {
63          if (dataInputStream != null) {
64              return dataInputStream;
65          }
66  
67          final HttpGet httpGet = new HttpGet(fileObject.getInternalURI());
68          httpGet.setHeader("Range", "bytes=" + filePointer + "-");
69          final HttpResponse httpResponse = fileObject.executeHttpUriRequest(httpGet);
70          final int status = httpResponse.getStatusLine().getStatusCode();
71  
72          if (status != HttpURLConnection.HTTP_PARTIAL && status != HttpURLConnection.HTTP_OK) {
73              throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(),
74                      Long.valueOf(filePointer), Integer.valueOf(status));
75          }
76  
77          monitorInputStream = new MonitoredHttpResponseContentInputStream(httpResponse);
78  
79          // If the range request was ignored
80          if (status == HttpURLConnection.HTTP_OK) {
81              final long skipped = monitorInputStream.skip(filePointer);
82              if (skipped != filePointer) {
83                  throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(),
84                          Long.valueOf(filePointer), Integer.valueOf(status));
85              }
86          }
87  
88          dataInputStream = new DataInputStream(new FilterInputStream(monitorInputStream) {
89              @Override
90              public int read() throws IOException {
91                  final int ret = super.read();
92                  if (ret > -1) {
93                      filePointer++;
94                  }
95                  return ret;
96              }
97  
98              @Override
99              public int read(final byte[] b) throws IOException {
100                 final int ret = super.read(b);
101                 if (ret > -1) {
102                     filePointer += ret;
103                 }
104                 return ret;
105             }
106 
107             @Override
108             public int read(final byte[] b, final int off, final int len) throws IOException {
109                 final int ret = super.read(b, off, len);
110                 if (ret > -1) {
111                     filePointer += ret;
112                 }
113                 return ret;
114             }
115         });
116 
117         return dataInputStream;
118     }
119 
120     @Override
121     public long getFilePointer() throws IOException {
122         return filePointer;
123     }
124 
125     @Override
126     public long length() throws IOException {
127         return fileObject.getContent().getSize();
128     }
129 
130     @Override
131     public void seek(final long pos) throws IOException {
132         if (pos == filePointer) {
133             // no change
134             return;
135         }
136 
137         if (pos < 0) {
138             throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos));
139         }
140 
141         if (dataInputStream != null) {
142             close();
143         }
144 
145         filePointer = pos;
146     }
147 }