Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Keep The TechnoWorldInc.com Community Clean: Read Guidelines Here.
Recent Updates
[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 08, 2024, 10:26:18 AM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » JAVA
 Java: Working With Buffers
Pages: [1]   Go Down
  Print  
Author Topic: Java: Working With Buffers  (Read 3148 times)
Admin
Administrator
Adv. Member
*****



Karma: 208
Offline Offline

Posts: 496

TWI Admin

159511729 vatsal2002 superwebchampz
View Profile WWW
Java: Working With Buffers
« Posted: January 03, 2007, 12:14:58 AM »


Working with buffers:
Buffers can be very useful in java since they can speed io operations a lot. Basically a buffer is a space allocated into memory for Bytes, Chars, and other data types to be entered. I have found Buffers really useful when writing Client/ Server applications.
You create a Buffer like this:

Code:
/* 
* Buffer size
*/
int BUFFER_SIZE = 100;

/*
* Allocates a ByteBuffer
* with a size of a 100
*/
ByteBuffer byteBuffer = ByteBuffer.allocate(BUFFER_SIZE);

/*
* Makes an IntBuffer
*/
IntBuffer intBuffer = IntBuffer.allocate(BUFFER_SIZE);

/*
* Makes a Direct CharBuffer
*/
CharBuffer charBuffer = CharBuffer.allocateDirect(BUFFER_SIZE);


The way the Buffer works it has three different markers: position, limit and capacity.

Capacity: Capacity is the number set with the allocate(BUFFER_SIZE). That is basically how big the buffer is.

Limit: The Limit of the Buffer is the index of the first element that should not be read or written in the buffer. The limit of the buffer cannot be negative or greater that it’s capacity.

Position: The position of the Buffer is the place that the buffer is the next element to be read/ written.

The Different methods that edit the buffer just move those markers around. There are four methods that you should familiarize yourself with:

Buffer.clear();
Buffer.compact();
Buffer.flip();
Buffer.rewind();

clear(); The clear method when applied to a buffer sets the limit to the capacity and the position the 0. All that that means that when new data is added to the buffer it will overwrite the old data.

compact(); The compact method moves the elements between the current position and the limit to the begging of the buffer.

flip(); The flip method need to be called before reading the date from the buffer. When a flip is called the limit is set to the current position, and the position to 0.

rewind(); The rewind method sets the position to zero again in case you want to make the buffer ready for another draining. You would need to flip the buffer first though.

Direct vs. nonDirect buffers: Buffers can be either direct or nonDirect.
Direct: creating a direct buffer simply means that the buffer is allocated inside the native data structure. That means that data can be transferred to native resources without having to go trough the java data structure. That can heave a really good impact on performance.

NonDirect: if you create a buffer that will not interact with native resource (ex. Just to store a String) you should use a NonDirect Buffer.

Adding to a Buffer: when adding data to a buffer you can use the wrap() method;


Code:
String string = "Text to be added"; 
CharBuffer charBuffer = CharBuffer.allocate(string.length());
charBuffer.wrap(string);

Adding to a Buffer: When adding data to a buffer you can use the wrap() method. Note that when a buffer is created by wrapping it are never direct.

Code:
/* 
*  wraps a string inside an buffer.
*/
String string = "Text to be added";
CharBuffer charBuffer = CharBuffer.allocate(string.length());
charBuffer.wrap(string);

or you could wrap entire blocks of data in a form of an array:

Code:
/* 
* takes a byte array and wraps it into a buffer.
*/
byte[] data = “Text to be added”.getBytes(“UTF-8”);
ByteBuffer buffer1 = ByteBuffer.wrap(data);

Draining a Buffer: Buffers can be drained into any data type:

Code:
/* 
* uses the get() method to fill a string.
*/
String fromBuffer = “”;
while (buffer.hasRemaining()) {
   fromBuffer += buffer.get();
}

Data Conversion: Data Conversion is an important aspect of buffers. You can use the factory methods to change a buffer from one type of another:

Code:
ByteBuffer byteBuffer = ByteBuffer.allocate(5); 
IntBuffer intBuffer = byteBuffer.asIntBuffer();

Here is a list of conversions:

asShortBuffer();

asCharBuffer();

asIntBuffer();

asLongBuffer();

asFloatBuffer();

asDoubleBuffer();



Buffer can be really useful to work with. When creating a server/client application buffer can significantly increase your performance.

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.196 seconds with 24 queries.