Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

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

Prototype

public abstract byte get(int index);

Source Link

Document

Returns the byte at the specified index and does not change the position.

Usage

From source file:de.dfki.kiara.jsonrpc.JsonRpcMessage.java

private static JsonNode readFromBuffer(JsonRpcProtocol protocol, ByteBuffer data) throws IOException {
    byte[] array;
    int arrayOffset;
    int arrayLength;
    int oldPos = data.position();
    if (data.hasArray()) {
        array = data.array();// w w  w .ja va 2s . c  o  m
        arrayOffset = data.arrayOffset();
        arrayLength = data.remaining();
    } else {
        array = new byte[data.remaining()];
        data.get(array);
        arrayOffset = 0;
        arrayLength = array.length;
    }
    data.position(oldPos);

    JsonNode node;
    try (JsonParser parser = protocol.getObjectReader().getFactory().createParser(array, arrayOffset,
            arrayLength)) {
        node = parser.readValueAsTree();
    }
    return node;
}

From source file:com.googlecode.mp4parser.boxes.microsoft.XtraBox.java

private static void dumpByteBuffer(ByteBuffer input)
        throws ArrayIndexOutOfBoundsException, IllegalArgumentException, IOException {
    input = input.slice();//from  www.j a v a 2s.  c o  m
    byte bytes[] = new byte[input.remaining()];
    input.get(bytes);
    HexDump.dump(bytes, 0, System.out, 0);
}

From source file:edu.umass.cs.nio.MessageNIOTransport.java

/**
 * @param bytes//from   ww w . j a v a  2s.c  o m
 * @return Sender InetSocketAddress from bytes
 * @throws UnknownHostException
 */
public static InetSocketAddress getSenderAddress(byte[] bytes) throws UnknownHostException {
    ByteBuffer bbuf = ByteBuffer.wrap(bytes, 0, NIOHeader.BYTES);
    byte[] addressBytes = new byte[4];
    bbuf.get(addressBytes);
    InetAddress address = InetAddress.getByAddress(addressBytes);
    int port = (int) bbuf.getShort();
    if (port < 0)
        port += 2 * (Short.MAX_VALUE + 1);
    return new InetSocketAddress(address, port);
}

From source file:edu.umass.cs.nio.MessageNIOTransport.java

/**
 * @param bytes//  www .  ja va 2  s . c o  m
 * @return Sender InetSocketAddress from bytes
 * @throws UnknownHostException
 */
public static InetSocketAddress getReceiverAddress(byte[] bytes) throws UnknownHostException {
    ByteBuffer bbuf = ByteBuffer.wrap(bytes, 6, NIOHeader.BYTES);
    byte[] addressBytes = new byte[4];
    bbuf.get(addressBytes);
    InetAddress address = InetAddress.getByAddress(addressBytes);
    int port = (int) bbuf.getShort();
    if (port < 0)
        port += 2 * (Short.MAX_VALUE + 1);
    return new InetSocketAddress(address, port);
}

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

public static byte[] parseMessageResponse(byte[] resp) throws BufferUnderflowException {
    ByteBuffer buffer = ByteBuffer.wrap(resp);
    byte[] encfile = null;

    int errCode = buffer.getInt();
    if (errCode != 1)
        return null;

    int fileLen = buffer.getInt();
    encfile = new byte[fileLen];
    try {//from  ww w. j a v  a2s .c  o m
        buffer.get(encfile);
        return encfile;
    } catch (BufferUnderflowException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:io.mycat.util.ByteBufferUtil.java

/**
 * ByteBuffer adaptation of org.apache.commons.lang3.ArrayUtils.lastIndexOf method
 *
 * @param buffer the array to traverse for looking for the object, may be <code>null</code>
 * @param valueToFind the value to find//from   w  w  w  .j av  a2  s . co m
 * @param startIndex the start index (i.e. BB position) to travers backwards from
 * @return the last index (i.e. BB position) of the value within the array
 * [between buffer.position() and buffer.limit()]; <code>-1</code> if not found.
 */
public static int lastIndexOf(ByteBuffer buffer, byte valueToFind, int startIndex) {
    assert buffer != null;

    if (startIndex < buffer.position()) {
        return -1;
    } else if (startIndex >= buffer.limit()) {
        startIndex = buffer.limit() - 1;
    }

    for (int i = startIndex; i >= buffer.position(); i--) {
        if (valueToFind == buffer.get(i)) {
            return i;
        }
    }

    return -1;
}

From source file:net.servicestack.client.Utils.java

public static byte[] toGuidBytes(UUID theUuid) {
    ByteBuffer first8 = ByteBuffer.allocate(8);
    first8.putLong(theUuid.getMostSignificantBits());
    first8.rewind();//from w  w w.  j a  v  a2  s  . c  o  m

    byte[] first4 = new byte[4];
    first8.get(first4);
    reverse(first4);

    byte[] second2 = new byte[2];
    first8.get(second2);
    reverse(second2);

    byte[] third2 = new byte[2];
    first8.get(third2);
    reverse(third2);

    ByteBuffer converted16 = ByteBuffer.allocate(16).put(first4).put(second2).put(third2);

    ByteBuffer last8 = ByteBuffer.allocate(8);
    last8.putLong(theUuid.getLeastSignificantBits());
    last8.rewind();

    converted16.put(last8);

    return converted16.array();
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static String bytesToHex(ByteBuffer bytes) {
    final int offset = bytes.position();
    final int size = bytes.remaining();
    final char[] c = new char[size * 2];
    for (int i = 0; i < size; i++) {
        final int bint = bytes.get(i + offset);
        c[i * 2] = Hex.byteToChar[(bint & 0xf0) >> 4];
        c[1 + i * 2] = Hex.byteToChar[bint & 0x0f];
    }//from   w  w  w  .  j av  a  2 s . c  o m
    return Hex.wrapCharArray(c);
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static void write(ByteBuffer buffer, DataOutput out) throws IOException {
    if (buffer.hasArray()) {
        out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
    } else {/*from   w  w  w. ja  va  2s  .c o m*/
        for (int i = buffer.position(); i < buffer.limit(); i++) {
            out.writeByte(buffer.get(i));
        }
    }
}

From source file:com.aerohive.nms.engine.admin.task.licensemgr.license.processor2.PacketUtil.java

public static byte[] join(Header header, byte[] content) {
    ByteBuffer buf = ByteBuffer.allocate(8192);

    byte[] outBytes;
    if (content.length == 0) {
        outBytes = new byte[0];
    } else {/*from  ww  w  .j a  v a2s . co m*/
        if (header.isSecretFlag()) {
            // encrypt data
            outBytes = encryptData(content);
        } else {
            outBytes = new byte[content.length];
            System.arraycopy(content, 0, outBytes, 0, content.length);
        }
    }
    buf.put(header.getType());
    buf.putInt(outBytes.length);
    buf.put(header.getProtocolVersion());
    buf.put(header.isSecretFlag() ? CommConst.Secret_Flag_Yes : CommConst.Secret_Flag_No);
    buf.put(outBytes);

    buf.flip();

    byte[] dst = new byte[buf.limit()];
    buf.get(dst);
    return dst;
}