@@ -148,38 +148,41 @@ public static void returnData(Object context, final Intent intent, final ResultW
148148 final Activity activity = (Activity ) ((context instanceof Activity ) ? context : null );
149149
150150 final Runnable runnable = () -> {
151+ PrintWriter writer = null ;
152+ LocalSocket outputSocket = null ;
151153 try {
152154 final ParcelFileDescriptor [] pfds = { null };
153- try ( LocalSocket outputSocket = new LocalSocket ()) {
154- String outputSocketAdress = intent .getStringExtra (SOCKET_OUTPUT_EXTRA );
155- outputSocket . connect ( new LocalSocketAddress ( outputSocketAdress ) );
156- try ( PrintWriter writer = new PrintWriter ( outputSocket . getOutputStream ())) {
157- if ( resultWriter != null ) {
158- if ( resultWriter instanceof BinaryOutput ) {
159- BinaryOutput bout = ( BinaryOutput ) resultWriter ;
160- bout . setOutput ( outputSocket . getOutputStream ());
161- }
162- if ( resultWriter instanceof WithInput ) {
163- try ( LocalSocket inputSocket = new LocalSocket ()) {
164- String inputSocketAdress = intent . getStringExtra ( SOCKET_INPUT_EXTRA );
165- inputSocket . connect ( new LocalSocketAddress ( inputSocketAdress ));
166- (( WithInput ) resultWriter ). setInput ( inputSocket . getInputStream () );
167- resultWriter . writeResult ( writer );
168- }
169- } else {
170- resultWriter . writeResult ( writer );
171- }
172- if ( resultWriter instanceof WithAncillaryFd ) {
173- int fd = (( WithAncillaryFd ) resultWriter ). getFd ();
174- if ( fd >= 0 ) {
175- pfds [ 0 ] = ParcelFileDescriptor . adoptFd ( fd );
176- FileDescriptor [] fds = { pfds [ 0 ]. getFileDescriptor () };
177- outputSocket . setFileDescriptorsForSend ( fds );
178- }
179- }
155+ outputSocket = new LocalSocket ();
156+ String outputSocketAdress = intent .getStringExtra (SOCKET_OUTPUT_EXTRA );
157+ Logger . logDebug ( LOG_TAG , "Connecting to output socket \" " + outputSocketAdress + " \" " );
158+ outputSocket . connect ( new LocalSocketAddress ( outputSocketAdress ));
159+ writer = new PrintWriter ( outputSocket . getOutputStream ());
160+
161+ if ( resultWriter != null ) {
162+ if ( resultWriter instanceof BinaryOutput ) {
163+ BinaryOutput bout = ( BinaryOutput ) resultWriter ;
164+ bout . setOutput ( outputSocket . getOutputStream ());
165+ }
166+ if ( resultWriter instanceof WithInput ) {
167+ try ( LocalSocket inputSocket = new LocalSocket ()) {
168+ String inputSocketAdress = intent . getStringExtra ( SOCKET_INPUT_EXTRA );
169+ inputSocket . connect ( new LocalSocketAddress ( inputSocketAdress ) );
170+ (( WithInput ) resultWriter ). setInput ( inputSocket . getInputStream ());
171+ resultWriter . writeResult ( writer );
172+ }
173+ } else {
174+ resultWriter . writeResult ( writer );
175+ }
176+ if ( resultWriter instanceof WithAncillaryFd ) {
177+ int fd = (( WithAncillaryFd ) resultWriter ). getFd ( );
178+ if ( fd >= 0 ) {
179+ pfds [ 0 ] = ParcelFileDescriptor . adoptFd ( fd );
180+ FileDescriptor [] fds = { pfds [ 0 ]. getFileDescriptor () };
181+ outputSocket . setFileDescriptorsForSend ( fds );
180182 }
181183 }
182184 }
185+
183186 if (pfds [0 ] != null ) {
184187 pfds [0 ].close ();
185188 }
@@ -202,14 +205,23 @@ public static void returnData(Object context, final Intent intent, final ResultW
202205 activity .setResult (1 );
203206 }
204207 } finally {
208+ try {
209+ if (writer != null )
210+ writer .close ();
211+ if (outputSocket != null )
212+ outputSocket .close ();
213+ } catch (Exception e ) {
214+ Logger .logStackTraceWithMessage (LOG_TAG , "Failed to close" , e );
215+ }
216+
205217 try {
206218 if (asyncResult != null ) {
207219 asyncResult .finish ();
208220 } else if (activity != null ) {
209221 activity .finish ();
210222 }
211223 } catch (Exception e ) {
212- Logger .logStackTraceWithMessage (LOG_TAG , "Failed to cleanup " , e );
224+ Logger .logStackTraceWithMessage (LOG_TAG , "Failed to finish " , e );
213225 }
214226 }
215227 };
0 commit comments