Skip to content

Commit

Permalink
Add AbstractByteArrayOutputStream.write(CharSequence, Charset)
Browse files Browse the repository at this point in the history
- Add AbstractByteArrayOutputStream.write(byte[])
- Refactor and update ByteArrayOutputStreamTest
  • Loading branch information
garydgregory committed Dec 12, 2024
1 parent 9e8cb86 commit b1c3d9d
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 114 deletions.
2 changes: 2 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" issue="IO-860" due-to="Nico Strecker, Gary Gregory">Add ThrottledInputStream.Builder.setMaxBytes(long, ChronoUnit).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOIterable.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">ReversedLinesFileReader implements IOIterable&lt;String&gt;.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractByteArrayOutputStream.write(CharSequence, Charset).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractByteArrayOutputStream.write(byte[]).</action>
<!-- UPDATE -->
</release>
<release version="2.18.0" date="2024-11-16" description="Version 2.18.0: Java 8 is required.">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ClosedInputStream;

Expand Down Expand Up @@ -55,9 +56,10 @@
* ignored.
* </p>
*
* @param <T> The AbstractByteArrayOutputStream subclass
* @since 2.7
*/
public abstract class AbstractByteArrayOutputStream extends OutputStream {
public abstract class AbstractByteArrayOutputStream<T extends AbstractByteArrayOutputStream<T>> extends OutputStream {

/**
* Constructor for an InputStream subclass.
Expand All @@ -83,18 +85,18 @@ protected interface InputStreamConstructor<T extends InputStream> {
/** The list of buffers, which grows and never reduces. */
private final List<byte[]> buffers = new ArrayList<>();

/** The total count of bytes written. */
protected int count;

/** The current buffer. */
private byte[] currentBuffer;

/** The index of the current buffer. */
private int currentBufferIndex;

/** The total count of bytes in all the filled buffers. */
private int filledBufferSum;

/** The current buffer. */
private byte[] currentBuffer;

/** The total count of bytes written. */
protected int count;

/** Flag to indicate if the buffers can be reused after reset */
private boolean reuseBuffers = true;

Expand All @@ -105,6 +107,16 @@ public AbstractByteArrayOutputStream() {
// empty
}

/*
* Returns this instance typed to {@code T}.
*
* @return this instance
*/
@SuppressWarnings("unchecked")
protected T asThis() {
return (T) this;
}

/**
* Does nothing.
*
Expand Down Expand Up @@ -304,9 +316,34 @@ public String toString(final String enc) throws UnsupportedEncodingException {
return new String(toByteArray(), enc);
}

/**
* Writes {@code b.length} bytes from the given byte array to this output stream. This has same effect as {@code write(b, 0, b.length)}.
*
* @param b the data.
* @see #write(byte[], int, int)
* @since 2.19.0
*/
@Override
public void write(final byte b[]) {
write(b, 0, b.length);
}

@Override
public abstract void write(final byte[] b, final int off, final int len);

/**
* Writes the bytes for given CharSequence encoded using a Charset.
*
* @param data The String to convert to bytes. not null.
* @param charset The {@link Charset} o encode the {@code String}, null means the default encoding.
* @return this instance.
* @since 2.19.0
*/
public T write(final CharSequence data, final Charset charset) {
write(data.toString().getBytes(Charsets.toCharset(charset)));
return asThis();
}

/**
* Writes the entire contents of the specified input stream to this
* byte stream. Bytes from the input stream are read directly into the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* Implements a ThreadSafe version of {@link AbstractByteArrayOutputStream} using instance synchronization.
*/
//@ThreadSafe
public class ByteArrayOutputStream extends AbstractByteArrayOutputStream {
public class ByteArrayOutputStream extends AbstractByteArrayOutputStream<ByteArrayOutputStream> {

/**
* Fetches entire contents of an {@link InputStream} and represent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @since 2.7
*/
//@NotThreadSafe
public final class UnsynchronizedByteArrayOutputStream extends AbstractByteArrayOutputStream {
public final class UnsynchronizedByteArrayOutputStream extends AbstractByteArrayOutputStream<UnsynchronizedByteArrayOutputStream> {

// @formatter:off
/**
Expand Down
Loading

0 comments on commit b1c3d9d

Please sign in to comment.