11package com .box .sdk ;
22
3+ import static java .lang .String .format ;
4+
35import com .box .sdk .http .HttpHeaders ;
46import com .box .sdk .http .HttpMethod ;
57import com .eclipsesource .json .Json ;
@@ -44,6 +46,7 @@ public class BoxAPIRequest {
4446 private static final BoxLogger LOGGER = BoxLogger .defaultLogger ();
4547 private static final int MAX_REDIRECTS = 3 ;
4648 private static final String ERROR_CREATING_REQUEST_BODY = "Error creating request body" ;
49+ private static final int BUFFER_SIZE = 8192 ;
4750 private static SSLSocketFactory sslSocketFactory ;
4851
4952 static {
@@ -439,7 +442,7 @@ public BoxAPIResponse send(ProgressListener listener) {
439442 }
440443
441444 LOGGER .warn (
442- String . format ("Retrying request due to transient error status=%d body=%s" ,
445+ format ("Retrying request due to transient error status=%d body=%s" ,
443446 apiException .getResponseCode (),
444447 apiException .getResponse ())
445448 );
@@ -510,7 +513,7 @@ BoxFileUploadSessionPart sendForUploadPart(BoxFileUploadSession session, long of
510513 } catch (BoxAPIException e ) {
511514 }
512515 }
513- LOGGER .warn (String . format (
516+ LOGGER .warn (format (
514517 "Retrying request due to transient error status=%d body=%s" ,
515518 apiException .getResponseCode (),
516519 apiException .getResponse ()
@@ -618,17 +621,22 @@ protected void writeBody(HttpURLConnection connection, ProgressListener listener
618621 if (listener != null ) {
619622 output = new ProgressOutputStream (output , listener , this .bodyLength );
620623 }
621- int b = this .body .read ();
622- while (b != -1 ) {
623- output .write (b );
624- b = this .body .read ();
625- }
624+ writeWithBuffer (output );
626625 output .close ();
627626 } catch (IOException e ) {
628627 throw new BoxAPIException (ERROR_CREATING_REQUEST_BODY , e );
629628 }
630629 }
631630
631+ private void writeWithBuffer (OutputStream output ) throws IOException {
632+ byte [] buffer = new byte [BUFFER_SIZE ];
633+ int b = this .body .read (buffer );
634+ while (b != -1 ) {
635+ output .write (buffer , 0 , b );
636+ b = this .body .read (buffer );
637+ }
638+ }
639+
632640 /**
633641 * Resets the InputStream containing this request's body.
634642 *
@@ -702,11 +710,14 @@ private BoxAPIResponse trySend(ProgressListener listener) {
702710
703711 int responseCode ;
704712 try {
713+ long writeStart = System .currentTimeMillis ();
705714 this .writeBody (connection , listener );
706-
715+ logDebug ( format ( "[trySend] Body write took %dms%n" , ( System . currentTimeMillis () - writeStart )));
707716 // Ensure that we're connected in case writeBody() didn't write anything.
708717 try {
718+ long start = System .currentTimeMillis ();
709719 connection .connect ();
720+ logDebug (format ("[trySend] connection.connect() took %dms%n" , (System .currentTimeMillis () - start )));
710721 } catch (IOException e ) {
711722 throw new BoxAPIException ("Couldn't connect to the Box API due to a network error." , e );
712723 }
@@ -717,7 +728,11 @@ private BoxAPIResponse trySend(ProgressListener listener) {
717728 // happens correctly. There seems to be a bug in Oracle's Java implementation where automatically handled
718729 // redirects will not keep the connection alive.
719730 try {
731+ long getResponseStart = System .currentTimeMillis ();
720732 responseCode = connection .getResponseCode ();
733+ logDebug (format (
734+ "[trySend] Get Response (read network) took %dms%n" , System .currentTimeMillis () - getResponseStart
735+ ));
721736 } catch (IOException e ) {
722737 throw new BoxAPIException ("Couldn't connect to the Box API due to a network error." , e );
723738 }
@@ -780,12 +795,16 @@ private BoxAPIResponse handleRedirect(HttpURLConnection connection, ProgressList
780795 }
781796 }
782797
783- private void logRequest ( ) {
798+ private void logDebug ( String message ) {
784799 if (LOGGER .isDebugEnabled ()) {
785- LOGGER .debug (this . toString () );
800+ LOGGER .debug (message );
786801 }
787802 }
788803
804+ private void logRequest () {
805+ logDebug (this .toString ());
806+ }
807+
789808 private HttpURLConnection createConnection () {
790809 HttpURLConnection connection ;
791810
0 commit comments