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(byte[] src, int off, int len) 

Source Link

Document

Writes bytes in the given byte array, starting from the specified offset, to the current position and increases the position by the number of bytes written.

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.put("java2s.com".getBytes(), 0, 2);
    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.put("java2s.com".getBytes(), 0, 2);

    ByteBuffer bbuf1 = ByteBuffer.allocate(10);
    bbuf1.put(bbuf);//from   w w  w . j a v a2  s  . c  o m
    System.out.println(Arrays.toString(bbuf1.array()));
}

From source file:com.openteach.diamond.network.waverider.network.Packet.java

public static void main(String[] args) {
    /*System.out.println("[DEBUG] Packet Header size = " + getHeaderSize());
            /*from www. j a  va  2  s  . com*/
    SlaveState slaveState = new SlaveState();
    slaveState.setId(1L);
    slaveState.setIsMasterCandidate(false);
    Command command = CommandFactory.createHeartbeatCommand(slaveState.toByteBuffer());
    Packet packet = Packet.newDataPacket(command);
    ByteBuffer buffer = packet.marshall();
    Packet p = Packet.unmarshall(buffer);
    Command cmd = Command.unmarshall(p.getPayLoad());
    SlaveState ss = SlaveState.fromByteBuffer(cmd.getPayLoad());
    System.out.println(cmd.toString());
            
            
    // Test 2
    MasterState masterState = new MasterState();
    masterState.setId(1L);
    masterState.setIp("127.0.0.1");
    masterState.setPort(8206);
    List<SessionState> sessionStateList = new LinkedList<SessionState>();
    masterState.setSessionStateList(sessionStateList);
    SessionState sessionState = null;
    for(int i = 0; i < 10; i++) {
       sessionState = new SessionState();
       sessionState.setIp("127.0.0.1");
       sessionState.setPriority(1L);
       sessionState.setIsMasterCandidate(false);
       sessionStateList.add(sessionState);
    }
    Command command2 = CommandFactory.createHeartbeatCommand(masterState.toByteBuffer());
    Packet packet2 = Packet.newDataPacket(command2);
    ByteBuffer buffer2 = packet2.marshall();
    Packet p2 = Packet.unmarshall(buffer2);
    Command cmd2 = Command.unmarshall(p2.getPayLoad());
    MasterState ms = MasterState.fromByteBuffer(cmd2.getPayLoad());
    System.out.println(cmd.toString());
    */

    System.out.println("[DEBUG] Packet Header size = " + getHeaderSize());

    BlockingQueue<ByteBuffer> queue = new LinkedBlockingQueue<ByteBuffer>();
    Random rand = new Random();
    int count = 0;
    int size = 0;
    for (int i = 0; i < 100000; i++) {
        SlaveState state = new SlaveState();
        state.setId(Long.valueOf(i));
        state.setIsMasterCandidate(true);
        Packet packet = Packet.newDataPacket(
                CommandFactory.createCommand(Command.AVAILABLE_COMMAND_START, state.toByteBuffer()));
        ByteBuffer buffer = packet.marshall();
        rand.setSeed(System.currentTimeMillis());
        count = rand.nextInt(100) + 1;
        size = buffer.remaining() / count;
        for (int j = 0; j < count; j++) {
            ByteBuffer buf = null;
            if (j == count - 1) {
                buf = ByteBuffer.allocate(buffer.remaining() - j * size);
                buf.put(buffer.array(), j * size, buffer.remaining() - j * size);
            } else {
                buf = ByteBuffer.allocate(size);
                buf.put(buffer.array(), j * size, size);
            }
            buf.flip();
            queue.add(buf);
        }
    }

    for (int i = 0; i < 100000; i++) {
        //Packet packet = Packet.parse(queue);
        //Command commad = Command.unmarshall(packet.getPayLoad());
        //SlaveState state = SlaveState.fromByteBuffer(commad.getPayLoad());
        //System.out.println(state.toString());
    }
}

From source file:Main.java

public static char[] getChars(byte[] buffer, int offset, int length) {

    ByteBuffer bb = ByteBuffer.allocate(length);
    bb.put(buffer, offset, length);
    bb.flip();// w w  w.j  a  v a 2s  . c  om

    return Charset.defaultCharset().decode(bb).array();
}

From source file:Main.java

/**
 * Converts an array of 4 bytes into a int.
 *
 * @param bytes The bytes./*from  w  w w .  j ava2s.  c om*/
 * @return The int.
 */
public static int bytesToInt(final byte[] bytes) {
    final ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.put(bytes, 0, 4);
    buffer.flip();
    return buffer.getInt();
}

From source file:Main.java

/**
 * Converts an array of 8 bytes into a long.
 *
 * @param bytes The bytes./*  w  ww .j a  va  2 s. c om*/
 * @return The long.
 */
public static long bytesToLong(final byte[] bytes) {
    final ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.put(bytes, 0, 8);
    buffer.flip();
    return buffer.getLong();
}

From source file:Main.java

public static long bytesToLong(byte[] bytes) {
    long l = 0;/* w w  w.ja  va  2  s  .com*/
    try {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.put(bytes, 0, bytes.length);
        buffer.flip();//need flip
        l = buffer.getLong();
    } catch (Exception e) {

    }
    return l;
}

From source file:Main.java

public static int readInt(byte[] message, int start) {
    /* Read in the type */
    ByteBuffer typeBuffer = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN);
    typeBuffer.put(message, start, 4).position(0);
    return typeBuffer.getInt();
}

From source file:Main.java

public static int bytesToInt(byte[] bytes) {
    ByteBuffer buffer = ByteBuffer.allocate(4);
    reverseArray(bytes);//from   w  w  w .  j  a  v  a2s . c om
    buffer.put(bytes, 0, bytes.length);
    buffer.flip();// need flip
    return buffer.getInt();
}

From source file:Main.java

public static long bytesToLong(byte[] bytes) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    reverseArray(bytes);/*ww w  . j ava2s  .co  m*/
    buffer.put(bytes, 0, bytes.length);
    buffer.flip();// need flip
    return buffer.getLong();
}