forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputSubstream.java
More file actions
112 lines (97 loc) · 3.59 KB
/
InputSubstream.java
File metadata and controls
112 lines (97 loc) · 3.59 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
/*
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.services.s3.internal;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Filtered input stream implementation that exposes a range of an input stream
* as a new input stream.
*/
public final class InputSubstream extends FilterInputStream {
private long currentPosition;
private final long requestedOffset;
private final long requestedLength;
private final boolean closeSourceStream;
private long markedPosition = 0;
/**
* Constructs a new InputSubstream so that when callers start reading from
* this stream they'll start at the specified offset in the real stream and
* after they've read the specified length, this stream will look empty.
*
* @param in
* The input stream to wrap.
* @param offset
* The offset, in bytes, into the specified input stream at which
* to start reading data.
* @param length
* The length, in bytes, of the specified input stream to return
* through this stream.
* @param closeSourceStream
* True if the wrapped InputStream should be closed when this
* InputSubstream is closed.
*/
public InputSubstream(InputStream in, long offset, long length, boolean closeSourceStream) {
super(in);
this.currentPosition = 0;
this.requestedLength = length;
this.requestedOffset = offset;
this.closeSourceStream = closeSourceStream;
}
@Override
public int read() throws IOException {
byte[] b = new byte[1];
int bytesRead = read(b, 0, 1);
if (bytesRead == -1) return bytesRead;
return b[0];
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
while (currentPosition < requestedOffset) {
long skippedBytes = super.skip(requestedOffset - currentPosition);
currentPosition += skippedBytes;
}
long bytesRemaining = (requestedLength + requestedOffset) - currentPosition;
if (bytesRemaining <= 0) return -1;
len = (int) Math.min(len, bytesRemaining);
int bytesRead = super.read(b, off, len);
currentPosition += bytesRead;
return bytesRead;
}
@Override
public synchronized void mark(int readlimit) {
markedPosition = currentPosition;
super.mark(readlimit);
}
@Override
public synchronized void reset() throws IOException {
currentPosition = markedPosition;
super.reset();
}
@Override
public void close() throws IOException {
// Only close the wrapped input stream if we're at the end of
// the wrapped stream. We don't want to close the wrapped input
// stream just because we've reached the end of one subsection.
if (closeSourceStream) super.close();
}
@Override
public int available() throws IOException {
long bytesRemaining;
if (currentPosition < requestedOffset) bytesRemaining = requestedLength;
else bytesRemaining = (requestedLength + requestedOffset) - currentPosition;
return (int)Math.min(bytesRemaining, super.available());
}
}