Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

FromBufferare derived the following specific buffer classes, which hold the type of
data that their names imply:


ByteBuffer CharBuffer DoubleBuffer FloatBuffer
IntBuffer LongBuffer MappedByteBuffer ShortBuffer

MappedByteBufferis a subclass ofByteBufferthat is used to map a file to a buffer.
All buffers support variousget( )andput( )methods, which allow you to get data from
a buffer or put data into a buffer. For example, Table 27-3 shows theget( )andput( )methods
defined byByteBuffer. (The other buffer classes have similar methods.) All buffer classes
also support methods that perform various buffer operations. For example, you can allocate
a buffer manually usingallocate( ). You can wrap an array inside a buffer usingwrap( ).You
can create a subsequence of a buffer usingslice( ).


Chapter 27: NIO, Regular Expressions, and Other Packages 817


Method Description
abstract byte get( ) Returns the byte at the current position.
ByteBuffer get(bytevals[ ] ) Copies the invoking buffer into the array referred to by
vals. Returns a reference to the buffer.
ByteBuffer get(bytevals[ ], intstart,
intnum)

Copiesnumelements from the invoking buffer into
the array referred to byvals,beginning at the index
specified bystart.Returns a reference to the buffer.
If there are notnumelements remaining in the buffer,
aBufferUnderflowExceptionis thrown.
abstract byte get(intidx) Returns the byte at the index specified byidxwithin
the invoking buffer.
abstract ByteBuffer put(byteb) Copiesbinto the invoking buffer at the current position.
Returns a reference to the buffer.
final ByteBuffer put(byte vals[ ] ) Copies all elements ofvalsinto the invoking buffer,
beginning at the current position. Returns a reference
to the buffer.
ByteBuffer put(bytevals[ ], intstart,
intnum)

Copiesnumelements fromvals,beginning atstart,
into the invoking buffer. Returns a reference to the
buffer. If the buffer cannot hold all of the elements,
aBufferOverflowExceptionis thrown.
ByteBuffer put(ByteBuffer bb) Copies the elements inbbto the invoking buffer,
beginning at the current position. If the buffer cannot
hold all of the elements, aBufferOverflowException
is thrown. Returns a reference to the buffer.
abstract ByteBuffer put(intidx, byteb) Copiesbinto the invoking buffer at the location
specified byidx.Returns a reference to the buffer.

TABLE 27-3 Theget( )andput( )Methods Defined forByteBuffer

Free download pdf