Duplication

It is sometimes helpful to duplicate a buffer. A duplicate is not a copy or a clone. Rather, it is a new buffer object that has the same internal data as the original buffer, but an independent mark, limit, and position. Changes to the elements in the original bufferthat is, putting data in the bufferaffect the duplicate, and vice versa. However, getting data from one buffer, or flipping, resetting, rewinding, or clearing it, has no effect on the other. Duplicates are often useful when you want to pass the same fixed content to several different operations that run simultaneously or independently.

This is the duplicate method for ByteBuffer:

public abstract ByteBuffer duplicate( )

As usual, each of the seven buffer classes has its own duplicate method that differs primarily in return type. For example, this is the duplicate method for IntBuffer:

public abstract IntBuffer duplicate( )

When the duplicate is created, its position and mark are set to 0 and its limit is set to the capacity, regardless of the position, mark, and limit in the original buffer.

Категории