Java Buffers

Java Buffers Print E-mail
Contributed by Joe   
Monday, 03 July 2006

Java buffer overview

A Java buffer is a linear, finite sequence of elements of a specific primitive type. Aside from its content, the essential properties of a buffer are its capacity, limit, and position:

A buffer's capacity is the number of elements it contains. The capacity of a buffer is never negative and never changes.

A buffer's limit is the index of the first element that should not be read or written. A buffer's limit is never negative and is never greater than the its capacity.

A buffer's position is the index of the next element to be read or written. A buffer's position is never negative and is never greater than its limit.

 

Buffer classes 

In java API all the classes that define buffers have the abstract class Buffer as a base. The Buffer class therefore defines the fundamental characteristics common to all buffers. A particular buffer can store a sequence of elements of a given type, and an element can be of any primitive data type other than boolean. Thus, you can create buffers to store byte values, char values, short values, int values, long values, float values, or double values. The following classes in the java.nio package define these buffers:

ByteBuffer A buffer that stores elements of type byte. You can also store the binary values of any of the other primitive types in this buffer, except for type boolean. Each binary value that you store will occupy a number of bytes in the buffer determined by the type – values of type char or short will occupy two bytes, int values will occupy four bytes, and so on. Only buffers of this type can be used in a file I/O operation.

CharBuffer A buffer that only stores elements of type char.

ShortBuffer A buffer that only stores elements of type short

IntBuffer   A buffer that only stores elements of type int.
 
LongBuffer  A buffer that only stores elements of type long.
 
FloatBuffer  A buffer that only stores elements of type float.
 
DoubleBuffer A buffer that only stores elements of type double.

 Setting the Java Buffer Position and Limit


position(int newPosition)  Sets the position to the index value specified by the argument. The new position value must be greater than or equal to zero, and not greater than the current limit, otherwise an exception of type IllegalArgumentException will be thrown. If the buffer's mark is defined (we will come to the mark in the next section) and greater than the new position, it will be discarded.
 
limit(int newLimit)  Sets the limit to the index value specified by the argument. If the buffer's position is greater than the new limit it will be set to the new limit. If the buffer's mark is defined and exceeds the new limit it will be discarded. If the new limit value is negative or greater than the buffer's capacity, an exception of type IllegalArgumentException will be thrown.
 

Creating Buffers

ByteBuffer buf =  ByteBuffer.allocate(1024);

 View Buffers

ByteBuffer buf = ByteBuffer.allocate(1024);
IntBuffer intBuf = buf.asIntBuffer();   // Now create a view buffer

Creating Buffers by Wrapping Arrays

String str= "Hello,Hotdir.biz.";
byte[] array = str.getBytes();   // Get string as byte array
ByteBuffer buf = ByteBuffer.wrap(array);

Last Updated ( Monday, 03 July 2006 )

  home              contact us

 

©2006-2012 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground