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