简体   繁体   English

为什么 Java 11 在 ByteArrayOutputStream 类中添加了 writeBytes​(byte[] b) 方法,而 write​(byte[] b) 方法也一样?

[英]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.

相关问题 为什么Java writeBytes方法在第一个TCP数据包中仅发送一个字节,而write方法却不发送? - Why does Java writeBytes method send only one byte in first TCP packet and write method does not? 创建自定义getColor(字节r,字节g,字节b)方法 - creating a custom getColor(byte r, byte g, byte b) method FileInputStream类方法read(byte [] b)如何工作? - FileInputStream class method read(byte[ ] b) how that works? InputStream中的read(byte b[], int off, int len)方法和FileInputStream中的read(byte b[], int off, int len)方法一样吗? - Is the method read(byte b[], int off, int len) in InputStream same as the method read(byte b[], int off, int len) in FileInputStream? java.io.InputStream `read()` 方法与 `read(byte[] b)` 相比为什么输出不同的值? - Why java.io.InputStream `read()` method outputs different value if compared with `read(byte[] b)`? 为什么Java没有检查字节的类型兼容性,因为它死于int? - Why Java did not check for Type compatibility for byte as it dies with int? 为什么我的此类的compareTo方法不返回边缘 <A,B> 和 <B,A> 一样吗? - Why is my compareTo method for this class not returning edges <A,B> and <B,A> as the same thing? java类A扩展了类B和方法覆盖 - java class A extends class B, and method override byte []到字符串到字节数组的转换在Java中无法正常工作 - byte[] to string to byte array conversion did not work fine in java write(byte [] b)大字节数组的优化使用 - write (byte[] b) optim usage for large byte array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM