1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 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.
33
33
* its sibling {@link ResizableByteArrayOutputStream}.
34
34
*
35
35
* <p>Unlike {@link java.io.ByteArrayOutputStream}, this implementation is backed
36
- * by a {@link java.util.ArrayDeque} of {@code byte[]} instead of 1 constantly
37
- * resizing {@code byte[]}. It does not copy buffers when it gets expanded.
36
+ * by a {@link java.util.ArrayDeque} of {@code byte[]} buffers instead of one
37
+ * constantly resizing {@code byte[]}. It does not copy buffers when it gets expanded.
38
38
*
39
39
* <p>The initial buffer is only created when the stream is first written.
40
- * There is also no copying of the internal buffer if its content is extracted
41
- * with the {@link #writeTo(OutputStream)} method.
40
+ * There is also no copying of the internal buffers if the stream's content is
41
+ * extracted via the {@link #writeTo(OutputStream)} method.
42
42
*
43
43
* @author Craig Andrews
44
44
* @author Juergen Hoeller
@@ -72,16 +72,16 @@ public class FastByteArrayOutputStream extends OutputStream {
72
72
73
73
74
74
/**
75
- * Create a new < code> FastByteArrayOutputStream</code>
76
- * with the default initial capacity of 256 bytes.
75
+ * Create a new {@ code FastByteArrayOutputStream} with the default initial
76
+ * capacity of 256 bytes.
77
77
*/
78
78
public FastByteArrayOutputStream () {
79
79
this (DEFAULT_BLOCK_SIZE );
80
80
}
81
81
82
82
/**
83
- * Create a new < code> FastByteArrayOutputStream</code>
84
- * with the specified initial capacity.
83
+ * Create a new {@ code FastByteArrayOutputStream} with the specified initial
84
+ * capacity.
85
85
* @param initialBlockSize the initial buffer size in bytes
86
86
*/
87
87
public FastByteArrayOutputStream (int initialBlockSize ) {
@@ -150,59 +150,60 @@ public void close() {
150
150
}
151
151
152
152
/**
153
- * Convert the buffer 's contents into a string decoding bytes using the
153
+ * Convert this stream 's contents to a string by decoding the bytes using the
154
154
* platform's default character set. The length of the new {@code String}
155
155
* is a function of the character set, and hence may not be equal to the
156
- * size of the buffer .
156
+ * size of the buffers .
157
157
* <p>This method always replaces malformed-input and unmappable-character
158
158
* sequences with the default replacement string for the platform's
159
159
* default character set. The {@linkplain java.nio.charset.CharsetDecoder}
160
160
* class should be used when more control over the decoding process is
161
161
* required.
162
- * @return a String decoded from the buffer's contents
162
+ * @return a String decoded from this stream's contents
163
+ * @see #toString(Charset)
163
164
*/
164
165
@ Override
165
166
public String toString () {
166
167
return toString (Charset .defaultCharset ());
167
168
}
168
169
169
170
/**
170
- * Converts the buffer 's contents into a string by decoding the bytes using
171
- * the specified {@link java.nio.charset. Charset charset }.
172
- *
173
- * @param charset the {@linkplain java.nio.charset.Charset charset}
174
- * to be used to decode the {@code bytes}
175
- * @return a String decoded from the buffer's contents
171
+ * Convert this stream 's contents to a string by decoding the bytes using the
172
+ * specified {@link Charset}.
173
+ * @param charset the {@link Charset} to use to decode the bytes
174
+ * @return a String decoded from this stream's contents
175
+ * @since 6.1.2
176
+ * @see #toString()
176
177
*/
177
178
public String toString (Charset charset ) {
178
179
if (size () == 0 ) {
179
180
return "" ;
180
181
}
181
- if (buffers .size () == 1 ) {
182
- return new String (buffers .getFirst (), 0 , index , charset );
182
+ if (this . buffers .size () == 1 ) {
183
+ return new String (this . buffers .getFirst (), 0 , this . index , charset );
183
184
}
184
185
return new String (toByteArrayUnsafe (), charset );
185
186
}
186
187
187
188
// Custom methods
188
189
189
190
/**
190
- * Return the number of bytes stored in this < code> FastByteArrayOutputStream</code> .
191
+ * Return the number of bytes stored in this {@ code FastByteArrayOutputStream} .
191
192
*/
192
193
public int size () {
193
194
return (this .alreadyBufferedSize + this .index );
194
195
}
195
196
196
197
/**
197
- * Convert the stream's data to a byte array and return the byte array.
198
+ * Convert this stream's contents to a byte array and return the byte array.
198
199
* <p>Also replaces the internal structures with the byte array to
199
200
* conserve memory: if the byte array is being created anyway, we might
200
201
* as well as use it. This approach also means that if this method is
201
202
* called twice without any writes in the interim, the second call is
202
203
* a no-op.
203
204
* <p>This method is "unsafe" as it returns the internal buffer.
204
205
* Callers should not modify the returned buffer.
205
- * @return the current contents of this output stream, as a byte array.
206
+ * @return the current contents of this stream as a byte array
206
207
* @see #size()
207
208
* @see #toByteArray()
208
209
*/
@@ -218,8 +219,8 @@ public byte[] toByteArrayUnsafe() {
218
219
/**
219
220
* Create a newly allocated byte array.
220
221
* <p>Its size is the current size of this output stream, and it will
221
- * contain the valid contents of the internal buffer .
222
- * @return the current contents of this output stream, as a byte array
222
+ * contain the valid contents of the internal buffers .
223
+ * @return the current contents of this stream as a byte array
223
224
* @see #size()
224
225
* @see #toByteArrayUnsafe()
225
226
*/
@@ -229,7 +230,7 @@ public byte[] toByteArray() {
229
230
}
230
231
231
232
/**
232
- * Reset the contents of this < code> FastByteArrayOutputStream</code> .
233
+ * Reset the contents of this {@ code FastByteArrayOutputStream} .
233
234
* <p>All currently accumulated output in the output stream is discarded.
234
235
* The output stream can be used again.
235
236
*/
@@ -242,19 +243,21 @@ public void reset() {
242
243
}
243
244
244
245
/**
245
- * Get an {@link InputStream} to retrieve the data in this OutputStream.
246
- * <p>Note that if any methods are called on the OutputStream
246
+ * Get an {@link InputStream} to retrieve the contents of this
247
+ * {@code FastByteArrayOutputStream}.
248
+ * <p>Note that if any methods are called on this {@code FastByteArrayOutputStream}
247
249
* (including, but not limited to, any of the write methods, {@link #reset()},
248
250
* {@link #toByteArray()}, and {@link #toByteArrayUnsafe()}) then the
249
- * {@link java.io. InputStream}'s behavior is undefined.
250
- * @return {@link InputStream} of the contents of this OutputStream
251
+ * {@code InputStream}'s behavior is undefined.
252
+ * @return {@code InputStream} of the contents of this {@code FastByteArrayOutputStream}
251
253
*/
252
254
public InputStream getInputStream () {
253
255
return new FastByteArrayInputStream (this );
254
256
}
255
257
256
258
/**
257
- * Write the buffers content to the given OutputStream.
259
+ * Write the contents of this {@code FastByteArrayOutputStream} to the given
260
+ * {@link OutputStream}.
258
261
* @param out the OutputStream to write to
259
262
*/
260
263
public void writeTo (OutputStream out ) throws IOException {
@@ -271,7 +274,7 @@ public void writeTo(OutputStream out) throws IOException {
271
274
}
272
275
273
276
/**
274
- * Resize the internal buffer size to a specified capacity.
277
+ * Resize the internal buffer size to the specified capacity.
275
278
* @param targetCapacity the desired size of the buffer
276
279
* @throws IllegalArgumentException if the given capacity is smaller than
277
280
* the actual size of the content stored in the buffer already
@@ -340,7 +343,7 @@ private static int nextPowerOf2(int val) {
340
343
341
344
/**
342
345
* An implementation of {@link java.io.InputStream} that reads from a given
343
- * < code> FastByteArrayOutputStream</code> .
346
+ * {@ code FastByteArrayOutputStream} .
344
347
*/
345
348
private static final class FastByteArrayInputStream extends UpdateMessageDigestInputStream {
346
349
@@ -358,8 +361,8 @@ private static final class FastByteArrayInputStream extends UpdateMessageDigestI
358
361
private int totalBytesRead = 0 ;
359
362
360
363
/**
361
- * Create a new < code>FastByteArrayOutputStreamInputStream</code> backed
362
- * by the given < code> FastByteArrayOutputStream</code> .
364
+ * Create a new {@ code FastByteArrayInputStream} backed by the given
365
+ * {@ code FastByteArrayOutputStream} .
363
366
*/
364
367
public FastByteArrayInputStream (FastByteArrayOutputStream fastByteArrayOutputStream ) {
365
368
this .fastByteArrayOutputStream = fastByteArrayOutputStream ;
0 commit comments