From cd62dfe3a9bf3c8f2e45a6bdf1f232045dea549a Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 2 Dec 2023 16:19:22 +0100 Subject: [PATCH] Polish FastByteArrayOutputStream[Tests] See gh-31737 --- .../util/FastByteArrayOutputStream.java | 73 ++++++++++--------- .../util/FastByteArrayOutputStreamTests.java | 24 +++--- 2 files changed, 50 insertions(+), 47 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java b/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java index 10b8798f2b9..7e7f1430c7f 100644 --- a/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java +++ b/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,12 +33,12 @@ import org.springframework.lang.Nullable; * its sibling {@link ResizableByteArrayOutputStream}. * *

Unlike {@link java.io.ByteArrayOutputStream}, this implementation is backed - * by a {@link java.util.ArrayDeque} of {@code byte[]} instead of 1 constantly - * resizing {@code byte[]}. It does not copy buffers when it gets expanded. + * by a {@link java.util.ArrayDeque} of {@code byte[]} buffers instead of one + * constantly resizing {@code byte[]}. It does not copy buffers when it gets expanded. * *

The initial buffer is only created when the stream is first written. - * There is also no copying of the internal buffer if its content is extracted - * with the {@link #writeTo(OutputStream)} method. + * There is also no copying of the internal buffers if the stream's content is + * extracted via the {@link #writeTo(OutputStream)} method. * * @author Craig Andrews * @author Juergen Hoeller @@ -72,16 +72,16 @@ public class FastByteArrayOutputStream extends OutputStream { /** - * Create a new FastByteArrayOutputStream - * with the default initial capacity of 256 bytes. + * Create a new {@code FastByteArrayOutputStream} with the default initial + * capacity of 256 bytes. */ public FastByteArrayOutputStream() { this(DEFAULT_BLOCK_SIZE); } /** - * Create a new FastByteArrayOutputStream - * with the specified initial capacity. + * Create a new {@code FastByteArrayOutputStream} with the specified initial + * capacity. * @param initialBlockSize the initial buffer size in bytes */ public FastByteArrayOutputStream(int initialBlockSize) { @@ -150,16 +150,17 @@ public class FastByteArrayOutputStream extends OutputStream { } /** - * Convert the buffer's contents into a string decoding bytes using the + * Convert this stream's contents to a string by decoding the bytes using the * platform's default character set. The length of the new {@code String} * is a function of the character set, and hence may not be equal to the - * size of the buffer. + * size of the buffers. *

This method always replaces malformed-input and unmappable-character * sequences with the default replacement string for the platform's * default character set. The {@linkplain java.nio.charset.CharsetDecoder} * class should be used when more control over the decoding process is * required. - * @return a String decoded from the buffer's contents + * @return a String decoded from this stream's contents + * @see #toString(Charset) */ @Override public String toString() { @@ -167,19 +168,19 @@ public class FastByteArrayOutputStream extends OutputStream { } /** - * Converts the buffer's contents into a string by decoding the bytes using - * the specified {@link java.nio.charset.Charset charset}. - * - * @param charset the {@linkplain java.nio.charset.Charset charset} - * to be used to decode the {@code bytes} - * @return a String decoded from the buffer's contents + * Convert this stream's contents to a string by decoding the bytes using the + * specified {@link Charset}. + * @param charset the {@link Charset} to use to decode the bytes + * @return a String decoded from this stream's contents + * @since 6.1.2 + * @see #toString() */ public String toString(Charset charset) { if (size() == 0) { return ""; } - if (buffers.size() == 1) { - return new String(buffers.getFirst(), 0, index, charset); + if (this.buffers.size() == 1) { + return new String(this.buffers.getFirst(), 0, this.index, charset); } return new String(toByteArrayUnsafe(), charset); } @@ -187,14 +188,14 @@ public class FastByteArrayOutputStream extends OutputStream { // Custom methods /** - * Return the number of bytes stored in this FastByteArrayOutputStream. + * Return the number of bytes stored in this {@code FastByteArrayOutputStream}. */ public int size() { return (this.alreadyBufferedSize + this.index); } /** - * Convert the stream's data to a byte array and return the byte array. + * Convert this stream's contents to a byte array and return the byte array. *

Also replaces the internal structures with the byte array to * conserve memory: if the byte array is being created anyway, we might * as well as use it. This approach also means that if this method is @@ -202,7 +203,7 @@ public class FastByteArrayOutputStream extends OutputStream { * a no-op. *

This method is "unsafe" as it returns the internal buffer. * Callers should not modify the returned buffer. - * @return the current contents of this output stream, as a byte array. + * @return the current contents of this stream as a byte array * @see #size() * @see #toByteArray() */ @@ -218,8 +219,8 @@ public class FastByteArrayOutputStream extends OutputStream { /** * Create a newly allocated byte array. *

Its size is the current size of this output stream, and it will - * contain the valid contents of the internal buffer. - * @return the current contents of this output stream, as a byte array + * contain the valid contents of the internal buffers. + * @return the current contents of this stream as a byte array * @see #size() * @see #toByteArrayUnsafe() */ @@ -229,7 +230,7 @@ public class FastByteArrayOutputStream extends OutputStream { } /** - * Reset the contents of this FastByteArrayOutputStream. + * Reset the contents of this {@code FastByteArrayOutputStream}. *

All currently accumulated output in the output stream is discarded. * The output stream can be used again. */ @@ -242,19 +243,21 @@ public class FastByteArrayOutputStream extends OutputStream { } /** - * Get an {@link InputStream} to retrieve the data in this OutputStream. - *

Note that if any methods are called on the OutputStream + * Get an {@link InputStream} to retrieve the contents of this + * {@code FastByteArrayOutputStream}. + *

Note that if any methods are called on this {@code FastByteArrayOutputStream} * (including, but not limited to, any of the write methods, {@link #reset()}, * {@link #toByteArray()}, and {@link #toByteArrayUnsafe()}) then the - * {@link java.io.InputStream}'s behavior is undefined. - * @return {@link InputStream} of the contents of this OutputStream + * {@code InputStream}'s behavior is undefined. + * @return {@code InputStream} of the contents of this {@code FastByteArrayOutputStream} */ public InputStream getInputStream() { return new FastByteArrayInputStream(this); } /** - * Write the buffers content to the given OutputStream. + * Write the contents of this {@code FastByteArrayOutputStream} to the given + * {@link OutputStream}. * @param out the OutputStream to write to */ public void writeTo(OutputStream out) throws IOException { @@ -271,7 +274,7 @@ public class FastByteArrayOutputStream extends OutputStream { } /** - * Resize the internal buffer size to a specified capacity. + * Resize the internal buffer size to the specified capacity. * @param targetCapacity the desired size of the buffer * @throws IllegalArgumentException if the given capacity is smaller than * the actual size of the content stored in the buffer already @@ -340,7 +343,7 @@ public class FastByteArrayOutputStream extends OutputStream { /** * An implementation of {@link java.io.InputStream} that reads from a given - * FastByteArrayOutputStream. + * {@code FastByteArrayOutputStream}. */ private static final class FastByteArrayInputStream extends UpdateMessageDigestInputStream { @@ -358,8 +361,8 @@ public class FastByteArrayOutputStream extends OutputStream { private int totalBytesRead = 0; /** - * Create a new FastByteArrayOutputStreamInputStream backed - * by the given FastByteArrayOutputStream. + * Create a new {@code FastByteArrayInputStream} backed by the given + * {@code FastByteArrayOutputStream}. */ public FastByteArrayInputStream(FastByteArrayOutputStream fastByteArrayOutputStream) { this.fastByteArrayOutputStream = fastByteArrayOutputStream; diff --git a/spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java b/spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java index 94e520d9755..c5e6a14d532 100644 --- a/spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java +++ b/spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,15 +29,13 @@ import static org.assertj.core.api.Assertions.assertThatIOException; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** - * Test suite for {@link FastByteArrayOutputStream}. + * Tests for {@link FastByteArrayOutputStream}. * * @author Craig Andrews */ class FastByteArrayOutputStreamTests { - private static final int INITIAL_CAPACITY = 256; - - private final FastByteArrayOutputStream os = new FastByteArrayOutputStream(INITIAL_CAPACITY); + private final FastByteArrayOutputStream os = new FastByteArrayOutputStream(); private final byte[] helloBytes = "Hello World".getBytes(StandardCharsets.UTF_8); @@ -63,16 +61,18 @@ class FastByteArrayOutputStreamTests { assertThat(this.os.toString()).isEqualTo("Hello World"); assertThat(this.os.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World"); + @SuppressWarnings("resource") FastByteArrayOutputStream empty = new FastByteArrayOutputStream(); assertThat(empty.toString()).isEqualTo(""); assertThat(empty.toString(StandardCharsets.US_ASCII)).isEqualTo(""); + @SuppressWarnings("resource") FastByteArrayOutputStream outputStream = new FastByteArrayOutputStream(5); // Add bytes in multiple writes to ensure we get more than one buffer internally outputStream.write(this.helloBytes, 0, 5); outputStream.write(this.helloBytes, 5, 6); - assertThat(outputStream.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World"); assertThat(outputStream.toString()).isEqualTo("Hello World"); + assertThat(outputStream.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World"); } @Test @@ -102,10 +102,9 @@ class FastByteArrayOutputStreamTests { } @Test - void close() throws Exception { + void close() { this.os.close(); - assertThatIOException().isThrownBy(() -> - this.os.write(this.helloBytes)); + assertThatIOException().isThrownBy(() -> this.os.write(this.helloBytes)); } @Test @@ -128,8 +127,9 @@ class FastByteArrayOutputStreamTests { @Test void failResize() throws Exception { this.os.write(this.helloBytes); - assertThatIllegalArgumentException().isThrownBy(() -> - this.os.resize(5)); + assertThatIllegalArgumentException() + .isThrownBy(() -> this.os.resize(5)) + .withMessage("New capacity must not be smaller than current size"); } @Test @@ -156,7 +156,7 @@ class FastByteArrayOutputStreamTests { @Test void getInputStreamReadBytePromotion() throws Exception { - byte[] bytes = new byte[] { -1 }; + byte[] bytes = { -1 }; this.os.write(bytes); InputStream inputStream = this.os.getInputStream(); ByteArrayInputStream bais = new ByteArrayInputStream(bytes);