1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33
34
35
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
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
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 }