1212 */
1313package com .ibm .watson .developer_cloud .http ;
1414
15+ import java .io .ByteArrayInputStream ;
16+ import java .io .ByteArrayOutputStream ;
1517import java .io .IOException ;
1618import java .io .InputStream ;
1719
20+ import com .ibm .watson .developer_cloud .util .HttpLogging ;
1821import okhttp3 .MediaType ;
1922import okhttp3 .RequestBody ;
2023import okhttp3 .internal .Util ;
24+ import okhttp3 .logging .HttpLoggingInterceptor ;
2125import okio .BufferedSink ;
2226import okio .Okio ;
2327import okio .Source ;
28+ import org .apache .commons .io .IOUtils ;
2429
2530/**
2631 * RequestBody that takes an {@link InputStream}.
@@ -30,6 +35,7 @@ public class InputStreamRequestBody extends RequestBody {
3035
3136 private InputStream inputStream ;
3237 private MediaType mediaType ;
38+ private byte [] bytes ;
3339
3440 /**
3541 * Creates the @link {@link RequestBody} from an @link {@link InputStream}.
@@ -45,6 +51,19 @@ public static RequestBody create(final MediaType mediaType, final InputStream in
4551 private InputStreamRequestBody (InputStream inputStream , MediaType mediaType ) {
4652 this .inputStream = inputStream ;
4753 this .mediaType = mediaType ;
54+
55+ // if we're logging everything, we'll need to store the bytes to reuse later
56+ if (HttpLogging .getLoggingInterceptor ().getLevel ().equals (HttpLoggingInterceptor .Level .BODY )) {
57+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
58+
59+ try {
60+ IOUtils .copy (inputStream , outputStream );
61+ } catch (IOException e ) {
62+ e .printStackTrace ();
63+ }
64+
65+ this .bytes = outputStream .toByteArray ();
66+ }
4867 }
4968
5069 /*
@@ -65,8 +84,13 @@ public MediaType contentType() {
6584 @ Override
6685 public void writeTo (BufferedSink sink ) throws IOException {
6786 Source source = null ;
87+
6888 try {
69- source = Okio .source (inputStream );
89+ if (bytes != null ) {
90+ source = Okio .source (new ByteArrayInputStream (bytes ));
91+ } else {
92+ source = Okio .source (inputStream );
93+ }
7094 sink .writeAll (source );
7195 } finally {
7296 Util .closeQuietly (source );
0 commit comments