1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.jcr;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import net.jcip.annotations.NotThreadSafe;
29 import org.modeshape.graph.property.Binary;
30
31
32
33
34
35
36
37
38
39
40
41
42 @NotThreadSafe
43 class SelfClosingInputStream extends InputStream {
44
45 private final Binary binary;
46 private final InputStream stream;
47
48
49
50
51
52
53
54
55 public SelfClosingInputStream( Binary binary ) {
56 assert binary != null;
57 this.binary = binary;
58 this.binary.acquire();
59 this.stream = binary.getStream();
60 }
61
62
63
64
65
66
67 @Override
68 public int available() throws IOException {
69 try {
70 return stream.available();
71 } catch (IOException e) {
72 this.binary.release();
73 throw e;
74 } catch (RuntimeException e) {
75 this.binary.release();
76 throw e;
77 }
78 }
79
80
81
82
83
84
85 @Override
86 public void close() throws IOException {
87 try {
88 stream.close();
89 } finally {
90 this.binary.release();
91 }
92 }
93
94
95
96
97
98
99 @Override
100 public boolean equals( Object obj ) {
101 return stream.equals(obj);
102 }
103
104
105
106
107
108
109 @Override
110 public int hashCode() {
111 return stream.hashCode();
112 }
113
114
115
116
117
118
119 @Override
120 public void mark( int readlimit ) {
121 try {
122 stream.mark(readlimit);
123 } catch (RuntimeException e) {
124 this.binary.release();
125 throw e;
126 }
127 }
128
129
130
131
132
133
134 @Override
135 public boolean markSupported() {
136 return stream.markSupported();
137 }
138
139
140
141
142
143
144 @Override
145 public int read( byte[] b,
146 int off,
147 int len ) throws IOException {
148 try {
149 int result = stream.read(b, off, len);
150 if (result == -1) {
151
152 this.binary.release();
153 }
154 return result;
155 } catch (IOException e) {
156 this.binary.release();
157 throw e;
158 } catch (RuntimeException e) {
159 this.binary.release();
160 throw e;
161 }
162 }
163
164
165
166
167
168
169 @Override
170 public int read( byte[] b ) throws IOException {
171 try {
172 int result = stream.read(b);
173 if (result == -1) {
174
175 this.binary.release();
176 }
177 return result;
178 } catch (IOException e) {
179 this.binary.release();
180 throw e;
181 } catch (RuntimeException e) {
182 this.binary.release();
183 throw e;
184 }
185 }
186
187
188
189
190
191
192 @Override
193 public int read() throws IOException {
194 try {
195 int result = stream.read();
196 if (result == -1) {
197
198 this.binary.release();
199 }
200 return result;
201 } catch (IOException e) {
202 this.binary.release();
203 throw e;
204 } catch (RuntimeException e) {
205 this.binary.release();
206 throw e;
207 }
208 }
209
210
211
212
213
214
215 @Override
216 public void reset() throws IOException {
217 try {
218 stream.reset();
219 } catch (IOException e) {
220 this.binary.release();
221 throw e;
222 } catch (RuntimeException e) {
223 this.binary.release();
224 throw e;
225 }
226 }
227
228
229
230
231
232
233 @Override
234 public long skip( long n ) throws IOException {
235 try {
236 return stream.skip(n);
237 } catch (IOException e) {
238 this.binary.release();
239 throw e;
240 } catch (RuntimeException e) {
241 this.binary.release();
242 throw e;
243 }
244 }
245
246
247
248
249
250
251 @Override
252 public String toString() {
253 try {
254 return stream.toString();
255 } catch (RuntimeException e) {
256 this.binary.release();
257 throw e;
258 }
259 }
260 }