Java New IO "_ Buffer with Buffer\ code

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.