forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressReportingInputStream.java
More file actions
138 lines (122 loc) · 4.91 KB
/
ProgressReportingInputStream.java
File metadata and controls
138 lines (122 loc) · 4.91 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
/*
* 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.event;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Simple InputStream wrapper that occasionally notifies a progress listener
* about the number of bytes transferred.
* <p>
* This class could be used for both Amazon S3 and Amazon Glacier clients. The
* legacy Amazon Amazon S3 ProgressReportingInputStream
* {@link com.amazonaws.services.s3.internal.ProgressReportingInputStream} is
* deprecated in favor of this new class.
* </p>
*/
public class ProgressReportingInputStream extends FilterInputStream {
/** The threshold of bytes between notifications. */
private static final int NOTIFICATION_THRESHOLD = 8 * 1024;
/** The listener callback executor */
private final ProgressListenerCallbackExecutor listenerCallbackExecutor;
/** The number of bytes read that the listener hasn't been notified about yet. */
private int unnotifiedByteCount;
/** True if this stream should fire a completed progress event when the stream runs out. */
private boolean fireCompletedEvent;
/**
* Creates a new progress reporting input stream that simply wraps the
* specified input stream and uses the specified listener callback executor to
* asynchronously notify the listener about the number of bytes transferred.
*
* @param in
* The input stream to wrap.
* @param listenerCallbackExecutor
* The listener callback executor that wraps the listener to notify about progress.
*/
public ProgressReportingInputStream(final InputStream in, final ProgressListenerCallbackExecutor listenerCallbackExecutor) {
super(in);
this.listenerCallbackExecutor = listenerCallbackExecutor;
}
/**
* Sets whether this input stream should fire an event with code
* {@link ProgressEvent#COMPLETED_EVENT_CODE} when this stream runs out of
* data. By default, completed events are not fired by this stream.
*
* @param fireCompletedEvent
* Whether this input stream should fire an event to indicate
* that the stream has been fully read.
*/
public void setFireCompletedEvent(boolean fireCompletedEvent) {
this.fireCompletedEvent = fireCompletedEvent;
}
/**
* Returns whether this input stream should fire an event with code
* {@link ProgressEvent#COMPLETED_EVENT_CODE} when this stream runs out of
* data. By default, completed events are not fired by this stream.
*
* @return Whether this input stream should fire an event to indicate that
* the stream has been fully read.
*/
public boolean getFireCompletedEvent() {
return fireCompletedEvent;
}
@Override
public int read() throws IOException {
int data = super.read();
if (data == -1) {
notifyCompleted();
} else {
notify(1);
}
return data;
}
@Override
public void reset() throws IOException {
super.reset();
ProgressEvent event = new ProgressEvent(unnotifiedByteCount);
event.setEventCode(ProgressEvent.RESET_EVENT_CODE);
listenerCallbackExecutor.progressChanged(event);
unnotifiedByteCount = 0;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int bytesRead = super.read(b, off, len);
if (bytesRead == -1) notifyCompleted();
if (bytesRead != -1) notify(bytesRead);
return bytesRead;
}
@Override
public void close() throws IOException {
if (unnotifiedByteCount > 0) {
listenerCallbackExecutor.progressChanged(new ProgressEvent(unnotifiedByteCount));
unnotifiedByteCount = 0;
}
super.close();
}
private void notifyCompleted() {
if (fireCompletedEvent == false) return;
ProgressEvent event = new ProgressEvent(unnotifiedByteCount);
event.setEventCode(ProgressEvent.COMPLETED_EVENT_CODE);
unnotifiedByteCount = 0;
listenerCallbackExecutor.progressChanged(event);
}
private void notify(int bytesRead) {
unnotifiedByteCount += bytesRead;
if (unnotifiedByteCount >= NOTIFICATION_THRESHOLD) {
listenerCallbackExecutor.progressChanged(new ProgressEvent(unnotifiedByteCount));
unnotifiedByteCount = 0;
}
}
}