1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
46
46
public abstract class StreamUtils {
47
47
48
48
/**
49
- * The default buffer size used why copying bytes.
49
+ * The default buffer size used when copying bytes.
50
50
*/
51
51
public static final int BUFFER_SIZE = 4096 ;
52
52
@@ -55,7 +55,7 @@ public abstract class StreamUtils {
55
55
56
56
/**
57
57
* Copy the contents of the given InputStream into a new byte array.
58
- * Leaves the stream open when done.
58
+ * <p> Leaves the stream open when done.
59
59
* @param in the stream to copy from (may be {@code null} or empty)
60
60
* @return the new byte array that has been copied to (possibly empty)
61
61
* @throws IOException in case of I/O errors
@@ -72,8 +72,9 @@ public static byte[] copyToByteArray(@Nullable InputStream in) throws IOExceptio
72
72
73
73
/**
74
74
* Copy the contents of the given InputStream into a String.
75
- * Leaves the stream open when done.
75
+ * <p> Leaves the stream open when done.
76
76
* @param in the InputStream to copy from (may be {@code null} or empty)
77
+ * @param charset the {@link Charset} to use to decode the bytes
77
78
* @return the String that has been copied to (possibly empty)
78
79
* @throws IOException in case of I/O errors
79
80
*/
@@ -85,16 +86,16 @@ public static String copyToString(@Nullable InputStream in, Charset charset) thr
85
86
StringBuilder out = new StringBuilder ();
86
87
InputStreamReader reader = new InputStreamReader (in , charset );
87
88
char [] buffer = new char [BUFFER_SIZE ];
88
- int bytesRead = - 1 ;
89
- while ((bytesRead = reader .read (buffer )) != -1 ) {
90
- out .append (buffer , 0 , bytesRead );
89
+ int charsRead ;
90
+ while ((charsRead = reader .read (buffer )) != -1 ) {
91
+ out .append (buffer , 0 , charsRead );
91
92
}
92
93
return out .toString ();
93
94
}
94
95
95
96
/**
96
97
* Copy the contents of the given byte array to the given OutputStream.
97
- * Leaves the stream open when done.
98
+ * <p> Leaves the stream open when done.
98
99
* @param in the byte array to copy from
99
100
* @param out the OutputStream to copy to
100
101
* @throws IOException in case of I/O errors
@@ -107,16 +108,16 @@ public static void copy(byte[] in, OutputStream out) throws IOException {
107
108
}
108
109
109
110
/**
110
- * Copy the contents of the given String to the given output OutputStream.
111
- * Leaves the stream open when done.
111
+ * Copy the contents of the given String to the given OutputStream.
112
+ * <p> Leaves the stream open when done.
112
113
* @param in the String to copy from
113
114
* @param charset the Charset
114
115
* @param out the OutputStream to copy to
115
116
* @throws IOException in case of I/O errors
116
117
*/
117
118
public static void copy (String in , Charset charset , OutputStream out ) throws IOException {
118
119
Assert .notNull (in , "No input String specified" );
119
- Assert .notNull (charset , "No charset specified" );
120
+ Assert .notNull (charset , "No Charset specified" );
120
121
Assert .notNull (out , "No OutputStream specified" );
121
122
122
123
Writer writer = new OutputStreamWriter (out , charset );
@@ -126,7 +127,7 @@ public static void copy(String in, Charset charset, OutputStream out) throws IOE
126
127
127
128
/**
128
129
* Copy the contents of the given InputStream to the given OutputStream.
129
- * Leaves both streams open when done.
130
+ * <p> Leaves both streams open when done.
130
131
* @param in the InputStream to copy from
131
132
* @param out the OutputStream to copy to
132
133
* @return the number of bytes copied
@@ -138,7 +139,7 @@ public static int copy(InputStream in, OutputStream out) throws IOException {
138
139
139
140
int byteCount = 0 ;
140
141
byte [] buffer = new byte [BUFFER_SIZE ];
141
- int bytesRead = - 1 ;
142
+ int bytesRead ;
142
143
while ((bytesRead = in .read (buffer )) != -1 ) {
143
144
out .write (buffer , 0 , bytesRead );
144
145
byteCount += bytesRead ;
@@ -170,7 +171,7 @@ public static long copyRange(InputStream in, OutputStream out, long start, long
170
171
}
171
172
172
173
long bytesToCopy = end - start + 1 ;
173
- byte [] buffer = new byte [StreamUtils .BUFFER_SIZE ];
174
+ byte [] buffer = new byte [( int ) Math . min ( StreamUtils .BUFFER_SIZE , bytesToCopy ) ];
174
175
while (bytesToCopy > 0 ) {
175
176
int bytesRead = in .read (buffer );
176
177
if (bytesRead == -1 ) {
@@ -190,7 +191,7 @@ else if (bytesRead <= bytesToCopy) {
190
191
191
192
/**
192
193
* Drain the remaining content of the given InputStream.
193
- * Leaves the InputStream open when done.
194
+ * <p> Leaves the InputStream open when done.
194
195
* @param in the InputStream to drain
195
196
* @return the number of bytes read
196
197
* @throws IOException in case of I/O errors
0 commit comments