Oracle added writeBytes(byte[] b)
method to the ByteArrayOutputStream
class since Java 11. This method takes a byte array and writes it to ByteArrayOutputStream
. However, ByteArrayOutputStream
extends OutputStream
class that defines a write(byte[] b)
to do same thing. Why did Java need a new method to do that?

- 100,966
- 191
- 140
- 197

- 405
- 1
- 5
- 12
-
Does this answer your question? https://stackoverflow.com/a/52308828/61158 – akarnokd Jun 16 '22 at 08:41
3 Answers
As noted by others, the benefit of the new method is that it isn't declared as throws IOException
.
It was added in response to this issue JDK-8180410 "ByteArrayOutputStream should not throw IOExceptions". The stated rationale in the issue is simply that it makes no sense1 to have to write a try ... catch
for an exception that will never be thrown. That's it.
They (strictly) didn't need to add it. They added it as a convenience.
1 - As the reporter puts it: "It's contradictive."

- 698,415
- 94
- 811
- 1,216
-
Still entirely pointless AFAICS. If you know you have a `ByteArrayOutputStream` you know you can call `write()` without a `catch`, and so does the compiler, and if you don't know it you can't call `writeBytes()` anyway. – user207421 Jun 16 '22 at 23:49
-
No, you cannot call `write` without caching `IOException`, because `IOException` is a checked Exception, which must be handled. One could change the signature not to throw it, but that would break backwards compatibility. – Dorian Gray Jun 17 '22 at 16:16
-
-
ByteArrayOutputStream.writeBytes doesn't throw the unnecessary IOException
.
It's a new method specific to the byte array output stream, whereas the other options are inherited from OutputStream and are declared to throw the checked IOException
(which is an unnecessary pain to handle if you know you're writing to a byte array output stream - you don't have a network connection to fail or similar)

- 268,207
- 37
- 334
- 440
-
But what is the point? You can only call it if your reference variable is of type `ByteArrayOutputStream`, and if it is you can alreay call `write()` without a `catch`. – user207421 Jun 17 '22 at 01:57
-
1Which write() ? You either have to call a variant which throws a checked exception, or you have to provide the offset and length. writeBytes() mitigates both those issues – Brian Agnew Jun 17 '22 at 07:30
Both method write bytes to outputstream. To compare them first we should look at their source code:
On the one hand in OutputStream class we have these 3 nested method to writing bytes:
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
public void write(byte b[], int off, int len) throws IOException {
Objects.checkFromIndexSize(off, len, b.length);
for (int i = 0 ; i < len ; i++) {
write(b[off + i]);
}
}
public abstract void write(int b) throws IOException;
All above method throws IOException.
On the other hand writesByte method of ByteArrayOutputStream calls this method:
public void writeBytes(byte b[]) {
write(b, 0, b.length);
}
public synchronized void write(byte b[], int off, int len) {
Objects.checkFromIndexSize(off, len, b.length);
ensureCapacity(count + len);
System.arraycopy(b, off, buf, count, len);
count += len;
}
These methods check byte array capacity before writing bytes, so they got rid of IOException. Also, the write method is trade safe because it is synchronized method.

- 405
- 1
- 5
- 12
-
None of those methods throws `UncheckedException`, for the simple reason that it doesn't exist. By 'tradesafe' you possibly mean 'threadsafe', but anything is possible. – user207421 Jun 16 '22 at 08:37
-