4747 */
4848class OperationAttachmentsProxy implements OperationAttachments {
4949
50- final List <ProxiedInputStream > proxiedStreams ;
50+ private final List <ProxiedInputStream > proxiedStreams ;
5151
52- public OperationAttachmentsProxy (final ManagementChannelAssociation channelAssociation , final int batchId , final int size ) {
52+ private OperationAttachmentsProxy (final ManagementChannelAssociation channelAssociation , final int batchId , final int size ) {
5353 proxiedStreams = new ArrayList <ProxiedInputStream >(size );
54- for (int i = 0 ; i < size ; i ++) {
54+ for (int i = 0 ; i < size ; i ++) {
5555 proxiedStreams .add (new ProxiedInputStream (channelAssociation , batchId , i ));
5656 }
5757 }
@@ -83,43 +83,60 @@ void shutdown(Exception error) {
8383 }
8484 }
8585
86- public static class ProxiedInputStream extends InputStream {
86+ private static class ProxiedInputStream extends InputStream {
8787 static final int BUFFER_SIZE = 8192 ;
8888
8989 private final int index ;
9090 private final int batchId ;
91- private volatile Pipe pipe = new Pipe ( BUFFER_SIZE ) ;
91+ private final Pipe pipe ;
9292 private final ManagementChannelAssociation channelAssociation ;
9393
94- private volatile boolean initialized ;
94+ private boolean initialized ;
9595 private volatile Exception error ;
9696
9797 ProxiedInputStream (final ManagementChannelAssociation channelAssociation , final int batchId , final int index ) {
9898 this .channelAssociation = channelAssociation ;
9999 this .batchId = batchId ;
100100 this .index = index ;
101+ pipe = new Pipe (BUFFER_SIZE );
101102 }
102103
103-
104104 @ Override
105105 public int read () throws IOException {
106- if (! initialized && error == null ) {
107- synchronized (this ) {
108- if (!initialized ) {
109- initializeBytes ();
110- }
111- }
106+ prepareForRead ();
107+ return pipe .getIn ().read ();
108+ }
109+
110+ @ Override
111+ public int read (byte [] b , int off , int len ) throws IOException {
112+ prepareForRead ();
113+ return pipe .getIn ().read (b , off , len );
114+ }
115+
116+ @ Override
117+ public void close () throws IOException {
118+ IOException ex = null ;
119+ try {
120+ pipe .getOut ().close ();
121+ } catch (IOException e ) {
122+ ex = e ;
112123 }
113- if (error != null ) {
114- if (error instanceof IOException ) {
115- throw (IOException )error ;
116- }
117- throw new IOException (error );
124+ try {
125+ pipe .getIn ().close ();
126+ } catch (IOException e ) {
127+ ex = e ;
128+ }
129+ if (ex != null ) {
130+ throw ex ;
118131 }
119- return pipe .getIn ().read ();
120132 }
121133
122- void initializeBytes () {
134+ private void prepareForRead () throws IOException {
135+ initializeBytes ();
136+ throwIfError ();
137+ }
138+
139+ private void initializeBytes () {
123140 if (!initialized ) {
124141 initialized = true ;
125142 try {
@@ -147,13 +164,13 @@ public void handleRequest(DataInput input, ActiveOperation.ResultHandler<Object>
147164 final byte [] buffer = new byte [BUFFER_SIZE ];
148165 int totalRead = 0 ;
149166 while (totalRead < size ) {
150- int len = Math .min (( int ) ( size - totalRead ) , buffer .length );
167+ int len = Math .min (size - totalRead , buffer .length );
151168 input .readFully (buffer , 0 , len );
152169 os .write (buffer , 0 , len );
153170 totalRead += len ;
154171 }
155172 os .close ();
156- } catch (IOException e ) {
173+ } catch (IOException e ) {
157174 shutdown (e );
158175 throw e ;
159176 }
@@ -165,27 +182,18 @@ public void handleRequest(DataInput input, ActiveOperation.ResultHandler<Object>
165182 }
166183 }
167184
168- void shutdown (Exception error ) {
169- StreamUtils .safeClose (this );
170- this .error = error ;
185+ private void throwIfError () throws IOException {
186+ if (error != null ) {
187+ if (error instanceof IOException ) {
188+ throw (IOException ) error ;
189+ }
190+ throw new IOException (error );
191+ }
171192 }
172193
173- @ Override
174- public void close () throws IOException {
175- IOException ex = null ;
176- try {
177- pipe .getOut ().close ();
178- } catch (IOException e ) {
179- ex = e ;
180- }
181- try {
182- pipe .getIn ().close ();
183- } catch (IOException e ) {
184- ex = e ;
185- }
186- if (ex != null ) {
187- throw ex ;
188- }
194+ private void shutdown (Exception error ) {
195+ StreamUtils .safeClose (this );
196+ this .error = error ;
189197 }
190198 }
191199}
0 commit comments