-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHttpCogImageInputStream.java
328 lines (263 loc) · 8.31 KB
/
HttpCogImageInputStream.java
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package it.geosolutions.imageioimpl.plugins.tiff.stream;
import it.geosolutions.imageioimpl.plugins.tiff.CogTileInfo;
import it.geosolutions.imageioimpl.plugins.tiff.HttpRangeReader;
import it.geosolutions.imageioimpl.plugins.tiff.RangeBuilder;
import it.geosolutions.imageioimpl.plugins.tiff.RangeReader;
import javax.imageio.stream.IIOByteBuffer;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.ByteOrder;
import java.util.logging.Logger;
/**
* @author joshfix
* Created on 2019-08-23
*/
public class HttpCogImageInputStream implements ImageInputStream, CogImageInputStream {
protected int headerByteLength = 16384;
protected URI uri;
protected CogTileInfo cogTileInfo = new CogTileInfo();
protected RangeReader rangeReader;
protected MemoryCacheImageInputStream delegate;
private final static Logger LOGGER = Logger.getLogger(HttpCogImageInputStream.class.getName());
public HttpCogImageInputStream(String url) {
this(URI.create(url));
}
public HttpCogImageInputStream(URL url) {
this(URI.create(url.toString()));
}
public HttpCogImageInputStream(URI uri) {
this.uri = uri;
rangeReader = new HttpRangeReader(uri);
rangeReader.readHeader(headerByteLength);
// wrap the result in a MemoryCacheInputStream
delegate = new MemoryCacheImageInputStream(new ByteArrayInputStream(rangeReader.getBytes()));
}
@Override
public CogTileInfo getCogTileInfo() {
return cogTileInfo;
}
@Override
public void setHeaderByteLength(int headerByteLength) {
this.headerByteLength = headerByteLength;
}
@Override
public void readRanges() {
// read data with the RangeReader and set the byte order and pointer on the new input stream
long firstTileOffset = cogTileInfo.getFirstTileOffset();
long firstTileByteLength = cogTileInfo.getFirstTileByteLength();
RangeBuilder rangeBuilder = new RangeBuilder(firstTileOffset, firstTileOffset +firstTileByteLength);
cogTileInfo.getTileRanges().forEach((tileIndex, tileRange) ->
rangeBuilder.addTileRange(tileRange.getStart(), tileRange.getByteLength()));
// read all of the ranges asynchronously
long[][] ranges = rangeBuilder.getRanges().toArray(new long[][]{});
LOGGER.fine("Submitting " + ranges.length + " range request(s)");
rangeReader.readAsync(ranges);
// obtain the byte order and stream position from the existing delegate
ByteOrder byteOrder = delegate.getByteOrder();
long streamPos = 0;
try {
streamPos = delegate.getStreamPosition();
} catch (IOException e) {
e.printStackTrace();
}
// create a new delegate with the newly acquired bytes
delegate = new MemoryCacheImageInputStream(new ByteArrayInputStream(rangeReader.getBytes()));
// set the byte order and stream position
delegate.setByteOrder(byteOrder);
try {
delegate.seek(streamPos);
} catch (IOException e) {
e.printStackTrace();
}
}
public String getUrl() {
return uri.toString();
}
public ImageInputStream getDelegate() {
return delegate;
}
@Override
public void setByteOrder(ByteOrder byteOrder) {
delegate.setByteOrder(byteOrder);
}
@Override
public ByteOrder getByteOrder() {
return delegate.getByteOrder();
}
@Override
public int read() throws IOException {
return delegate.read();
}
@Override
public int read(byte[] b) throws IOException {
return delegate.read(b);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return delegate.read(b, off, len);
}
@Override
public long length() {
return rangeReader.getBytes().length;
}
@Override
public int skipBytes(int n) throws IOException {
return delegate.skipBytes(n);
}
@Override
public long skipBytes(long n) throws IOException {
return delegate.skipBytes(n);
}
@Override
public void seek(long pos) throws IOException {
delegate.seek(pos);
}
@Override
public void mark() {
delegate.mark();
}
@Override
public void reset() throws IOException {
delegate.reset();
}
@Override
public void flushBefore(long pos) throws IOException {
delegate.flushBefore(pos);
}
@Override
public void flush() throws IOException {
delegate.flush();
}
@Override
public long getFlushedPosition() {
return delegate.getFlushedPosition();
}
@Override
public boolean isCached() {
return delegate.isCached();
}
@Override
public boolean isCachedMemory() {
return delegate.isCachedMemory();
}
@Override
public boolean isCachedFile() {
return delegate.isCachedFile();
}
@Override
public void close() throws IOException {
delegate.close();
}
@Override
public void readBytes(IIOByteBuffer buf, int len) throws IOException {
delegate.readBytes(buf, len);
}
@Override
public boolean readBoolean() throws IOException {
return delegate.readBoolean();
}
@Override
public byte readByte() throws IOException {
return delegate.readByte();
}
@Override
public int readUnsignedByte() throws IOException {
return delegate.readUnsignedByte();
}
@Override
public short readShort() throws IOException {
return delegate.readShort();
}
@Override
public int readUnsignedShort() throws IOException {
return delegate.readUnsignedShort();
}
@Override
public char readChar() throws IOException {
return delegate.readChar();
}
@Override
public int readInt() throws IOException {
return delegate.readInt();
}
@Override
public long readUnsignedInt() throws IOException {
return delegate.readUnsignedInt();
}
@Override
public long readLong() throws IOException {
return delegate.readLong();
}
@Override
public float readFloat() throws IOException {
return delegate.readFloat();
}
@Override
public double readDouble() throws IOException {
return delegate.readDouble();
}
@Override
public String readLine() throws IOException {
return delegate.readLine();
}
@Override
public String readUTF() throws IOException {
return delegate.readUTF();
}
@Override
public void readFully(byte[] b, int off, int len) throws IOException {
delegate.readFully(b, off, len);
}
@Override
public void readFully(byte[] b) throws IOException {
delegate.readFully(b);
}
@Override
public void readFully(short[] s, int off, int len) throws IOException {
delegate.readFully(s, off, len);
}
@Override
public void readFully(char[] c, int off, int len) throws IOException {
delegate.readFully(c, off, len);
}
@Override
public void readFully(int[] i, int off, int len) throws IOException {
delegate.readFully(i, off, len);
}
@Override
public void readFully(long[] l, int off, int len) throws IOException {
delegate.readFully(l, off, len);
}
@Override
public void readFully(float[] f, int off, int len) throws IOException {
delegate.readFully(f, off, len);
}
@Override
public void readFully(double[] d, int off, int len) throws IOException {
delegate.readFully(d, off, len);
}
@Override
public long getStreamPosition() throws IOException {
return delegate.getStreamPosition();
}
@Override
public int getBitOffset() throws IOException {
return delegate.getBitOffset();
}
@Override
public void setBitOffset(int bitOffset) throws IOException {
delegate.setBitOffset(bitOffset);
}
@Override
public int readBit() throws IOException {
return delegate.readBit();
}
@Override
public long readBits(int numBits) throws IOException {
return delegate.readBits(numBits);
}
}