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