Example usage for java.nio ByteBuffer put

List of usage examples for java.nio ByteBuffer put

Introduction

In this page you can find the example usage for java.nio ByteBuffer put.

Prototype

public ByteBuffer put(ByteBuffer src) 

Source Link

Document

Writes all the remaining bytes of the src byte buffer to this buffer's current position, and increases both buffers' position by the number of bytes copied.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);

    bbuf.order(ByteOrder.LITTLE_ENDIAN);

    bbuf.put("java2s.com".getBytes());

    System.out.println(Arrays.toString(bbuf.array()));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);

    bbuf.order(ByteOrder.nativeOrder());

    bbuf.put("java2s.com".getBytes());

    System.out.println(Arrays.toString(bbuf.array()));
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    DatagramChannel channel = DatagramChannel.open();
    SocketAddress address = new InetSocketAddress(0);
    DatagramSocket socket = channel.socket();
    socket.bind(address);//from   w  w w.ja  v a  2s .  c om

    SocketAddress server = new InetSocketAddress("time-a.nist.gov", 37);
    channel.connect(server);

    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.order(ByteOrder.BIG_ENDIAN);
    // send a byte of data to the server
    buffer.put((byte) 0);
    buffer.flip();
    channel.write(buffer);

    // get the buffer ready to receive data
    buffer.clear();
    // fill the first four bytes with zeros
    buffer.putInt(0);
    channel.read(buffer);
    buffer.flip();

    // convert seconds since 1900 to a java.util.Date
    long secondsSince1900 = buffer.getLong();
    long differenceBetweenEpochs = 2208988800L;
    long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;
    long msSince1970 = secondsSince1970 * 1000;
    Date time = new Date(msSince1970);

    System.out.println(time);
}

From source file:ExplicitChannelWrite.java

public static void main(String args[]) {
    FileOutputStream fOut;//from www. ja  v  a  2s .  c o  m
    FileChannel fChan;
    ByteBuffer mBuf;

    try {
        fOut = new FileOutputStream("test.txt");
        fChan = fOut.getChannel();
        mBuf = ByteBuffer.allocateDirect(26);
        for (int i = 0; i < 26; i++)
            mBuf.put((byte) ('A' + i));
        mBuf.rewind();
        fChan.write(mBuf);
        fChan.close();
        fOut.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:MappedChannelWrite.java

public static void main(String args[]) {
    RandomAccessFile fOut;//  w  w  w. ja  v  a 2s .  c o m
    FileChannel fChan;
    ByteBuffer mBuf;

    try {
        fOut = new RandomAccessFile("test.txt", "rw");
        fChan = fOut.getChannel();
        mBuf = fChan.map(FileChannel.MapMode.READ_WRITE, 0, 26);
        for (int i = 0; i < 26; i++)
            mBuf.put((byte) ('A' + i));

        fChan.close();
        fOut.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    FileOutputStream fileOutputStream;
    FileChannel fileChannel;/*from  w w  w .  j  a v a 2s.co  m*/
    ByteBuffer byteBuffer;

    try {
        fileOutputStream = new FileOutputStream("test.txt");
        fileChannel = fileOutputStream.getChannel();
        byteBuffer = ByteBuffer.allocateDirect(26);

        for (int i = 0; i < 26; i++)
            byteBuffer.put((byte) ('A' + i));
        byteBuffer.rewind();
        fileChannel.write(byteBuffer);
        fileChannel.close();
        fileOutputStream.close();
    } catch (IOException exc) {
        System.out.println(exc);
    }
}

From source file:UDPTimeClient.java

public static void main(String[] args) throws Exception {

    DatagramChannel channel = DatagramChannel.open();
    // port 0 selects any available port
    SocketAddress address = new InetSocketAddress(0);
    DatagramSocket socket = channel.socket();
    socket.setSoTimeout(5000);// w  w  w  . java  2  s .c om
    socket.bind(address);

    SocketAddress server = new InetSocketAddress("time.nist.gov", 37);
    ByteBuffer buffer = ByteBuffer.allocate(8192);
    // time protocol always uses big-endian order
    buffer.order(ByteOrder.BIG_ENDIAN);
    // Must put at least one byte of data in the buffer;
    // it doesn't matter what it is.
    buffer.put((byte) 65);
    buffer.flip();

    channel.send(buffer, server);

    buffer.clear();
    buffer.put((byte) 0).put((byte) 0).put((byte) 0).put((byte) 0);
    channel.receive(buffer);
    buffer.flip();
    long secondsSince1970 = buffer.getLong();

    System.out.println(secondsSince1970);
    channel.close();

}

From source file:MainClass.java

public static void main(String args[]) {
    RandomAccessFile randomAccessFile;
    FileChannel fileChannel;//  ww w . j a  v a  2  s . c  o  m
    ByteBuffer byteBuffer;

    try {
        randomAccessFile = new RandomAccessFile("test.txt", "rw");
        fileChannel = randomAccessFile.getChannel();
        byteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 26);
        for (int i = 0; i < 10; i++)
            byteBuffer.put((byte) ('A' + i));
        fileChannel.close();
        randomAccessFile.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:com.talent.nio.communicate.receive.DecodeRunnable.java

/**
 * @param args//from   w  ww.j av  a  2s.c o m
 */
public static void main(String[] args) {
    byte[] bs1 = new byte[] { 1, 10, 11, 12 };
    byte[] bs2 = new byte[] { 2, 2, 2, 2 };
    byte[] bs3 = new byte[] { 3, 3, 3, 3 };
    byte[] bs4 = new byte[] { 4, 4, 4, 4 };
    byte[] bs5 = new byte[] { 5, 5, 5, 5 };
    byte[] bs6 = new byte[] { 6, 6, 6, 6 };

    ByteBuffer buffer1 = ByteBuffer.allocate(1024);
    buffer1.put(bs1);
    buffer1.flip();

    ByteBuf buf1 = Unpooled.copiedBuffer(buffer1);// .copiedBuffer(bs1);

    buffer1.put(bs3);

    ByteBuf buf2 = Unpooled.copiedBuffer(bs2);
    ByteBuf buf3 = Unpooled.copiedBuffer(bs3);
    ByteBuf buf4 = Unpooled.copiedBuffer(bs4);
    ByteBuf buf5 = Unpooled.copiedBuffer(bs5);
    ByteBuf buf6 = Unpooled.copiedBuffer(bs6);

    CompositeByteBuf cb = Unpooled.compositeBuffer();
    cb.addComponents(buf1, buf2, buf3);

    byte dd = cb.getByte(0);

    CompositeByteBuf cb2 = Unpooled.compositeBuffer();
    cb.addComponents(buf4, buf5, buf6);

    // cb.c
    // cb2.writerIndex(128 * 1024);

    cb.addComponent(cb2);

    Long number = cb2.readLong(); // causes IllegalBufferAccessException
                                  // here!

}

From source file:com.openteach.diamond.network.waverider.session.DefaultSession.java

public static void main(String[] args) {

    BlockingQueue<ByteBuffer> inputBuffer = new LinkedBlockingQueue<ByteBuffer>();
    /*for (int i = 0; i < 10; i++)
    {*//* ww w  .  ja v a 2  s  .  c om*/
    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    byteBuffer.put(makePacket().marshall());
    byteBuffer.put(makePacket().marshall());
    byteBuffer.flip();
    byte[] b = new byte[8];
    ByteBuffer halfBuf0 = ByteBuffer.allocate(8);
    byteBuffer.get(b);
    halfBuf0.put(b);
    halfBuf0.flip();
    inputBuffer.add(halfBuf0);
    inputBuffer.add(byteBuffer);
    /*}*/

    int size = 0;
    int oldSize = size;
    long length = Packet.getHeaderSize();
    ByteBuffer buffer = ByteBuffer.allocate(NetWorkConstants.DEFAULT_NETWORK_BUFFER_SIZE);
    ByteBuffer currentBuffer = null;

    while (size < length) {
        currentBuffer = inputBuffer.peek();
        oldSize = size;
        int position = currentBuffer.position();
        size += currentBuffer.remaining();
        buffer.put(currentBuffer);
        if (size >= Packet.getHeaderSize()) {
            length = buffer.getLong(Packet.getLengthPosition());
        }

        if (size <= length) {
            inputBuffer.remove();
        } else {
            currentBuffer.position(position);
            buffer.position(buffer.position() - currentBuffer.remaining());
            byte[] buf = new byte[(int) (length - oldSize)];
            currentBuffer.get(buf);
            buffer.put(buf);
        }
    }

    // buffer.position(0);
    buffer.flip();
    Packet packet = Packet.unmarshall(buffer);

    Command command = CommandFactory.createCommand(packet.getType(), packet.getPayLoad());

    String str = new String(command.getPayLoad().array());

    System.out.println(str);

}