Skip to content

Commit b1c3d9d

Browse files
committed
Add AbstractByteArrayOutputStream.write(CharSequence, Charset)
- Add AbstractByteArrayOutputStream.write(byte[]) - Refactor and update ByteArrayOutputStreamTest
1 parent 9e8cb86 commit b1c3d9d

6 files changed

Lines changed: 427 additions & 114 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ The <action> type attribute can be add,update,fix,remove.
5454
<action dev="ggregory" type="add" issue="IO-860" due-to="Nico Strecker, Gary Gregory">Add ThrottledInputStream.Builder.setMaxBytes(long, ChronoUnit).</action>
5555
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOIterable.</action>
5656
<action dev="ggregory" type="add" due-to="Gary Gregory">ReversedLinesFileReader implements IOIterable&lt;String&gt;.</action>
57+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractByteArrayOutputStream.write(CharSequence, Charset).</action>
58+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractByteArrayOutputStream.write(byte[]).</action>
5759
<!-- UPDATE -->
5860
</release>
5961
<release version="2.18.0" date="2024-11-16" description="Version 2.18.0: Java 8 is required.">

src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Collections;
2929
import java.util.List;
3030

31+
import org.apache.commons.io.Charsets;
3132
import org.apache.commons.io.IOUtils;
3233
import org.apache.commons.io.input.ClosedInputStream;
3334

@@ -55,9 +56,10 @@
5556
* ignored.
5657
* </p>
5758
*
59+
* @param <T> The AbstractByteArrayOutputStream subclass
5860
* @since 2.7
5961
*/
60-
public abstract class AbstractByteArrayOutputStream extends OutputStream {
62+
public abstract class AbstractByteArrayOutputStream<T extends AbstractByteArrayOutputStream<T>> extends OutputStream {
6163

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

88+
/** The total count of bytes written. */
89+
protected int count;
90+
91+
/** The current buffer. */
92+
private byte[] currentBuffer;
93+
8694
/** The index of the current buffer. */
8795
private int currentBufferIndex;
8896

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

92-
/** The current buffer. */
93-
private byte[] currentBuffer;
94-
95-
/** The total count of bytes written. */
96-
protected int count;
97-
98100
/** Flag to indicate if the buffers can be reused after reset */
99101
private boolean reuseBuffers = true;
100102

@@ -105,6 +107,16 @@ public AbstractByteArrayOutputStream() {
105107
// empty
106108
}
107109

110+
/*
111+
* Returns this instance typed to {@code T}.
112+
*
113+
* @return this instance
114+
*/
115+
@SuppressWarnings("unchecked")
116+
protected T asThis() {
117+
return (T) this;
118+
}
119+
108120
/**
109121
* Does nothing.
110122
*
@@ -304,9 +316,34 @@ public String toString(final String enc) throws UnsupportedEncodingException {
304316
return new String(toByteArray(), enc);
305317
}
306318

319+
/**
320+
* 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)}.
321+
*
322+
* @param b the data.
323+
* @see #write(byte[], int, int)
324+
* @since 2.19.0
325+
*/
326+
@Override
327+
public void write(final byte b[]) {
328+
write(b, 0, b.length);
329+
}
330+
307331
@Override
308332
public abstract void write(final byte[] b, final int off, final int len);
309333

334+
/**
335+
* Writes the bytes for given CharSequence encoded using a Charset.
336+
*
337+
* @param data The String to convert to bytes. not null.
338+
* @param charset The {@link Charset} o encode the {@code String}, null means the default encoding.
339+
* @return this instance.
340+
* @since 2.19.0
341+
*/
342+
public T write(final CharSequence data, final Charset charset) {
343+
write(data.toString().getBytes(Charsets.toCharset(charset)));
344+
return asThis();
345+
}
346+
310347
/**
311348
* Writes the entire contents of the specified input stream to this
312349
* byte stream. Bytes from the input stream are read directly into the

src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* Implements a ThreadSafe version of {@link AbstractByteArrayOutputStream} using instance synchronization.
2626
*/
2727
//@ThreadSafe
28-
public class ByteArrayOutputStream extends AbstractByteArrayOutputStream {
28+
public class ByteArrayOutputStream extends AbstractByteArrayOutputStream<ByteArrayOutputStream> {
2929

3030
/**
3131
* Fetches entire contents of an {@link InputStream} and represent

src/main/java/org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @since 2.7
3737
*/
3838
//@NotThreadSafe
39-
public final class UnsynchronizedByteArrayOutputStream extends AbstractByteArrayOutputStream {
39+
public final class UnsynchronizedByteArrayOutputStream extends AbstractByteArrayOutputStream<UnsynchronizedByteArrayOutputStream> {
4040

4141
// @formatter:off
4242
/**

0 commit comments

Comments
 (0)