• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

box / box-java-sdk / #4144

21 Nov 2024 12:29PM UTC coverage: 71.77% (+0.007%) from 71.763%
#4144

Pull #1273

github

web-flow
Merge 0f199548c into 50f5a6118
Pull Request #1273: feat: Track bytes in rep service output stream

19 of 26 new or added lines in 2 files covered. (73.08%)

1 existing line in 1 file now uncovered.

8067 of 11240 relevant lines covered (71.77%)

0.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.91
/src/main/java/com/box/sdk/BinaryBodyUtils.java
1
package com.box.sdk;
2

3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.OutputStream;
6

7
/**
8
 * Utility class to help writing binary data to output stream.
9
 */
10
final class BinaryBodyUtils {
11
    private static final int BUFFER_SIZE = 8192;
12

13
    private BinaryBodyUtils() {
14
        // utility class has no public constructor
15
    }
16

17
    /**
18
     * Writes response body bytes to output stream. After all closes the input stream.
19
     *
20
     * @param response Response that is going to be written.
21
     * @param output   Output stream.
22
     */
23
    static void writeStream(BoxAPIResponse response, OutputStream output) {
24
        writeStream(response, output, null);
1✔
25
    }
1✔
26

27
    /**
28
     * Writes response body bytes to output stream. After all closes the input stream.
29
     *
30
     * @param response Response that is going to be written.
31
     * @param output   Output stream.
32
     * @param listener Listener that will be notified on writing response. Can be null.
33
     */
34

35
    static void writeStream(BoxAPIResponse response, OutputStream output, ProgressListener listener) {
36
        try {
37
            InputStream input;
38
            if (listener != null) {
1✔
39
                input = response.getBody(listener);
×
40
            } else {
41
                input = response.getBody();
1✔
42
            }
43
            writeStreamTo(input, output);
1✔
44
        } finally {
45
            response.close();
1✔
46
        }
47
    }
1✔
48

49
    /**
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);
1✔
58
    }
1✔
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) {
1✔
NEW
72
                input = response.getBody(listener);
×
73
            } else {
74
                input = response.getBody();
1✔
75
            }
76
            writeStreamTo(input, output, response.getContentLength());
1✔
77
        } finally {
78
            response.close();
1✔
79
        }
80
    }
1✔
81

82
    /**
83
     * Writes content of input stream to provided output.
84
     *
85
     * @param input  Input that will be read.
86
     * @param output Output stream.
87
     */
88
    static void writeStreamTo(InputStream input, OutputStream output) {
89
        try {
90
            byte[] buffer = new byte[BUFFER_SIZE];
1✔
91
            int n = input.read(buffer);
1✔
92
            while (n != -1) {
1✔
93
                output.write(buffer, 0, n);
1✔
94
                n = input.read(buffer);
1✔
95
            }
96
        } catch (IOException e) {
×
97
            throw new RuntimeException(e);
×
98
        } finally {
99
            try {
100
                input.close();
1✔
101
                output.close();
1✔
102
            } catch (IOException e) {
×
103
                throw new RuntimeException(e);
×
104
            }
1✔
105
        }
106
    }
1✔
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;
1✔
119
        if (expectedLength < 0) {
1✔
NEW
120
            throw new RuntimeException("Expected content length should not be negative: " + expectedLength);
×
121
        }
122
        try {
123
            byte[] buffer = new byte[BUFFER_SIZE];
1✔
124
            for (int n = input.read(buffer); n != -1; n = input.read(buffer)) {
1✔
125
                output.write(buffer, 0, n);
1✔
126
                totalBytesRead += n;  // Track the total bytes read
1✔
127
            }
128
            if (totalBytesRead != expectedLength) {
1✔
NEW
129
                throw new IOException("Stream ended prematurely. Expected " + expectedLength
×
130
                        + " bytes, but read " + totalBytesRead + " bytes.");
131
            }
NEW
132
        } catch (IOException e) {
×
NEW
133
            throw new RuntimeException("Error during streaming: " + e.getMessage(), e);
×
134
        } finally {
135
            try {
136
                input.close();
1✔
137
                output.close();
1✔
NEW
138
            } catch (IOException closeException) {
×
NEW
139
                throw new RuntimeException("IOException during stream close", closeException);
×
140
            }
1✔
141
        }
142
    }
1✔
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc