-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathMantaSeekableByteChannel.java
More file actions
443 lines (372 loc) · 15 KB
/
MantaSeekableByteChannel.java
File metadata and controls
443 lines (372 loc) · 15 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* Copyright (c) 2016-2019, Joyent, Inc. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.joyent.manta.client;
import com.joyent.manta.exception.MantaClientException;
import com.joyent.manta.exception.MantaResourceCloseException;
import com.joyent.manta.http.HttpHelper;
import com.joyent.manta.http.MantaConnectionFactory;
import com.joyent.manta.http.MantaHttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.NonWritableChannelException;
import java.nio.channels.SeekableByteChannel;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
/**
* A read-only {@link SeekableByteChannel} implementation that utilizes
* the HTTP Range header to allow you to seek any position in an object on
* Manta. Connection opening to the remote server happens lazily upon the
* first read() or size() method invoked.
*
* @author <a href="https://github.com/dekobon">Elijah Zupancic</a>
* @author <a href="https://github.com/nairashwin952013">Ashwin A Nair</a>
*/
public class MantaSeekableByteChannel extends InputStream
implements SeekableByteChannel {
/**
* Constant representing the value returned when we have reached the
* end of a stream.
*/
private static final int EOF = -1;
/**
* Constant representing an unknown HTTP content length value.
*/
private static final long UNKNOWN_CONTENT_LENGTH = -1L;
/**
* Flag indicating if the channel is open. Marked as volatile so
* that different threads can flip its state.
*/
private volatile boolean open = true;
/**
* Path of the object on the Manta API.
*/
private final String path;
/**
* Current position in bytes from the start of the file.
*/
private AtomicLong position;
/**
* Helper class providing useful HTTP functions.
*/
private final HttpHelper httpHelper;
/**
* Threadsafe reference to the Manta API HTTP request object.
*/
private final AtomicReference<HttpUriRequest> requestRef;
/**
* Threadsafe reference to the Manta API HTTP response object.
*/
private final AtomicReference<MantaObjectInputStream> responseStream;
/**
* Creates a new instance of a read-only seekable byte channel.
*
* @param path path of the object on the Manta API
* @param position starting position in bytes from the start of the file
* @param connectionFactory connection factory instance used for building requests to Manta
* @param httpHelper helper class providing useful HTTP functions
*/
@Deprecated
public MantaSeekableByteChannel(final String path,
final long position,
final MantaConnectionFactory connectionFactory,
final HttpHelper httpHelper) {
this.path = path;
this.position = new AtomicLong(position);
this.httpHelper = httpHelper;
this.requestRef = new AtomicReference<>();
this.responseStream = new AtomicReference<>();
}
/**
* Creates a new instance of a read-only seekable byte channel.
*
* @param path path of the object on the Manta API
* @param connectionFactory connection factory instance used for building requests to Manta
* @param httpHelper helper class providing useful HTTP functions
*/
@Deprecated
public MantaSeekableByteChannel(final String path,
final MantaConnectionFactory connectionFactory,
final HttpHelper httpHelper) {
this(path, 0L, connectionFactory, httpHelper);
}
/**
* Constructor used for creating a new instance of a read-only seekable byte
* channel from within this class. This is used when position() is called.
*
* @param requestRef reference to existing HTTP request
* @param responseStream reference to existing HTTP response
* @param path path of the object on the Manta API
* @param position starting position in bytes from the start of the file
* @param connectionFactory connection factory instance used for building requests to Manta
* @param httpHelper helper class providing useful HTTP functions
*/
@Deprecated
protected MantaSeekableByteChannel(final AtomicReference<HttpUriRequest> requestRef,
final AtomicReference<MantaObjectInputStream> responseStream,
final String path,
final AtomicLong position,
final MantaConnectionFactory connectionFactory,
final HttpHelper httpHelper) {
this.requestRef = requestRef;
this.responseStream = responseStream;
this.path = path;
this.position = position;
this.httpHelper = httpHelper;
}
/**
* Creates a new instance of a read-only seekable byte channel.
*
* @param path path of the object on the Manta API
* @param position starting position in bytes from the start of the file
* @param httpHelper helper class providing useful HTTP functions
*/
public MantaSeekableByteChannel(final String path,
final long position,
final HttpHelper httpHelper) {
this.path = path;
this.position = new AtomicLong(position);
this.httpHelper = httpHelper;
this.requestRef = new AtomicReference<>();
this.responseStream = new AtomicReference<>();
}
/**
* Creates a new instance of a read-only seekable byte channel.
*
* @param path path of the object on the Manta API
* @param httpHelper helper class providing useful HTTP functions
*/
public MantaSeekableByteChannel(final String path,
final HttpHelper httpHelper) {
this(path, 0L, httpHelper);
}
/**
* Constructor used for creating a new instance of a read-only seekable byte
* channel from within this class. This is used when position() is called.
*
* @param requestRef reference to existing HTTP request
* @param responseStream reference to existing HTTP response
* @param path path of the object on the Manta API
* @param position starting position in bytes from the start of the file
* @param httpHelper helper class providing useful HTTP functions
*/
protected MantaSeekableByteChannel(final AtomicReference<HttpUriRequest> requestRef,
final AtomicReference<MantaObjectInputStream> responseStream,
final String path,
final AtomicLong position,
final HttpHelper httpHelper) {
this.requestRef = requestRef;
this.responseStream = responseStream;
this.path = path;
this.position = position;
this.httpHelper = httpHelper;
}
@Override
public int read(final ByteBuffer dst) throws IOException, UnsupportedOperationException {
if (!open) {
throw new ClosedChannelException();
}
final MantaObjectInputStream stream = connectOrGetResponse();
final long size = size();
if (position.get() >= size) {
return EOF;
}
if (dst.isReadOnly()) {
throw new MantaClientException("Read-only buffer", new IllegalArgumentException());
}
if (!dst.hasArray()) {
throw new MantaClientException("Buffer read not backed by an accessible\n"
+ "byte array", new UnsupportedOperationException());
} else {
final byte[] buff = new byte[dst.remaining()];
ByteBuffer dstCopy = dst.get(buff);
dst.position(dstCopy.position() - buff.length); // Restores the buffer position
final int startIndex = dstCopy.arrayOffset();
final int endIndex = startIndex + dstCopy.position() + dstCopy.remaining();
final int buffLength = endIndex - startIndex;
final int bytesRead = stream.read(buff, startIndex, buffLength);
this.position.addAndGet(bytesRead);
return bytesRead;
}
}
/**
* Reads the next byte of data from the backing input stream at the current
* position. The value byte is returned as an <code>int</code> in the range
* <code>0</code> to <code>255</code>. If no byte is available because the
* end of the stream has been reached, the value <code>-1</code> is returned.
* This method blocks until input data is available, the end of the stream
* is detected, or an exception is thrown.
*
* @return the next byte of data, or <code>-1</code> if the end of the
* stream is reached.
* @exception IOException if an I/O error occurs.
*/
@Override
public int read() throws IOException {
if (!open) {
throw new ClosedChannelException();
}
final MantaObjectInputStream stream = connectOrGetResponse();
final int read = stream.read();
if (read > -1) {
position.incrementAndGet();
}
return read;
}
@Override
public int read(final byte[] buffer) throws IOException {
if (!open) {
throw new ClosedChannelException();
}
final MantaObjectInputStream stream = connectOrGetResponse();
final int totalRead = stream.read(buffer);
if (totalRead > -1) {
position.addAndGet(totalRead);
}
return totalRead;
}
@Override
public int read(final byte[] buffer, final int offset, final int length)
throws IOException {
if (!open) {
throw new ClosedChannelException();
}
final MantaObjectInputStream stream = connectOrGetResponse();
final int totalRead = stream.read(buffer, offset, length);
if (totalRead > -1) {
position.addAndGet(totalRead);
}
return totalRead;
}
@Override
public long skip(final long noOfBytesToSkip) throws IOException {
if (!open) {
return 0;
}
final MantaObjectInputStream stream = connectOrGetResponse();
final long totalSkipped = stream.skip(noOfBytesToSkip);
position.addAndGet(totalSkipped);
return totalSkipped;
}
@Override
public int available() throws IOException {
if (!open) {
throw new ClosedChannelException();
}
final MantaObjectInputStream stream = connectOrGetResponse();
return stream.available();
}
@Override
public int write(final ByteBuffer src) throws IOException {
// This is a read-only channel
throw new NonWritableChannelException();
}
@Override
public long position() throws IOException {
return position.get();
}
@Override
public SeekableByteChannel position(final long newPosition) throws IOException {
return new MantaSeekableByteChannel(
new AtomicReference<>(),
new AtomicReference<>(),
path,
new AtomicLong(newPosition),
httpHelper);
}
@Override
public long size() throws IOException {
if (!open) {
throw new ClosedChannelException();
}
final MantaObjectInputStream stream = connectOrGetResponse();
final long contentLength = stream.getContentLength();
if (contentLength == UNKNOWN_CONTENT_LENGTH) {
MantaClientException e = new MantaClientException(
"Can't get SeekableByteChannel for objects of unknown size");
HttpUriRequest request = requestRef.get();
if (request != null) {
@SuppressWarnings("unchecked")
HttpResponse response = (HttpResponse)stream.getHttpResponse();
HttpHelper.annotateContextedException(e, request, response);
}
throw e;
}
return contentLength;
}
@Override
public synchronized void mark(final int readlimit) {
}
@Override
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
@Override
public boolean markSupported() {
return false;
}
@Override
public SeekableByteChannel truncate(final long newSize) throws IOException {
throw new NonWritableChannelException();
}
@Override
public boolean isOpen() {
return open;
}
@Override
public synchronized void close() throws IOException {
if (!open) {
return;
}
MantaObjectInputStream stream = responseStream.getAndSet(null);
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
String msg = "Problem closing response stream";
throw new MantaResourceCloseException(msg, e);
}
open = false;
}
/**
* Connects to the Manta API, updates the atomic reference and returns a
* response if the atomic reference hasn't been set. Otherwise, it just returns
* the response embedded in the atomic reference.
*
* @return HTTP response object
* @throws IOException thrown when there are network problems connecting to the remote API
*/
protected synchronized MantaObjectInputStream connectOrGetResponse() throws IOException {
if (responseStream.get() != null) {
return responseStream.get();
}
final HttpUriRequest request = httpHelper.getRequestFactory().get(path);
final MantaHttpHeaders headers = new MantaHttpHeaders();
// Set byte range requested via HTTP range header
headers.setRange(String.format("bytes=%d-", position.get()));
// Store the request so that we can use it for adding information to exceptions
this.requestRef.set(request);
MantaObjectInputStream stream = httpHelper.httpRequestAsInputStream(
request, headers);
responseStream.compareAndSet(null, stream);
final String contentType = stream.getContentType();
if (MantaObjectResponse.DIRECTORY_RESPONSE_CONTENT_TYPE.equals(contentType)) {
MantaClientException e = new MantaClientException(
"Can't get SeekableByteChannel for directory objects");
@SuppressWarnings("unchecked")
HttpResponse response = (HttpResponse)stream.getHttpResponse();
HttpHelper.annotateContextedException(e, request, response);
throw e;
}
return stream;
}
}