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

box / box-java-sdk / #4098

19 Nov 2024 01:11PM CUT coverage: 71.765% (+0.002%) from 71.763%
#4098

Pull #1273

github

web-flow
Merge f7f9d1a09 into 5eb4c93bd
Pull Request #1273: feat: Tracking the bytes in output stream from representation service…

17 of 24 new or added lines in 2 files covered. (70.83%)

1 existing line in 1 file now uncovered.

8065 of 11238 relevant lines covered (71.77%)

0.72 hits per line

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

72.73
/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
     * @param listener Listener that will be notified on writing response. Can be null.
55
     */
56

57
    static void writeStreamWithContentLength(BoxAPIResponse response, OutputStream output, ProgressListener listener) {
58
        try {
59
            InputStream input;
60
            if (listener != null) {
1✔
NEW
61
                input = response.getBody(listener);
×
62
            } else {
63
                input = response.getBody();
1✔
64
            }
65
            writeStreamTo(input, output, response.getContentLength());
1✔
66
        } finally {
67
            response.close();
1✔
68
        }
69
    }
1✔
70

71
    /**
72
     * Writes content of input stream to provided output. Method is NOT closing input stream.
73
     *
74
     * @param input  Input that will be read.
75
     * @param output Output stream.
76
     */
77
    static void writeStreamTo(InputStream input, OutputStream output) {
78
        try {
79
            byte[] buffer = new byte[BUFFER_SIZE];
1✔
80
            int n = input.read(buffer);
1✔
81
            while (n != -1) {
1✔
82
                output.write(buffer, 0, n);
1✔
83
                n = input.read(buffer);
1✔
84
            }
85
        } catch (IOException e) {
×
86
            throw new RuntimeException(e);
×
87
        } finally {
88
            try {
89
                input.close();
1✔
90
                output.close();
1✔
91
            } catch (IOException e) {
×
92
                throw new RuntimeException(e);
×
93
            }
1✔
94
        }
95
    }
1✔
96

97
    static void writeStreamTo(InputStream input, OutputStream output, long expectedLength) {
98
        long totalBytesRead = 0;
1✔
99
        if (expectedLength < 0) {
1✔
NEW
100
            throw new RuntimeException("No Data bytes in stream");
×
101
        }
102
        try {
103
            byte[] buffer = new byte[8192];
1✔
104
            for (int n = input.read(buffer); n != -1; n = input.read(buffer)) {
1✔
105
                output.write(buffer, 0, n);
1✔
106
                totalBytesRead += n;  // Track the total bytes read
1✔
107
            }
108
            if (totalBytesRead != expectedLength) {
1✔
NEW
109
                throw new IOException("Stream ended prematurely. Expected " + expectedLength
×
110
                        + " bytes, but read " + totalBytesRead + " bytes.");
111
            }
NEW
112
        } catch (IOException e) {
×
NEW
113
            throw new RuntimeException("Error during streaming: " + e.getMessage(), e);
×
114
        } finally {
115
            try {
116
                input.close();
1✔
117
                output.close();
1✔
NEW
118
            } catch (IOException closeException) {
×
NEW
119
                throw new RuntimeException("IOException during stream close", closeException);
×
120
            }
1✔
121
        }
122
    }
1✔
123
}
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

© 2025 Coveralls, Inc