Example usage for java.nio ByteBuffer allocate

List of usage examples for java.nio ByteBuffer allocate

Introduction

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

Prototype

public static ByteBuffer allocate(int capacity) 

Source Link

Document

Creates a byte buffer based on a newly allocated byte array.

Usage

From source file:com.esri.geoevent.test.performance.ClockSync.java

@Override
public void run() {
    DatagramSocket socket = null;
    try {/*  w  w  w.  j  av  a  2 s . c  o m*/
        byte[] incomingBuffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(incomingBuffer, incomingBuffer.length);

        ByteBuffer bb = ByteBuffer.allocate(8);
        DatagramPacket outgoingPacket = new DatagramPacket(bb.array(), 0, 8, null, port);
        socket = new DatagramSocket(port);
        socket.setSoTimeout(100);
        while (isRunning.get()) {
            try {
                socket.receive(packet);
                long now = System.currentTimeMillis();
                bb.putLong(now);
                outgoingPacket.setAddress(packet.getAddress());
                outgoingPacket.setPort(packet.getPort());
                socket.send(outgoingPacket);
                bb.clear();
                //System.out.println("Sent the time " + now);
            } catch (SocketTimeoutException ex) {
                // Do nothing if nothing was sent.
            }
        }
    } catch (BindException e) {
        // port is in use - increment and try again
        port++;
        this.run();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(socket);
    }
}

From source file:com.openteach.diamond.network.waverider.command.MasterGreetCommandHandler.java

private Command makeGreetCommand(Session session) {
    String hello = new StringBuilder("Hello slave:").append(session.getSlaveWorker().getIp()).toString();
    ByteBuffer buffer = ByteBuffer.allocate(hello.getBytes().length);
    buffer.put(hello.getBytes());/*  w w  w  .  jav a 2s  .  co m*/
    buffer.flip();

    Command command = new Command(1L, buffer);

    return command;
}

From source file:org.ardverk.daap.nio.DaapRequestReaderNIO.java

/** Creates a new instance of DaapRequestReader */
DaapRequestReaderNIO(DaapConnectionNIO connection) {

    this.connection = connection;

    in = ByteBuffer.allocate(MAX_HEADER_SIZE);
    in.clear();/*  w  ww .  j a va 2  s . co  m*/
    in.flip();

    lineReader = new DaapLineReaderNIO();
    headers = new ArrayList<Header>();
    pending = new LinkedList<DaapRequest>();
}

From source file:net.beaconpe.jraklib.protocol.Packet.java

protected byte[] get(int len) {
    if (len < 0) {
        offset = buffer.length - 1;/*ww  w.  j a  v a 2 s .c o m*/
        return new byte[] {};
    } else {
        ByteBuffer bb = ByteBuffer.allocate(len);
        while (len > 0) {
            bb.put(buffer[offset]);
            len = len - 1;
            if (len != 0) {
                offset = offset + 1;
            }
        }
        return bb.array();
    }
}

From source file:ConversionUtil.java

public static byte[] convertToByteArray(int value) {
    byte[] bytes = new byte[4];
    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    buffer.putInt(value);/*from w w w . j av  a  2  s.  c  o m*/
    return buffer.array();
    // buffer.get(bytes);
    /*
    for (int i =0;i<bytes.length; ++i) {
    int offset = (bytes.length -i-1) *8;
    bytes[i] = (byte)((value & (0xff << offset)) >>> offset);
    }
     */
    // return bytes;
}

From source file:net.jradius.freeradius.FreeRadiusRequest.java

public FreeRadiusRequest() {
    super();/*from   w ww  .  java  2 s. com*/

    buffer_in = ByteBuffer.allocate(25000);
    buffer_in.order(ByteOrder.BIG_ENDIAN);

    buffer_out = ByteBuffer.allocate(25000);
    buffer_out.order(ByteOrder.BIG_ENDIAN);
}

From source file:com.alibaba.otter.shared.common.utils.ByteUtils.java

public static long bytes2long(byte[] b) {
    ByteBuffer buf = ByteBuffer.allocate(8);
    buf.put(b);//from w  ww.  jav  a  2  s.c om
    buf.flip();
    return buf.getLong();
}

From source file:com.googlecode.jcimd.TextMessageUserDataFactory.java

private static byte[] encodeAs(Charset charset, String textMessage) {
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer byteBuffer = ByteBuffer
            .allocate(textMessage.length() * (int) Math.ceil(encoder.maxBytesPerChar()));
    encoder.encode(CharBuffer.wrap(textMessage), byteBuffer, true);
    byte[] bytes = new byte[byteBuffer.position()];
    byteBuffer.flip();/*from   w  ww.  j a  v a  2s .co  m*/
    byteBuffer.get(bytes);
    return bytes;
}

From source file:com.github.neoio.net.message.staging.memory.TestMemoryMessageStaging.java

@Test
public void test_tempWrite() {
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    buffer.put("Hello World".getBytes());
    buffer.rewind();//from   w w w .  jav a 2 s .c om
    staging.writeTempWriteBytes(buffer);
    Assert.assertTrue(staging.hasTempWriteBytes());

    buffer.clear();
    staging.readTempWriteBytes(buffer);
    Assert.assertEquals("Hello World",
            new String(ArrayUtils.subarray(buffer.array(), 0, "Hello World".getBytes().length)));
    staging.resetTempWriteBytes();

    Assert.assertFalse(staging.hasTempWriteBytes());
}

From source file:org.jmangos.sniffer.handler.PKTLogHandler.java

@Override
public void init() {
    final String date = String.format("%X", System.currentTimeMillis());
    final ByteBuffer buffer = ByteBuffer.allocate(66);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put("PKT".getBytes());
    buffer.put((byte) 1);
    buffer.put((byte) 3);
    buffer.put((byte) 12);
    buffer.putInt(this.build);
    buffer.put("xxXX".getBytes());
    buffer.put(this.keyReader.getKey());
    buffer.putInt((int) (System.currentTimeMillis() / 1000L));
    buffer.putInt(0);//from   w  ww  .  j  a v a 2  s.  c om
    buffer.putInt(0);
    try {
        this.fous = new DataOutputStream(new FileOutputStream(new File(this.build + "_" + date + ".pkt")));
        this.fous.write(buffer.array());
        setInit(true);
    } catch (final FileNotFoundException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    }
}