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   * @param <FS> Type of HttpFileSystem.
34   * @deprecated Use {@link org.apache.commons.vfs2.provider.http5}.
35   */
36  @Deprecated
37  final class HttpRandomAccessContent<FS extends HttpFileSystem> extends AbstractRandomAccessStreamContent {
38  
39      protected long filePointer;
40  
41      private final HttpFileObject<FS> fileObject;
42      private final HttpFileSystem fileSystem;
43  
44      private DataInputStream dataInputStream;
45      private MonitorInputStream monitorInputStream;
46  
47      HttpRandomAccessContent(final HttpFileObject<FS> fileObject, final RandomAccessMode mode) {
48          super(mode);
49  
50          this.fileObject = fileObject;
51          fileSystem = (HttpFileSystem) this.fileObject.getFileSystem();
52      }
53  
54      @Override
55      public void close() throws IOException {
56          if (dataInputStream != null) {
57              dataInputStream.close();
58              dataInputStream = null;
59              monitorInputStream = null;
60          }
61      }
62  
63      @Override
64      protected DataInputStream getDataInputStream() throws IOException {
65          if (dataInputStream != null) {
66              return dataInputStream;
67          }
68  
69          final GetMethod getMethod = new GetMethod();
70          fileObject.setupMethod(getMethod);
71          getMethod.setRequestHeader("Range", "bytes=" + filePointer + "-");
72          final int status = fileSystem.getClient().executeMethod(getMethod);
73          if (status != HttpURLConnection.HTTP_PARTIAL && status != HttpURLConnection.HTTP_OK) {
74              throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(),
75                      Long.valueOf(filePointer), Integer.valueOf(status));
76          }
77  
78          monitorInputStream = new HttpFileObject.HttpInputStream(getMethod);
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          dataInputStream = new DataInputStream(new FilterInputStream(monitorInputStream) {
88              @Override
89              public int read() throws IOException {
90                  final int ret = super.read();
91                  if (ret > -1) {
92                      filePointer++;
93                  }
94                  return ret;
95              }
96  
97              @Override
98              public int read(final byte[] b) throws IOException {
99                  final int ret = super.read(b);
100                 if (ret > -1) {
101                     filePointer += ret;
102                 }
103                 return ret;
104             }
105 
106             @Override
107             public int read(final byte[] b, final int off, final int len) throws IOException {
108                 final int ret = super.read(b, off, len);
109                 if (ret > -1) {
110                     filePointer += ret;
111                 }
112                 return ret;
113             }
114         });
115 
116         return dataInputStream;
117     }
118 
119     @Override
120     public long getFilePointer() throws IOException {
121         return filePointer;
122     }
123 
124     @Override
125     public long length() throws IOException {
126         return fileObject.getContent().getSize();
127     }
128 
129     @Override
130     public void seek(final long pos) throws IOException {
131         if (pos == filePointer) {
132             // no change
133             return;
134         }
135 
136         if (pos < 0) {
137             throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos));
138         }
139         if (dataInputStream != null) {
140             close();
141         }
142 
143         filePointer = pos;
144     }
145 }