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:edu.umass.cs.reconfiguration.reconfigurationutils.ReconfigurationPacketDemultiplexer.java

@Override
public JSONObject processHeader(byte[] msg, NIOHeader header) {
    long t = System.nanoTime();
    int type;//from   ww  w  .  ja v a 2  s .  c om
    JSONObject json = null;
    log.log(Level.FINEST, "{0} processHeader received message with header {1}", new Object[] { this, header });
    if (msg.length >= Integer.BYTES
            && ReconfigurationPacket.PacketType.intToType
                    .containsKey(type = ByteBuffer.wrap(msg, 0, 4).getInt())
            && JSONPacket.couldBeJSON(msg, Integer.BYTES)
            && type != PacketType.REPLICABLE_CLIENT_REQUEST.getInt()) {
        json = super.processHeader(msg, Integer.BYTES, header, true);
        if (json != null) {
            if (Util.oneIn(50))
                DelayProfiler.updateDelayNano("processHeader", t);
            return json;
        }
    } else if (!BYTEIFICATION && JSONPacket.couldBeJSON(msg)
            && (json = super.processHeader(msg, header, true)) != null) {
        return json;
    }
    // else prefix msg with addresses
    byte[] stamped = new byte[NIOHeader.BYTES + msg.length];
    ByteBuffer bbuf = ByteBuffer.wrap(stamped);
    bbuf.put(header.toBytes());
    bbuf.put(msg);
    if (Util.oneIn(50))
        DelayProfiler.updateDelayNano("processHeader", t);
    return new JSONMessenger.JSONObjectWrapper(stamped);
}

From source file:io.dyn.net.nio.BufferByteChannel.java

@Override
public int read(ByteBuffer dst) throws IOException {
    int start = buffer.position();
    if (null != buffer.byteBuffer()) {
        dst.put(buffer.byteBuffer());
    }//w w  w.  j av a 2 s .c o  m
    return buffer.position() - start;
}

From source file:com.github.cambierr.lorawanpacket.semtech.TxAck.java

@Override
public void toRaw(ByteBuffer _raw) throws MalformedPacketException {
    super.toRaw(_raw);
    _raw.put(new JSONObject().put("txpk_ack", new JSONObject().put("error", error.name())).toString()
            .getBytes());/*from  w ww  .  j av  a  2s .c o m*/
}

From source file:edu.csun.ecs.cs.multitouchj.ui.control.Canvas.java

protected ByteBuffer prepareImage(Image image) {
    ByteBuffer imageData = ByteBuffer.allocateDirect(image.getData().length).order(ByteOrder.nativeOrder());
    imageData.put(image.getData());
    imageData.flip();//from w ww . j a  v a 2  s .  co  m

    return imageData;
}

From source file:com.offbynull.portmapper.pcp.PcpOption.java

/**
 * Dump this PCP option in to a byte buffer.
 * @param dst byte buffer to dump to//from   w  ww  . ja  va2s . c om
 * @throws NullPointerException if {@code dst} is {@code null}
 * @throws BufferOverflowException if {@code dst} doesn't have enough space to write this option
 * @throws ReadOnlyBufferException if {@code dst} is read-only
 */
final void dump(ByteBuffer dst) {
    Validate.notNull(data);

    dst.put((byte) code);
    dst.put((byte) 0); // reserved
    dst.putShort((short) length);
    dst.put(data.asReadOnlyBuffer());
}

From source file:com.offbynull.portmapper.pcp.MapPcpRequest.java

@Override
protected void dumpOpCodeSpecificInformation(ByteBuffer dst) {
    dst.put(mappingNonce.asReadOnlyBuffer());
    dst.put((byte) protocol);

    for (int i = 0; i < 3; i++) { // reserved block
        dst.put((byte) 0);
    }//from  ww  w . ja v a  2  s. c  om

    dst.putShort((short) internalPort);
    dst.putShort((short) suggestedExternalPort);
    dst.put(NetworkUtils.convertToIpv6Array(suggestedExternalIpAddress));
}

From source file:net.socket.nio.TimeClientHandle.java

private void doWrite(SocketChannel sc) throws IOException {
    byte[] req = "QUERY TIME ORDER".getBytes();
    ByteBuffer writeBuffer = ByteBuffer.allocate(req.length);
    writeBuffer.put(req);
    writeBuffer.flip();//from   w  ww  . ja v  a  2s.  com
    sc.write(writeBuffer);
    sc.write(writeBuffer);
    if (!writeBuffer.hasRemaining()) {
        System.out.println("Send order 2 server succeed.");
    }
}

From source file:net.jenet.Header.java

public void toBuffer(ByteBuffer buffer) {
    buffer.putShort(peerID);//from   w  ww. ja v a  2  s. c o  m
    buffer.put(flags);
    buffer.put(commandCount);
    buffer.putInt(sentTime);
    buffer.putInt(challenge);
}

From source file:com.mapr.storm.test.TailSpoutTest.java

private byte[] payload(String msg) {
    byte[] content = msg.getBytes(Charsets.UTF_8);
    ByteBuffer buf = ByteBuffer.allocate(4 + content.length);
    buf.putInt(content.length);/* w  w w  .j  av a  2s  . co m*/
    buf.put(content);
    buf.flip();
    return buf.array();
}

From source file:com.netflix.aegisthus.pig.AegisthusLoadCaster.java

private long getNumber(byte[] arg0) throws Exception {
    byte[] by = hex.decode(arg0);
    ByteBuffer bb = ByteBuffer.allocate(by.length);
    bb.put(by);
    bb.position(0);//from  w w  w  . j a v  a2 s  .com
    switch (by.length) {
    case 1:
        return (long) bb.get();
    case 2:
        return (long) bb.getShort();
    case 4:
        return (long) bb.getInt();
    case 8:
        return (long) bb.getLong();
    }
    throw new UnexpectedException("couldn't determine datatype");
}