Java FileInputStream读取方法的样式
FileInputStream读取方法的签名是否正确Java FileInputStream读取方法的样式,java,coding-style,Java,Coding Style,FileInputStream读取方法的签名是否正确 public int read(byte[] b) throws IOException // Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available. // Returns: the total numbe
public int read(byte[] b) throws IOException
// Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.
// Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
有这样的签名比有这样的签名有什么好处——
public byte[] read(int numberOfBytes) throws IOException
// Reads up to numberOfBytes bytes of data from this input stream into an array of bytes.
// Returns- an array of bytes read. Array is empty if there is no more data because the end of the file has been reached.
第一种形式允许多次执行相同的byte[]数组。基本上,您可以读取整个流,从而产生最小的垃圾低GC活动 后者显然更方便,但每次在read方法内部执行byte[]时,都需要创建新的实例。这意味着,即使在100字节块中读取10Gib文件,应用程序也会总共分配10Gib的内存——不是在同一时间,但垃圾收集器仍然会疯狂地工作
请看一看-它遵循相同的原则。第一种形式允许您将相同的byte[]数组重新用于多次执行。基本上,您可以读取整个流,从而产生最小的垃圾低GC活动 后者显然更方便,但每次在read方法内部执行byte[]时,都需要创建新的实例。这意味着,即使在100字节块中读取10Gib文件,应用程序也会总共分配10Gib的内存——不是在同一时间,但垃圾收集器仍然会疯狂地工作
看一看-它遵循相同的原则。后一个如何更方便?后一个如何更方便?