New buffers are created by either allocation or wrapping. Wrapping creates a buffer object but does not allocate any space to hold the data elements. It uses the array you provide as backing storage to hold the data elements of the buffer. If you want to provide your own array to be used as the buffer’s backing store, call the wrap( ) method :


This constructs a new buffer object, but the data elements will live in the array. This implies that changes made to the buffer by invoking put( ) will be reflected in the array, and any changes made directly to the array will be visible to the buffer object.


wrapping output

Program Source

import java.nio.ByteBuffer;

public class Javaapp {

    public static void main(String[] args) {

        byte by[] = new byte[]{10,20,30,40,50,60,70,80,90,100};
        ByteBuffer buf = ByteBuffer.wrap(by);
        buf.put(new byte[]{110,115,120,125});
        for(byte d : by) 
            System.out.print(d+", ");

Leave a Comment