11/**
22 * Copyright Microsoft Corporation
3- *
3+ *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 * http://www.apache.org/licenses/LICENSE-2.0
8- *
8+ *
99 * Unless required by applicable law or agreed to in writing, software
1010 * distributed under the License is distributed on an "AS IS" BASIS,
1111 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1717
1818import java .io .IOException ;
1919import java .io .InputStream ;
20+ import java .io .OutputStream ;
2021import java .net .HttpURLConnection ;
2122import java .util .Date ;
2223import java .util .Map .Entry ;
3031public final class ExecutionEngine {
3132 /**
3233 * Executes an operation and enforces a retrypolicy to handle any potential errors
33- *
34+ *
3435 * @param <CLIENT_TYPE>
3536 * The type of the service client
3637 * @param <PARENT_TYPE>
@@ -72,6 +73,7 @@ public static <CLIENT_TYPE, PARENT_TYPE, RESULT_TYPE> RESULT_TYPE executeWithRet
7273 int currentRetryCount = 0 ;
7374 StorageException translatedException = null ;
7475 HttpURLConnection request = null ;
76+ OutputStream requestOutputStream = null ;
7577 final long startTime = new Date ().getTime ();
7678
7779 while (true ) {
@@ -87,8 +89,9 @@ public static <CLIENT_TYPE, PARENT_TYPE, RESULT_TYPE> RESULT_TYPE executeWithRet
8789 try {
8890 if (task .getSendStream () != null ) {
8991 Logger .info (opContext , LogConstants .UPLOAD );
92+ requestOutputStream = request .getOutputStream ();
9093 final StreamMd5AndLength descriptor = Utility .writeToOutputStream (task .getSendStream (),
91- request . getOutputStream () , task .getLength (), false /* rewindStream */ ,
94+ requestOutputStream , task .getLength (), false /* rewindStream */ ,
9295 false /* calculate MD5 */ , opContext , task .getRequestOptions ());
9396
9497 task .validateStreamWrite (descriptor );
@@ -131,6 +134,12 @@ public static <CLIENT_TYPE, PARENT_TYPE, RESULT_TYPE> RESULT_TYPE executeWithRet
131134
132135 ExecutionEngine .fireErrorReceivingResponseEvent (opContext , request , task .getResult ());
133136 }
137+ if (requestOutputStream != null ) {
138+ try {
139+ requestOutputStream .close ();
140+ } catch (IOException logOrIgnore ) {
141+ }
142+ }
134143 }
135144
136145 // 8. Pre-process response to check if there was an exception. Do Response parsing (headers etc).
@@ -196,7 +205,7 @@ public static <CLIENT_TYPE, PARENT_TYPE, RESULT_TYPE> RESULT_TYPE executeWithRet
196205 task .getResult ().setStatusCode (e .getHttpStatusCode ());
197206 task .getResult ().setStatusMessage (e .getMessage ());
198207 task .getResult ().setException (e );
199-
208+
200209 Logger .warn (opContext , LogConstants .RETRYABLE_EXCEPTION , e .getClass ().getName (), e .getMessage ());
201210 translatedException = e ;
202211 }
0 commit comments