2828import java .util .Collections ;
2929import java .util .List ;
3030
31+ import org .apache .commons .io .Charsets ;
3132import org .apache .commons .io .IOUtils ;
3233import org .apache .commons .io .input .ClosedInputStream ;
3334
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
0 commit comments