简体   繁体   English

FileChannel和FileInputStream中read(ByteBuffer)和read(byte [])之间的区别

[英]difference between read(ByteBuffer) and read(byte[]) in FileChannel and FileInputStream

I am newly to NIO and I find an article saying ' the block-based transmission is commonly more effective than stream-based transmission '. 刚来NIO ,发现有一篇文章说“ 基于块的传输通常比基于流的传输更有效 ”。 It means read(ByteBuffer) is block-based transmission and read(byte[]) is stream-based transmission. 这意味着read(ByteBuffer)是基于块的传输,而read(byte [])是基于流的传输。

I want to know what's the internal difference between the two methods. 我想知道两种方法之间的内部区别是什么。

ps:I also hear block-based transmission is transferring byte arrays and stream-based transmission is transferring byte one by one. ps:我也听到基于块的传输正在传输字节数组,而基于流的传输正在逐字节传输字节。 I think it's wrong, because java.io.FileInputStream.read(byte[]) transfers byte array as well. 我认为这是错误的,因为java.io.FileInputStream.read(byte [])也会传输字节数组。

One thing that makes Bytebuffer more efficient is using direct memory. 使字节缓冲区更有效的一件事是使用直接内存。 This avoids a copy from direct memory into a byte[]. 这样可以避免将直接存储器中的副本复制到byte []中。 If you are merely copying data from one Channel to another this can be up to 30% faster. 如果仅将数据从一个通道复制到另一个通道,则速度最高可提高30%。 If you reading byte by byte it can be slightly slower to use a ByteBuffer as it has more overhead accessing each byte. 如果逐字节读取,使用ByteBuffer可能会稍慢一些,因为访问每个字节的开销更大。 If you use it to read binary eg int or double it can be much faster as it can grab the whole value in one access. 如果使用它读取二进制(例如intdouble ,则可以更快,因为它可以一次访问所有值。

I think you're talking about buffer-based vs stream-based I/O operations. 我认为你是在谈论buffer-based VS stream-based I / O操作。 Java NIO is buffer oriented in the sense that data is first read into a buffer which is then processed. Java NIO是面向缓冲区的,即首先将数据读入缓冲区然后进行处理。 This gives one flexibility. 这提供了一种灵活性。 Also, you need to be sure that the buffer has all the data you require, before you process it. 另外,在处理缓冲区之前,您需要确保缓冲区具有所需的所有数据。 On the other hand, with stream-based I/O, you read one or more bytes from the stream, these are not cached anywhere. 另一方面,使用stream-based I / O,您将从流中读取一个或多个字节,这些字节不会缓存在任何地方。 This is a blocking I/O, while buffer-based I/O (which is Java NIO) is a non-blocking IO. 这是一个阻塞I / O,而buffer-based I / O(Java NIO)是一个非阻塞IO。

While I wouldn't use "stream-based" to characterize read(byte[]) , there are efficiency gains to a ByteBuffer over a byte[] in some cases. 尽管我不会使用“基于流的”来表征read(byte[]) ,但在某些情况下, ByteBuffer效率要高于byte[]

See A simple rule of when I should use direct buffers with Java NIO for network I/O? 请参见关于何时应将直接缓冲区与Java NIO一起用于网络I / O的简单规则? and ByteBuffer.allocate() vs. ByteBuffer.allocateDirect() ByteBuffer.allocate()与ByteBuffer.allocateDirect()

The memory backing a ByteBuffer can be (if "direct") easier for the JVM to pass to the OS and to do IO tricks with (for example passing the memory directly to read to write calls), and may not be "on" the JVM's heap. 支持ByteBuffer的内存可以使JVM更容易(如果是“直接”)传递给OS并执行IO技巧(例如,直接传递内存以进行读写调用),并且可能不会“打开” JVM的堆。 The memory backing a byte[] is on the JVM heap and IO generally does not go directly into the memory used by the array (instead it often goes through a bounce buffer --- because the GC may "move" array objects around in memory while IO is pending or the array memory may not be contiguous). 支持byte[]的内存在JVM堆上,并且IO通常不会直接进入数组使用的内存中(相反,它通常会通过反弹缓冲区---因为GC可能会在内存中“移动”数组对象IO未决或阵列内存可能不连续)。

However, if you have to manipulate the data in Java, a ByteBuffer may not make much difference, as you'll eventually have to copy the data into the Java heap to manipulate it. 但是,如果您必须使用Java操作数据,则ByteBuffer可能不会有太大的不同,因为最终您将不得不将数据复制到Java堆中以对其进行操作。 If you're doing a data copy in and back out with out manipulation, a direct ByteBuffer can be a win. 如果您要在不进行任何操作的情况下进行数据复制进出,则直接使用ByteBuffer可能是一个成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FileChannel.read(ByteBuffer)是否像RandomAccessFile.readFully(byte [])一样工作? - Does FileChannel.read(ByteBuffer) work like RandomAccessFile.readFully(byte[])? FileInputStream.read(...) 和 RandomAccessFile 的区别 - Difference between FileInputStream.read(...) and RandomAccessFile FileInputStream 逐字节或块读取? - FileInputStream read byte by byte or block? FileInputStream.read(byte[]) 有什么问题? - What is wrong with FileInputStream.read(byte[])? fileinputstream中read方法中使用的字节数据类型 - types of byte data used in read method in fileinputstream 如何使用 java.nio.channels.FileChannel 读取到 ByteBuffer 实现类似 BufferedReader#readLine() 的行为 - How to use java.nio.channels.FileChannel to read to ByteBuffer achieve similiar behavior like BufferedReader#readLine() 如何使用FileChannel和ByteBuffer从文件写入和读取Java对象中的字符串属性 - How to write and read a string property in a Java Object from a file with FileChannel and ByteBuffer FileInputStream类方法read(byte [] b)如何工作? - FileInputStream class method read(byte[ ] b) how that works? fileinputstream以特定顺序读取给定文件并存储在byte []数组中 - fileinputstream to read a given file in a particular order and store in byte[] array Java Socket.read(ByteBuffer dst)没有得到任何字节 - Java Socket.read(ByteBuffer dst) not getting any byte
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM