Java Tutorial - Java Script : Buffers

Java Tutorial - Java Script :

Buffers

The java.nio package includes support for buffers, objects that represent data streams stored in memory. Buffers are often used to improve the performance of programs that read input or write output. They enable a program to put a lot of data in memory, where it can be read, written, and modified more quickly. A buffer corresponds with each of the primitive data types in Java:
·         ByteBuffer
·         CharBuffer
·         DoubleBuffer
·         FloatBuffer
·         IntBuffer
·         LongBuffer
·         ShortBuffer
Each of these classes has a static method called wrap() that can be used to create a buffer from an array of the corresponding data type. The only argument to the method should be the array. For example, the following statements create an array of integers and an IntBuffer that holds the integers in memory as a buffer:
int[] temperatures = { 90, 85, 87, 78, 80, 75, 70, 79, 85, 92, 99 };
IntBuffer tempBuffer = IntBuffer.wrap(temperatures);
A buffer keeps track of how it is used, storing the position where the next item will be read or written. After the buffer is created, its get() method reads the data at the current position in the buffer. The following statements extend the previous example and display everything in the integer buffer:
for (int i = 0; tempBuffer.remaining() > 0; i++)
System.out.println(tempBuffer.get());
Another way to create a buffer is to set up an empty buffer and then put data into it. To create the buffer, call the static method allocate(int) of the desired buffer class with the size of the buffer as an argument. You can use five put() methods to store data in a buffer (or replace the data already there). The arguments used with these methods depend on the kind of buffer you’re working with. For an integer buffer:
put(int)—Stores the integer at the current position in the buffer and then increments the position.
·         put(int, int)—Stores an integer (the second argument) at a specific position in the buffer (the first argument).
·         put(int[])—Stores all the elements of the integer array in the buffer, beginning at  the first position of the buffer.
·         put(int[], int, int)—Stores all or a portion of an integer array in the buffer.The second argument specifies the position in the buffer where the first integer in the array should be stored. The third argument specifies the number of elements from the array to store in the buffer.
·         put(IntBuffer)—Stores the contents of an integer buffer in another buffer, beginning at the first position of the buffer.
As you put data in a buffer, you must often keep track of the current position so that you know where the next data will be stored. To find out the current position, call the buffer’s position() method. An integer is returned that represents the position. If this value is 0, you’re at the start of the buffer. Call the position(int) method to change the position to the argument specified as an integer. Another important position to track when using buffers is the limit—the last place in t e buffer that contains data. It isn’t necessary to figure out the limit when the buffer is always full; in that case, you know the last position of the buffer has something in it.
However, if there’s a chance your buffer might contain less data than you have allocated, you should call the buffer’s flip() method after reading data into the buffer. This sets the current position to the start of the data you just read and sets the limit to the end. Later today, you’ll use a byte buffer to store data loaded from a web page on the Internet. This is a place where flip() becomes necessary because you don’t know how much data the page contains when you request it. If the buffer is 1,024 bytes in size and the page contains 1,500 bytes, the first attempt to read data loads the buffer with 1,024 bytes, filling it. The second attempt to read data loads the buffer with only 476 bytes, leaving the rest empty. If you call flip() afterward, the current position is set to the beginning of the buffer, and the limit is set to 476. The following code creates an array of Fahrenheit temperatures, converts them to Celsius, and then stores the Celsius values in a buffer:
int[] temps = { 90, 85, 87, 78, 80, 75, 70, 79, 85, 92, 99 };
IntBuffer tempBuffer = IntBuffer.allocate(temperatures.length);
for (int i = 0; i < temps.length; i++) {
float celsius = ( (float)temps[i] - 32 ) / 9 * 5;
tempBuffer.put( (int)celsius );
};
tempBuffer.position(0);
for (int i = 0; tempBuffer.remaining() > 0; i++)
System.out.println(tempBuffer.get());
After the buffer’s position is set back to the start, the contents of the buffer are displayed. Byte Buffers You can use the buffer methods introduced so far with byte buffers, but byte buffers also offer additional useful methods. For starters, byte buffers have methods to store and retrieve data that isn’t a byte:
·         putChar(char)—Stores 2 bytes in the buffer that represent the specified char value
·         putDouble(double)—Stores 8 bytes in the buffer that represent a double value
·         putFloat(float)—Stores 4 bytes in the buffer that represent a float value
·         putInt(int)—Stores 4 bytes in the buffer that represent an int value
·         putLong(long)—Stores 8 bytes in the buffer that represent a long value
·         putShort(short)—Stores 2 bytes in the buffer that represent a short value
Each of these methods puts more than one byte in the buffer, moving the current position forward the same number of bytes. There also are methods to retrieve nonbytes from a byte buffer: getChar(), getDouble(), getFloat(), getInt(), getLong(), and getShort().