1
15 package com.amazonaws.internal;
16
17 import static com.amazonaws.util.SdkRuntime.shouldAbort;
18
19 import com.amazonaws.AbortedException;
20 import com.amazonaws.annotation.SdkProtectedApi;
21 import java.io.FilterInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24
25
28 public class SdkFilterInputStream extends FilterInputStream implements
29 MetricAware, Releasable {
30 private volatile boolean aborted = false;
31
32 protected SdkFilterInputStream(InputStream in) {
33 super(in);
34 }
35
36
39 @SdkProtectedApi
40 public InputStream getDelegateStream() {
41 return in;
42 }
43
44 @Override
45 public boolean isMetricActivated() {
46 if (in instanceof MetricAware) {
47 MetricAware metricAware = (MetricAware)in;
48 return metricAware.isMetricActivated();
49 }
50 return false;
51 }
52
53
58 protected final void abortIfNeeded() {
59 if (shouldAbort()) {
60 abort();
61 throw new AbortedException();
62 }
63 }
64
65
70 public void abort() {
71 if (in instanceof SdkFilterInputStream) {
72 ((SdkFilterInputStream) in).abort();
73 }
74 aborted = true;
75 }
76
77 protected boolean isAborted() {
78 return aborted;
79 }
80
81 @Override
82 public int read() throws IOException {
83 abortIfNeeded();
84 return in.read();
85 }
86
87 @Override
88 public int read(byte b[], int off, int len) throws IOException {
89 abortIfNeeded();
90 return in.read(b, off, len);
91 }
92
93 @Override
94 public long skip(long n) throws IOException {
95 abortIfNeeded();
96 return in.skip(n);
97 }
98
99 @Override
100 public int available() throws IOException {
101 abortIfNeeded();
102 return in.available();
103 }
104
105 @Override
106 public void close() throws IOException {
107 in.close();
108 abortIfNeeded();
109 }
110
111 @Override
112 public synchronized void mark(int readlimit) {
113 abortIfNeeded();
114 in.mark(readlimit);
115 }
116
117 @Override
118 public synchronized void reset() throws IOException {
119 abortIfNeeded();
120 in.reset();
121 }
122
123 @Override
124 public boolean markSupported() {
125 abortIfNeeded();
126 return in.markSupported();
127 }
128
129 @Override
130 public void release() {
131
132 SdkIOUtils.closeQuietly(this);
133 if (in instanceof Releasable) {
134
135
136 Releasable r = (Releasable)in;
137 r.release();
138 }
139 }
140 }
141