[中英对照]Java NIO Buffer 详解

Web应用开发 William 226浏览 0评论

Java NIO Buffers are used when interacting with NIO Channels. As you know, data is read from channels into buffers, and written from buffers into channels.

A buffer is essentially a block of memory into which you can write data, which you can then later read again. This memory block is wrapped in a NIO Buffer object, which provides a set of methods that makes it easier to work with the memory block.

Basic Buffer Usage

Using a Buffer to read and write data typically follows this little 4-step process:

  1. Write data into the Buffer

  2. Call buffer.flip()

  3. Read data out of the Buffer

  4. Call buffer.clear() or buffer.compact()

在与NIO通道交互时使用Java NIO Buffer。 如您所知,数据从通道读入缓冲区,并从缓冲区写入通道。

缓冲区本质上是一个可以写入数据的内存块,然后可以再次读取。 此内存块包含在NIO Buffer对象中,该对象提供了一组方法,可以更轻松地使用内存块。

基本缓冲区用法

使用缓冲区读取和写入数据通常遵循这4个小步骤:

  1. 写入数据到缓冲区

  2. 调用 buffer.flip()

  3. 从缓冲区读取数据

  4. 调用 buffer.clear() 或者 buffer.compact()

When you write data into a buffer, the buffer keeps track of how much data you have written. Once you need to read the data, you need to switch the buffer from writing mode into reading mode using the flip()method call. In reading mode the buffer lets you read all the data written into the buffer.

Once you have read all the data, you need to clear the buffer, to make it ready for writing again. You can do this in two ways: By calling clear() or by calling compact(). The clear() method clears the whole buffer. The compact() method only clears the data which you have already read. Any unread data is moved to the beginning of the buffer, and data will now be written into the buffer after the unread data.

Here is a simple Buffer usage example, with the write, flip, read and clear operations maked in bold:

RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel(); //create buffer with capacity of 48 bytes
ByteBuffer buf = ByteBuffer.allocate(48);int bytesRead = inChannel.read(buf); //read into buffer.
while (bytesRead != -1) {  buf.flip();  //make buffer ready for read   while(buf.hasRemaining()){
      System.out.print((char) buf.get()); // read 1 byte at a time
  }  buf.clear(); //make buffer ready for writing
  bytesRead = inChannel.read(buf);
}
aFile.close();

当你将数据写入Buffer时,Buffer会跟踪你已经写入了多少数据。一旦你需要读出数据,你需要调用 flip() 方法将Buffer从写模式转换到读模式。在读模式,Buffer允许你将之前写入的数据全部读出。

一旦你已经读出了所有数据,你需要清除Buffer,为下次写入数据做准备。可以通过以下两种方法来完成:clear() 和 compact()。clear() 方法清除整个Buffer,而 compact() 方法仅仅清除你已经读出的Buffer,未读数据会被移动到Buffer的开始位置,再次写入的数据会追加到未读数据的后面。

这是一个简单的 Buffer 使用的例子,使用的 write, flip, read 和 clear 操作:

RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel(); //create buffer with capacity of 48 bytes
ByteBuffer buf = ByteBuffer.allocate(48);int bytesRead = inChannel.read(buf); //read into buffer.
while (bytesRead != -1) {  buf.flip();  //make buffer ready for read   while(buf.hasRemaining()){
      System.out.print((char) buf.get()); // read 1 byte at a time
  }  buf.clear(); //make buffer ready for writing
  bytesRead = inChannel.read(buf);
}
aFile.close();

 

Buffer Capacity, Position and Limit

A buffer is essentially a block of memory into which you can write data, which you can then later read again. This memory block is wrapped in a NIO Buffer object, which provides a set of methods that makes it easier to work with the memory block.

Buffer has three properties you need to be familiar with, in order to understand how a Buffer works. These are:

  • capacity

  • position

  • limit

The meaning of position and limit depends on whether the Buffer is in read or write mode. Capacity always means the same, no matter the buffer mode.

Here is an illustration of capacity, position and limit in write and read modes. The explanation follows in the sections after the illustration.

Buffer capacity, position and limit in write and read mode.

Buffer的三个属性:容量,位置和限定符

Buffer本质上是一块你可以写入数据的内存区域,当然你也可以在写入之后读出数据。该内存区域被封装成一个 NIO Buffer 对象,它提供一系列的方法,以方便对该内存区域的操作。

为了学习Buffer 是如何工作的,Buffer 的三个属性你必须要熟悉,它们是:

  • capacity (容量)

  • position (游标位置)

  • limit (末尾限定符)

其中,position 和 limit 的意义依赖于当前 Buffer 是处于读模式还是写模式。capacity 的含义无论读写模式都是相同的。

下面是对以上三个属性在读模式和写模式的一个示例,后面会有详细的解释:

               Buffer capacity, position and limit in write and read mode.

Capacity

Being a memory block, a Buffer has a certain fixed size, also called its “capacity”. You can only write capacity bytes, longs, chars etc. into the Buffer. Once the Buffer is full, you need to empty it (read the data, or clear it) before you can write more data into it.

Position

When you write data into the Buffer, you do so at a certain position. Initially the position is 0. When a byte, long etc. has been written into the Buffer the position is advanced to point to the next cell in the buffer to insert data into. Position can maximally become capacity - 1.

When you read data from a Buffer you also do so from a given position. When you flip a Buffer from writing mode to reading mode, the position is reset back to 0. As you read data from the Buffer you do so from position, and position is advanced to next position to read.

Capacity (容量)

作为一个内存块,Buffer 有一个固定的大小,我们叫做 “capacity(容量)”。你最多只能向 Buffer 写入 capacity 大小的字节,长整数,字符等。一旦 Buffer 满了,你必须在继续写入数据之前清空它(读出数据,或清除数据)。

Position (游标位置)

当你开始向 Buffer 写入数据时,你必须知道数据将要写入的位置。position 的初始值为 0。当一个字节或长整数等类似数据类型被写入 Buffer 后,position 就会指向下一个将要写入数据的位置(根据数据类型大小计算)。position 的最大值是 capacity – 1。

当你需要从 Buffer 读出数据时,你也需要知道将要从什么位置开始读数据。在你调用 flip 方法将 Buffer 从写模式转换为读模式时,position 被重新设置为 0。然后你从 position 指向的位置开始读取数据,接下来 position 指向下一个你要读取的位置。

Limit

In write mode the limit of a Buffer is the limit of how much data you can write into the buffer. In write mode the limit is equal to the capacity of the Buffer.

When flipping the Buffer into read mode, limit means the limit of how much data you can read from the data. Therefore, when flipping a Buffer into read mode, limit is set to write position of the write mode. In other words, you can read as many bytes as were written (limit is set to the number of bytes written, which is marked by position).

Buffer Types

Java NIO comes with the following Buffer types:

  • ByteBuffer

  • MappedByteBuffer

  • CharBuffer

  • DoubleBuffer

  • FloatBuffer

  • IntBuffer

  • LongBuffer

  • ShortBuffer

As you can see, these Buffer types represent different data types. In other words, they let you work with the bytes in the buffer as char, short, int, long, float or double instead.

The MappedByteBuffer is a bit special, and will be covered in its own text.

限制(Limit)

在写模式下对一个Buffer的限制即你能将多少数据写入Buffer中。在写模式下,限制等同于Buffer的容量(capacity)。

当切换Buffer为读模式时,限制表示你最多能读取到多少数据。因此,当切换Buffer为读模式时,限制会被设置为写模式下的position值。换句话说,你能读到之前写入的所有数据(限制被设置为已写的字节数,在写模式下就是position)。

Buffer类型

Java NIO提出了如下几种Buffer类型:

  • ByteBuffer

  • MappedByteBuffer

  • CharBuffer

  • DoubleBuffer

  • FloatBuffer

  • IntBuffer

  • LongBuffer

  • ShortBuffer

正如你所看到的,这些Buffer类型代表了不同的数据类型。换句话说,他们让你可以在使用的时候用char, short, int, long, float 或者double类型来代替直接使用buffer中的字节。

其中MappedByteBuffer有点特殊,将在它自己的部分来阐述。

Allocating a Buffer

To obtain a Buffer object you must first allocate it. Every Buffer class has an allocate() method that does this. Here is an example showing the allocation of a ByteBuffer, with a capacity of 48 bytes:

ByteBuffer buf = ByteBuffer.allocate(48);

Here is an example allocating a CharBuffer with space for 1024 characters:

CharBuffer buf = CharBuffer.allocate(1024);

Writing Data to a Buffer

You can write data into a Buffer in two ways:

  1. Write data from a Channel into a Buffer

  2. Write data into the Buffer yourself, via the buffer’s put() methods.

Here is an example showing how a Channel can write data into a Buffer:

int bytesRead = inChannel.read(buf); //read into buffer.

Here is an example that writes data into a Buffer via the put() method:

buf.put(127);

There are many other versions of the put() method, allowing you to write data into the Buffer in many different ways. For instance, writing at specific positions, or writing an array of bytes into the buffer. See the JavaDoc for the concrete buffer implementation for more details.

分配一个Buffer

若要获取一个Buffer对象,必须先分配它。每个Buffer类都有allocate()函数用来分配。下面的例子展示了分配一个ByteBuffer,其容量为48字节。

ByteBuffer buf = ByteBuffer.allocate(48);

下面的例子是分配一个具有1024个字符的空间的CharBuffer:

CharBuffer buf = CharBuffer.allocate(1024);

将数据写入Buffer

有两种方法可以将数据写入Buffer:

  1. 从Channel将数据写入Buffer

  2. 调用buffer的put()函数,自己将数据写入Buffer。

下面的例子是展示Channel如何将数据写入到Buffer中:

int bytesRead = inChannel.read(buf); //read into buffer.

下面的例子是通过put()函数将数据写入Buffer:

buf.put(127);

put()函数还有很多其他版本,可以让你使用不用的方法将数据写入到Buffer。例如,在特定的位置写,或者将字节数组写入到buffer。查看JavaDoc来了解buffer实现的更多细节。

flip()

The flip() method switches a Buffer from writing mode to reading mode. Calling flip() sets the position back to 0, and sets the limit to where position just was.

In other words, position now marks the reading position, and limit marks how many bytes, chars etc. were written into the buffer – the limit of how many bytes, chars etc. that can be read.

Reading Data from a Buffer

There are two ways you can read data from a Buffer.

  1. Read data from the buffer into a channel.

  2. Read data from the buffer yourself, using one of the get() methods.

Here is an example of how you can read data from a buffer into a channel:

//read from buffer into channel.
int bytesWritten = inChannel.write(buf);

Here is an example that reads data from a Buffer using the get() method:

byte aByte = buf.get();

There are many other versions of the get() method, allowing you to read data from the Buffer in many different ways. For instance, reading at specific positions, or reading an array of bytes from the buffer. See the JavaDoc for the concrete buffer implementation for more details.

flip()

用 flip() 方法将 Buffer f从写入模式切换到读取模式。调用 flip() 将 position 设置回0,并将 limit 置为刚才的位置。

换句话说,position 现在标记了读取位置,limit 标记了写入缓冲区的字节、字符数等——可以读取的字节数、字符数等的限制。

从缓冲区读取数据

有两种方法可以从 Buffer 中读取数据。

  1. 从缓冲区读取数据到通道。

  2. 使用缓冲区自带方法中的 get() 方法从缓冲区读取数据。

下面是如何将数据从缓冲区读取到通道的例子:

//read from buffer into channel.
int bytesWritten = inChannel.write(buf);

下面是使用 get() 方法从 Buffer 中读取数据的例子:

byte aByte = buf.get();

get() 方法还有许多其他版本,允许您以多种不同的方式从 Buffer 中读取数据。 例如,在特定位置读取,或者从缓冲区读取字节数组。有关具体缓冲区实现的详细信息,请参阅JavaDoc。

rewind()

The Buffer.rewind() sets the position back to 0, so you can reread all the data in the buffer. The limit remains untouched, thus still marking how many elements (bytes, chars etc.) that can be read from the Buffer.

clear() and compact()

Once you are done reading data out of the Buffer you have to make the Buffer ready for writing again. You can do so either by calling clear() or by calling compact().

If you call clear() the position is set back to 0 and the limit to capacity. In other words, the Buffer is cleared. The data in the Buffer is not cleared. Only the markers telling where you can write data into the Buffer are.

If there is any unread data in the Buffer when you call clear() that data will be “forgotten”, meaning you no longer have any markers telling what data has been read, and what has not been read.

If there is still unread data in the Buffer, and you want to read it later, but you need to do some writing first, call compact() instead of clear().

compact() copies all unread data to the beginning of the Buffer. Then it sets position to right after the last unread element. The limit property is still set to capacity, just like clear() does. Now the Buffer is ready for writing, but you will not overwrite the unread data.

rewind()

Buffer.rewind() 将 position 设置回0,因此你可以重读缓冲区中的所有数据。这个 limit 保持不变,因此仍然标记有多少元素(字节、字符等)可以从Buffer读取。

clear() and compact()

从 Buffer 中读取数据之后,必须让 Buffer 为再次写入做好准备。您可以通过调用 clear() 或调用 compact()来做到这一点。

如果您调用 clear() ,position 将被设置为0,并 limit 置为 capacity。换句话说,Buffer 被清除。Buffer 中的数据没有清除。只有标记告诉您可以将数据写入 Buffer 的位置。

当您调用 clear() 时,如果缓冲区中有任何未读数据,则数据将被“遗忘”,这意味着您不再有任何标记来说明哪些数据已被读取,哪些数据未被读取。

如果 Buffer 中仍然有未读数据,并且您希望稍后读取它,但是您需要先写一些东西,那么调用 compact() 而不是 clear().

compact() 将所有未读的数据复制到 Buffer 的开头。然后将 position 设置为最后一个未读元素之后的位置。与 clear() 一样,limit 属性仍然设置为 capacity。现在 Buffer 已经准备好写入,但是不会覆盖未读数据。

mark() and reset()

You can mark a given position in a Buffer by calling the Buffer.mark() method. You can then later reset the position back to the marked position by calling the Buffer.reset() method. Here is an example:

buffer.mark(); //call buffer.get() a couple of times, e.g. during parsing. buffer.reset();  //set position back to mark.

equals() and compareTo()

It is possible to compare two buffers using equals() and compareTo().

equals()

Two buffers are equal if:

  1. They are of the same type (byte, char, int etc.)

  2. They have the same amount of remaining bytes, chars etc. in the buffer.

  3. All remaining bytes, chars etc. are equal.

As you can see, equals only compares part of the Buffer, not every single element inside it. In fact, it just compares the remaining elements in the Buffer.

compareTo()

The compareTo() method compares the remaining elements (bytes, chars etc.) of the two buffers, for use in e.g. sorting routines. A buffer is considered “smaller” than another buffer if:

  1. The first element which is equal to the corresponding element in the other buffer, is smaller than that in the other buffer.

  2. All elements are equal, but the first buffer runs out of elements before the second buffer does (it has fewer elements).

mark() and reset()

你可以调用 Buffer.mark() 方法在 Buffer 中标记给定位置。之后,你可以调用 Buffer.reset() 方法重置回标记的这个位置。下面是个例子:

buffer.mark(); //call buffer.get() a couple of times, e.g. during parsing. buffer.reset();  //set position back to mark.

equals() and compareTo()

equals() 和 compareTo()方法可以比较两个缓冲区。

equals()

两个缓冲相同,如果:

  1. 他们是同一个类型(byte,char,int等)。

  2. 在缓冲区,它们遗留有相同量的字节、字符等。

  3. 所有遗留的字节、字符都相同。

正如你看到的,equals只比较Buffer的一部分,而不是每个元素。事实上,它只比较Buffer中遗留的元素。

compareTo()

用 compareTo() 方法比较两个缓冲区的遗留元素(字节、字符等),用在例如排序例程。在下列情况中,一个缓冲区被视为“小于”另一个缓冲区,如果:

  1. 与另一个缓冲区对应元素相等的第一个元素,小于另一个缓冲区的元素。

  2. 所有的元素都相等,但是第一个缓冲区在第二个缓冲区之前耗尽了元素(它有更少的元素)。


via:oschina

转载请注明:AspxHtml学习分享网 » [中英对照]Java NIO Buffer 详解

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址