-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpSeekableChannel.java
More file actions
188 lines (169 loc) · 5.64 KB
/
Copy pathHttpSeekableChannel.java
File metadata and controls
188 lines (169 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.NonWritableChannelException;
import java.nio.channels.SeekableByteChannel;
public class HttpSeekableChannel implements SeekableByteChannel {
private long position = 0;
private long size;
private byte[] headBytes;
private final URL url;
private HttpURLConnection connection;
private InputStream body;
private String ifRange;
private final int discard;
private int count;
public HttpSeekableChannel(URL url) throws IOException {
this.url = url;
this.discard = 1024 * 512;
init();
}
public HttpSeekableChannel(URL url, int discard) throws IOException {
this.url = url;
this.discard = discard;
init();
}
private void request() throws IOException {
if (body != null) {
body.close();
}
if (connection != null) {
connection.getInputStream().close();
}
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Range", String.format("bytes=%d-", position));
connection.setRequestProperty("If-Range", ifRange);
connection.connect();
body = connection.getInputStream();
count++;
if (connection.getResponseCode() != 206) {
throw new IOException("not 206 partial content or resource changed");
}
}
private void init() throws IOException {
if (!"https".equalsIgnoreCase(url.getProtocol()) && !"http".equalsIgnoreCase(url.getProtocol())) {
throw new IOException("only support http and https");
}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Range", "bytes=0-511");
connection.connect();
count++;
byte[] buff = new byte[512];
try (InputStream in = connection.getInputStream()) {
int n = in.read(buff);
byte[] head = new byte[n];
System.arraycopy(buff, 0, head, 0, n);
headBytes = head;
}
if (connection.getResponseCode() < 200 || connection.getResponseCode() >= 300) {
throw new IOException(String.format("unexpected response (status %d)", connection.getResponseCode()));
}
String range = connection.getHeaderField("Content-Range");
if (range.contains("/")) {
size = Long.parseLong(range.split("/")[1]);
ifRange = validatorFromResponse(connection);
} else {
throw new IOException("invalid response header Content-Range " + range);
}
}
private String validatorFromResponse(HttpURLConnection connection) throws IOException {
String etag = connection.getHeaderField("ETag");
if (etag != null && !etag.equals("") && etag.charAt(0) == '"') {
return etag;
}
String modifiedTime = connection.getHeaderField("Last-Modified");
if (modifiedTime != null && !modifiedTime.equals("")) {
return modifiedTime;
}
throw new IOException(url.toString() + " did not offer a strong-enough validator for subsequent requests");
}
public byte[] getHeadBytes() {
return headBytes;
}
public int getCount() {
return count;
}
@Override
public int read(ByteBuffer dst) throws IOException {
if (position >= size) {
return -1;
}
if (body == null) {
request();
}
int total = 0;
int remain = dst.remaining();
while (total < remain) {
int n = body.read(dst.array(), dst.position(), dst.remaining());
if (n >= 0) {
dst.position(dst.position() + n);
position = position + n;
total = total + n;
} else {
if (total == 0) {
return n;
} else {
break;
}
}
}
return total;
}
@Override
public int write(ByteBuffer src) throws IOException {
throw new IOException("read-only");
}
@Override
public long position() throws IOException {
return position;
}
@Override
public SeekableByteChannel position(long newPosition) throws IOException {
if (newPosition == position) {
return this;
}
if (newPosition >= size) {
throw new IOException("seek beyond end of file");
}
if (newPosition < 0) {
throw new IOException("seek before beginning of file");
}
long length = newPosition - position;
if (length <= discard && length >= 0 && body != null) {
long n = body.skip(length);
if (n != length) {
throw new IOException("skip data error");
}
position = newPosition;
} else {
position = newPosition;
request();
}
return this;
}
@Override
public long size() throws IOException {
return size;
}
@Override
public SeekableByteChannel truncate(long size) throws IOException {
throw new NonWritableChannelException();
}
@Override
public boolean isOpen() {
return true;
}
@Override
public void close() throws IOException {
if (body != null) {
body.close();
}
if (connection != null) {
connection.disconnect();
}
}
}