@@ -47,7 +47,40 @@ static void writeStream(BoxAPIResponse response, OutputStream output, ProgressLi
4747 }
4848
4949 /**
50- * Writes content of input stream to provided output. Method is NOT closing input stream.
50+ * Writes response body bytes to output stream. After all closes the input stream.
51+ *
52+ * @param response Response that is going to be written.
53+ * @param output Output stream.
54+ */
55+
56+ static void writeStreamWithContentLength (BoxAPIResponse response , OutputStream output ) {
57+ writeStreamWithContentLength (response , output , null );
58+ }
59+
60+ /**
61+ * Writes response body bytes to output stream. After all closes the input stream.
62+ *
63+ * @param response Response that is going to be written.
64+ * @param output Output stream.
65+ * @param listener Listener that will be notified on writing response. Can be null.
66+ */
67+
68+ static void writeStreamWithContentLength (BoxAPIResponse response , OutputStream output , ProgressListener listener ) {
69+ try {
70+ InputStream input ;
71+ if (listener != null ) {
72+ input = response .getBody (listener );
73+ } else {
74+ input = response .getBody ();
75+ }
76+ writeStreamTo (input , output , response .getContentLength ());
77+ } finally {
78+ response .close ();
79+ }
80+ }
81+
82+ /**
83+ * Writes content of input stream to provided output.
5184 *
5285 * @param input Input that will be read.
5386 * @param output Output stream.
@@ -71,4 +104,40 @@ static void writeStreamTo(InputStream input, OutputStream output) {
71104 }
72105 }
73106 }
107+
108+ /**
109+ * Writes the content of the input stream to the provided output stream, ensuring the exact number of bytes specified
110+ * by the expected length is written. If the stream ends prematurely an exception is thrown.
111+ *
112+ * @param input The input stream to be read.
113+ * @param output The output stream where data will be written.
114+ * @param expectedLength The expected number of bytes to be transferred.
115+ */
116+
117+ static void writeStreamTo (InputStream input , OutputStream output , long expectedLength ) {
118+ long totalBytesRead = 0 ;
119+ if (expectedLength < 0 ) {
120+ throw new RuntimeException ("Expected content length should not be negative: " + expectedLength );
121+ }
122+ try {
123+ byte [] buffer = new byte [BUFFER_SIZE ];
124+ for (int n = input .read (buffer ); n != -1 ; n = input .read (buffer )) {
125+ output .write (buffer , 0 , n );
126+ totalBytesRead += n ; // Track the total bytes read
127+ }
128+ if (totalBytesRead != expectedLength ) {
129+ throw new IOException ("Stream ended prematurely. Expected " + expectedLength
130+ + " bytes, but read " + totalBytesRead + " bytes." );
131+ }
132+ } catch (IOException e ) {
133+ throw new RuntimeException ("Error during streaming: " + e .getMessage (), e );
134+ } finally {
135+ try {
136+ input .close ();
137+ output .close ();
138+ } catch (IOException closeException ) {
139+ throw new RuntimeException ("IOException during stream close" , closeException );
140+ }
141+ }
142+ }
74143}
0 commit comments