Import Java.nio.ByteBuffer;
public class bytebufferdemo01{
public static void Main (String args[]) {
Bytebuffer buf = Bytebuffer.allocatedirect (10); Prepare a buffer of 10 sizes
byte temp[] = {1,3,5,7,9}; Set content
Buf.put (temp); Set up a set of content
Buf.flip ();
System.out.print ("content in the main buffer:");
while (Buf.hasremaining ()) {
int x = Buf.get ();
System.out.print (x + ",");
}
}
}
//***
Import Java.nio.IntBuffer;
public class intbufferdemo01{
public static void Main (String args[]) {
Intbuffer buf = intbuffer.allocate (10); Prepare a buffer of 10 sizes
System.out.print ("1, position, limit and capacity before writing the data:");
System.out.println ("position =" + buf.position () + ", limit =" + buf.limit () + ", Capacty =" + buf.capacity ());
int temp[] = {5,7,9};//define an int array
Buf.put (3); Set up a data
Buf.put (temp); At this point, four records have been stored
System.out.print ("2, position, limit and capacity after writing the data:");
System.out.println ("position =" + buf.position () + ", limit =" + buf.limit () + ", Capacty =" + buf.capacity ());
Buf.flip (); Resetting buffers
postion = 0, limit = original position
System.out.print ("3, position, limit and capacity when preparing the output data:");
System.out.println ("position =" + buf.position () + ", limit =" + buf.limit () + ", Capacty =" + buf.capacity ());
System.out.print ("Contents in buffer:");
while (Buf.hasremaining ()) {
int x = Buf.get ();
System.out.print (x + ",");
}
}
}//***
Import Java.nio.IntBuffer;
public class intbufferdemo02{
public static void Main (String args[]) {
Intbuffer buf = intbuffer.allocate (10); Prepare a buffer of 10 sizes
Intbuffer sub = null; Defining child buffers
for (int i=0;i<10;i++) {
Buf.put (2 * i + 1); Add 10 odd numbers to the main buffer
}
Need to create a sub-buffer via slice ()
Buf.position (2);
Buf.limit (6);
Sub = Buf.slice ();
for (int i=0;i<sub.capacity (); i++) {
int temp = Sub.get (i);
Sub.put (temp-1);
}
Buf.flip (); Resetting buffers
Buf.limit (Buf.capacity ());
System.out.print ("content in the main buffer:");
while (Buf.hasremaining ()) {
int x = Buf.get ();
System.out.print (x + ",");
}
}
}//****************************
Import Java.nio.IntBuffer;
public class intbufferdemo03{
public static void Main (String args[]) {
Intbuffer buf = intbuffer.allocate (10); Prepare a buffer of 10 sizes
Intbuffer read = null; Defining child buffers
for (int i=0;i<10;i++) {
Buf.put (2 * i + 1); Add 10 odd numbers to the main buffer
}
Read = Buf.asreadonlybuffer ();//create read-only buffer
Read.flip (); Resetting buffers
System.out.print ("content in the main buffer:");
while (Read.hasremaining ()) {
int x = Read.get ();
System.out.print (x + ",");
}
Read.put (30); Modify, Error
}
}
Java New IO "_ Buffer with Buffer\ code