Skip to content

Commit cd62dfe

Browse files
committed
Polish FastByteArrayOutputStream[Tests]
See spring-projectsgh-31737
1 parent 7cdacf3 commit cd62dfe

File tree

2 files changed

+50
-47
lines changed

2 files changed

+50
-47
lines changed

spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java

+38-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,12 +33,12 @@
3333
* its sibling {@link ResizableByteArrayOutputStream}.
3434
*
3535
* <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.
3838
*
3939
* <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.
4242
*
4343
* @author Craig Andrews
4444
* @author Juergen Hoeller
@@ -72,16 +72,16 @@ public class FastByteArrayOutputStream extends OutputStream {
7272

7373

7474
/**
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.
7777
*/
7878
public FastByteArrayOutputStream() {
7979
this(DEFAULT_BLOCK_SIZE);
8080
}
8181

8282
/**
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.
8585
* @param initialBlockSize the initial buffer size in bytes
8686
*/
8787
public FastByteArrayOutputStream(int initialBlockSize) {
@@ -150,59 +150,60 @@ public void close() {
150150
}
151151

152152
/**
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
154154
* platform's default character set. The length of the new {@code String}
155155
* 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.
157157
* <p>This method always replaces malformed-input and unmappable-character
158158
* sequences with the default replacement string for the platform's
159159
* default character set. The {@linkplain java.nio.charset.CharsetDecoder}
160160
* class should be used when more control over the decoding process is
161161
* 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)
163164
*/
164165
@Override
165166
public String toString() {
166167
return toString(Charset.defaultCharset());
167168
}
168169

169170
/**
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()
176177
*/
177178
public String toString(Charset charset) {
178179
if (size() == 0) {
179180
return "";
180181
}
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);
183184
}
184185
return new String(toByteArrayUnsafe(), charset);
185186
}
186187

187188
// Custom methods
188189

189190
/**
190-
* Return the number of bytes stored in this <code>FastByteArrayOutputStream</code>.
191+
* Return the number of bytes stored in this {@code FastByteArrayOutputStream}.
191192
*/
192193
public int size() {
193194
return (this.alreadyBufferedSize + this.index);
194195
}
195196

196197
/**
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.
198199
* <p>Also replaces the internal structures with the byte array to
199200
* conserve memory: if the byte array is being created anyway, we might
200201
* as well as use it. This approach also means that if this method is
201202
* called twice without any writes in the interim, the second call is
202203
* a no-op.
203204
* <p>This method is "unsafe" as it returns the internal buffer.
204205
* 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
206207
* @see #size()
207208
* @see #toByteArray()
208209
*/
@@ -218,8 +219,8 @@ public byte[] toByteArrayUnsafe() {
218219
/**
219220
* Create a newly allocated byte array.
220221
* <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
223224
* @see #size()
224225
* @see #toByteArrayUnsafe()
225226
*/
@@ -229,7 +230,7 @@ public byte[] toByteArray() {
229230
}
230231

231232
/**
232-
* Reset the contents of this <code>FastByteArrayOutputStream</code>.
233+
* Reset the contents of this {@code FastByteArrayOutputStream}.
233234
* <p>All currently accumulated output in the output stream is discarded.
234235
* The output stream can be used again.
235236
*/
@@ -242,19 +243,21 @@ public void reset() {
242243
}
243244

244245
/**
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}
247249
* (including, but not limited to, any of the write methods, {@link #reset()},
248250
* {@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}
251253
*/
252254
public InputStream getInputStream() {
253255
return new FastByteArrayInputStream(this);
254256
}
255257

256258
/**
257-
* Write the buffers content to the given OutputStream.
259+
* Write the contents of this {@code FastByteArrayOutputStream} to the given
260+
* {@link OutputStream}.
258261
* @param out the OutputStream to write to
259262
*/
260263
public void writeTo(OutputStream out) throws IOException {
@@ -271,7 +274,7 @@ public void writeTo(OutputStream out) throws IOException {
271274
}
272275

273276
/**
274-
* Resize the internal buffer size to a specified capacity.
277+
* Resize the internal buffer size to the specified capacity.
275278
* @param targetCapacity the desired size of the buffer
276279
* @throws IllegalArgumentException if the given capacity is smaller than
277280
* the actual size of the content stored in the buffer already
@@ -340,7 +343,7 @@ private static int nextPowerOf2(int val) {
340343

341344
/**
342345
* An implementation of {@link java.io.InputStream} that reads from a given
343-
* <code>FastByteArrayOutputStream</code>.
346+
* {@code FastByteArrayOutputStream}.
344347
*/
345348
private static final class FastByteArrayInputStream extends UpdateMessageDigestInputStream {
346349

@@ -358,8 +361,8 @@ private static final class FastByteArrayInputStream extends UpdateMessageDigestI
358361
private int totalBytesRead = 0;
359362

360363
/**
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}.
363366
*/
364367
public FastByteArrayInputStream(FastByteArrayOutputStream fastByteArrayOutputStream) {
365368
this.fastByteArrayOutputStream = fastByteArrayOutputStream;

spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,15 +29,13 @@
2929
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3030

3131
/**
32-
* Test suite for {@link FastByteArrayOutputStream}.
32+
* Tests for {@link FastByteArrayOutputStream}.
3333
*
3434
* @author Craig Andrews
3535
*/
3636
class FastByteArrayOutputStreamTests {
3737

38-
private static final int INITIAL_CAPACITY = 256;
39-
40-
private final FastByteArrayOutputStream os = new FastByteArrayOutputStream(INITIAL_CAPACITY);
38+
private final FastByteArrayOutputStream os = new FastByteArrayOutputStream();
4139

4240
private final byte[] helloBytes = "Hello World".getBytes(StandardCharsets.UTF_8);
4341

@@ -63,16 +61,18 @@ void stringConversion() throws Exception {
6361
assertThat(this.os.toString()).isEqualTo("Hello World");
6462
assertThat(this.os.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World");
6563

64+
@SuppressWarnings("resource")
6665
FastByteArrayOutputStream empty = new FastByteArrayOutputStream();
6766
assertThat(empty.toString()).isEqualTo("");
6867
assertThat(empty.toString(StandardCharsets.US_ASCII)).isEqualTo("");
6968

69+
@SuppressWarnings("resource")
7070
FastByteArrayOutputStream outputStream = new FastByteArrayOutputStream(5);
7171
// Add bytes in multiple writes to ensure we get more than one buffer internally
7272
outputStream.write(this.helloBytes, 0, 5);
7373
outputStream.write(this.helloBytes, 5, 6);
74-
assertThat(outputStream.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World");
7574
assertThat(outputStream.toString()).isEqualTo("Hello World");
75+
assertThat(outputStream.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World");
7676
}
7777

7878
@Test
@@ -102,10 +102,9 @@ void reset() throws Exception {
102102
}
103103

104104
@Test
105-
void close() throws Exception {
105+
void close() {
106106
this.os.close();
107-
assertThatIOException().isThrownBy(() ->
108-
this.os.write(this.helloBytes));
107+
assertThatIOException().isThrownBy(() -> this.os.write(this.helloBytes));
109108
}
110109

111110
@Test
@@ -128,8 +127,9 @@ void writeTo() throws Exception {
128127
@Test
129128
void failResize() throws Exception {
130129
this.os.write(this.helloBytes);
131-
assertThatIllegalArgumentException().isThrownBy(() ->
132-
this.os.resize(5));
130+
assertThatIllegalArgumentException()
131+
.isThrownBy(() -> this.os.resize(5))
132+
.withMessage("New capacity must not be smaller than current size");
133133
}
134134

135135
@Test
@@ -156,7 +156,7 @@ void getInputStreamRead() throws Exception {
156156

157157
@Test
158158
void getInputStreamReadBytePromotion() throws Exception {
159-
byte[] bytes = new byte[] { -1 };
159+
byte[] bytes = { -1 };
160160
this.os.write(bytes);
161161
InputStream inputStream = this.os.getInputStream();
162162
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

0 commit comments

Comments
 (0)