Skip to content

Commit

Permalink
Polish FastByteArrayOutputStream[Tests]
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Dec 2, 2023
1 parent 7cdacf3 commit cd62dfe
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -33,12 +33,12 @@
* its sibling {@link ResizableByteArrayOutputStream}.
*
* <p>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.
*
* <p>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
Expand Down Expand Up @@ -72,16 +72,16 @@ public class FastByteArrayOutputStream extends OutputStream {


/**
* Create a new <code>FastByteArrayOutputStream</code>
* 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 <code>FastByteArrayOutputStream</code>
* 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) {
Expand Down Expand Up @@ -150,59 +150,60 @@ public void close() {
}

/**
* 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.
* <p>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() {
return toString(Charset.defaultCharset());
}

/**
* 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);
}

// Custom methods

/**
* Return the number of bytes stored in this <code>FastByteArrayOutputStream</code>.
* 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.
* <p>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
* called twice without any writes in the interim, the second call is
* a no-op.
* <p>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()
*/
Expand All @@ -218,8 +219,8 @@ public byte[] toByteArrayUnsafe() {
/**
* Create a newly allocated byte array.
* <p>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()
*/
Expand All @@ -229,7 +230,7 @@ public byte[] toByteArray() {
}

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

/**
* Get an {@link InputStream} to retrieve the data in this OutputStream.
* <p>Note that if any methods are called on the OutputStream
* Get an {@link InputStream} to retrieve the contents of this
* {@code FastByteArrayOutputStream}.
* <p>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 {
Expand All @@ -271,7 +274,7 @@ public void writeTo(OutputStream out) throws IOException {
}

/**
* 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
Expand Down Expand Up @@ -340,7 +343,7 @@ private static int nextPowerOf2(int val) {

/**
* An implementation of {@link java.io.InputStream} that reads from a given
* <code>FastByteArrayOutputStream</code>.
* {@code FastByteArrayOutputStream}.
*/
private static final class FastByteArrayInputStream extends UpdateMessageDigestInputStream {

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

/**
* Create a new <code>FastByteArrayOutputStreamInputStream</code> backed
* by the given <code>FastByteArrayOutputStream</code>.
* Create a new {@code FastByteArrayInputStream} backed by the given
* {@code FastByteArrayOutputStream}.
*/
public FastByteArrayInputStream(FastByteArrayOutputStream fastByteArrayOutputStream) {
this.fastByteArrayOutputStream = fastByteArrayOutputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,15 +29,13 @@
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);

Expand All @@ -63,16 +61,18 @@ void stringConversion() throws Exception {
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
Expand Down Expand Up @@ -102,10 +102,9 @@ void reset() throws Exception {
}

@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
Expand All @@ -128,8 +127,9 @@ void writeTo() throws Exception {
@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
Expand All @@ -156,7 +156,7 @@ void getInputStreamRead() throws Exception {

@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);
Expand Down

0 comments on commit cd62dfe

Please sign in to comment.