[英]Why did Java 11 add writeBytes(byte[] b) method to the ByteArrayOutputStream class while the write(byte[] b) method did the same?
Oracle added writeBytes(byte[] b)
method to the ByteArrayOutputStream
class since Java 11. This method takes a byte array and writes it to ByteArrayOutputStream
.从 Java 11 开始,Oracle 在
ByteArrayOutputStream
类中添加了writeBytes(byte[] b)
方法。该方法接受一个字节数组并将其写入ByteArrayOutputStream
。 However, ByteArrayOutputStream
extends OutputStream
class that defines a write(byte[] b)
to do same thing.但是,
ByteArrayOutputStream
扩展了OutputStream
类,该类定义了一个write(byte[] b)
来做同样的事情。 Why did Java need a new method to do that?为什么 Java 需要一种新方法来做到这一点?
As noted by others, the benefit of the new method is that it isn't declared as throws IOException
.正如其他人所指出的,新方法的好处是它没有被声明为
throws IOException
。
It was added in response to this issue JDK-8180410 "ByteArrayOutputStream should not throw IOExceptions" .它是针对JDK-8180410 "ByteArrayOutputStream should not throw IOExceptions"问题添加的。 The stated rationale in the issue is simply that it makes no sense 1 to have to write a
try ... catch
for an exception that will never be thrown.该问题中陈述的理由很简单, 1必须为永远不会抛出的异常编写
try ... catch
是没有意义的。 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." 1 - 正如记者所说: “这很矛盾。”
ByteArrayOutputStream.writeBytes doesn't throw the unnecessary IOException
. ByteArrayOutputStream.writeBytes不会抛出不必要的
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)这是一种特定于字节数组输出流的新方法,而其他选项是从OutputStream继承的,并被声明为抛出检查过的
IOException
(如果您知道您正在写入字节数组输出流,这是一个不必要的痛苦处理 -您没有网络连接失败或类似情况)
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:一方面,在 OutputStream 类中,我们有这 3 个嵌套方法来写入字节:
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.以上所有方法都会抛出 IOException。
On the other hand writesByte method of ByteArrayOutputStream calls this method:另一方面,ByteArrayOutputStream 的 writesByte 方法调用此方法:
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.这些方法在写入字节之前检查字节数组的容量,因此它们摆脱了 IOException。 Also, the write method is trade safe because it is synchronized method.
此外,write 方法是交易安全的,因为它是同步方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.