forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractRepeatableInputStream.java
More file actions
104 lines (87 loc) · 3.04 KB
/
AbstractRepeatableInputStream.java
File metadata and controls
104 lines (87 loc) · 3.04 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
/*
* Copyright 2013-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Portions copyright 2006-2009 James Murty. Please see LICENSE.txt
* for applicable license terms and NOTICE.txt for applicable notices.
*
* 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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Abstract base class for input stream wrappers that add support for
* mark/resetting streams that don't natively support it. Implementations must
* provide their own support for reopening streams and rereading to the last
* marked position.
*/
public abstract class AbstractRepeatableInputStream extends FilterInputStream {
private static final Log log = LogFactory.getLog(AbstractRepeatableInputStream.class);
private long bytesReadPastMarkPoint = 0;
private long markPoint = 0;
protected AbstractRepeatableInputStream(InputStream in) {
super(in);
}
@Override
public boolean markSupported() {
return true;
}
@Override
public synchronized void mark(int readlimit) {
this.markPoint += bytesReadPastMarkPoint;
this.bytesReadPastMarkPoint = 0;
if (log.isDebugEnabled()) {
log.debug("Input stream marked at " + this.markPoint + " bytes");
}
}
protected abstract void reopenWrappedStream() throws IOException;
@Override
public synchronized void reset() throws IOException {
reopenWrappedStream();
long skipped = 0;
long toSkip = markPoint;
while (toSkip > 0) {
skipped = skip(toSkip);
toSkip -= skipped;
}
if (log.isDebugEnabled()) {
log.debug("Reseting to mark point " + markPoint
+ " after returning " + bytesReadPastMarkPoint + " bytes");
}
this.bytesReadPastMarkPoint = 0;
}
@Override
public int read() throws IOException {
int byteRead = super.read();
if (byteRead != -1) {
bytesReadPastMarkPoint++;
return byteRead;
} else {
return -1;
}
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int count = super.read(b, off, len);
bytesReadPastMarkPoint += count;
return count;
}
@Override
public long skip(long n) throws IOException {
long skipped = super.skip(n);
bytesReadPastMarkPoint += skipped;
return skipped;
}
}