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:io.mycat.util.ByteBufferUtil.java

/**
 * Compare two ByteBuffer at specified offsets for length.
 * Compares the non equal bytes as unsigned.
 * @param bytes1 First byte buffer to compare.
 * @param offset1 Position to start the comparison at in the first array.
 * @param bytes2 Second byte buffer to compare.
 * @param offset2 Position to start the comparison at in the second array.
 * @param length How many bytes to compare?
 * @return -1 if byte1 is less than byte2, 1 if byte2 is less than byte1 or 0 if equal.
 *///  w w w.  j  av  a2s .c  o  m
public static int compareSubArrays(ByteBuffer bytes1, int offset1, ByteBuffer bytes2, int offset2, int length) {
    if (bytes1 == null) {
        return bytes2 == null ? 0 : -1;
    }
    if (bytes2 == null) {
        return 1;
    }

    assert bytes1.limit() >= offset1
            + length : "The first byte array isn't long enough for the specified offset and length.";
    assert bytes2.limit() >= offset2
            + length : "The second byte array isn't long enough for the specified offset and length.";
    for (int i = 0; i < length; i++) {
        byte byte1 = bytes1.get(offset1 + i);
        byte byte2 = bytes2.get(offset2 + i);
        //            if (byte1 == byte2)
        //                continue;
        // compare non-equal bytes as unsigned
        if (byte1 != byte2) {
            return (byte1 & 0xFF) < (byte2 & 0xFF) ? -1 : 1;
        }
    }
    return 0;
}

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

/**
 * Transfer bytes from one ByteBuffer to another. This function acts as
 * System.arrayCopy() but for ByteBuffers.
 * /*from ww  w .  j a va  2  s  .  c om*/
 * @param src
 *            the source ByteBuffer
 * @param srcPos
 *            starting position in the source ByteBuffer
 * @param dst
 *            the destination ByteBuffer
 * @param dstPos
 *            starting position in the destination ByteBuffer
 * @param length
 *            the number of bytes to copy
 */
public static void arrayCopy(ByteBuffer src, int srcPos, ByteBuffer dst, int dstPos, int length) {
    if (src.hasArray() && dst.hasArray()) {
        System.arraycopy(src.array(), src.arrayOffset() + srcPos, dst.array(), dst.arrayOffset() + dstPos,
                length);
    } else {
        if (src.limit() - srcPos < length || dst.limit() - dstPos < length)
            throw new IndexOutOfBoundsException();

        for (int i = 0; i < length; i++)

            dst.put(dstPos++, src.get(srcPos++));
    }
}

From source file:com.ah.ui.actions.home.clientManagement.service.CertificateGenSV.java

@SuppressWarnings("resource")
public static byte[] readFromFile(File file) throws IOException {
    FileChannel fileChannel = new FileInputStream(file).getChannel();
    ByteBuffer bb = ByteBuffer.allocate((int) fileChannel.size());
    fileChannel.read(bb);/*from www.  j  a v a  2 s.c o  m*/
    fileChannel.close();
    bb.flip();
    byte[] bytes;

    if (bb.hasArray()) {
        bytes = bb.array();
    } else {
        bytes = new byte[bb.limit()];
        bb.get(bytes);
    }

    return bytes;
}

From source file:com.FalcoLabs.Fido.api.datastore.serializers.BinarySerializer.java

@SuppressWarnings("unchecked")
public T fromByteBuffer(ByteBuffer byteBuffer) {
    byte[] bytes = new byte[byteBuffer.remaining()];
    byteBuffer.get(bytes);
    Object obj = SerializationUtils.deserialize(bytes);
    return (T) obj;
}

From source file:com.eventsourcing.layout.binary.ByteArrayBinaryDeserializer.java

@Override
public Object deserialize(ByteArrayTypeHandler typeHandler, ByteBuffer buffer) {
    int len = buffer.getInt();
    byte[] bytes = new byte[len];
    buffer.get(bytes);
    if (typeHandler.isPrimitive()) {
        return bytes;
    } else {//ww w  .ja va2  s  .c o m
        return toObject(bytes);
    }
}

From source file:de.ailis.usb4java.descriptors.SimpleUsbStringDescriptor.java

/**
 * Constructs a new string descriptor by reading the descriptor data
 * from the specified byte buffer./*w  w  w .  ja v a  2s.c o m*/
 * 
 * @param data
 *            The descriptor data as a byte buffer.
 */
public SimpleUsbStringDescriptor(final ByteBuffer data) {
    super(data.get(0), data.get(1));

    data.position(2);
    this.bString = new byte[bLength() - 2];
    data.get(this.bString);
}

From source file:org.elasticsoftware.elasticactors.base.serialization.JacksonMessageDeserializer.java

@Override
public T deserialize(ByteBuffer serializedObject) throws IOException {
    byte[] buf = new byte[serializedObject.remaining()];
    serializedObject.get(buf);
    return objectMapper.readValue(buf, objectClass);
}

From source file:com.gumgum.kafka.consumer.KafkaTemplate.java

private String convertToUtf8String(ByteBuffer buffer) throws Exception {
    byte[] bytes = new byte[buffer.remaining()];
    buffer.get(bytes);
    return new String(bytes, "UTF-8");
}

From source file:ByteString.java

public void retrieve(ByteBuffer bb) {
    short n = bb.getShort();
    if (n > 0) {
        bytes = new byte[n];
        bb.get(bytes);
    } else {/*from   w  w  w.j  av a 2s. c  om*/
        bytes = null;
    }
}

From source file:de.rwhq.serializer.FixedStringSerializer.java

@Override
public String deserialize(final byte[] o) {
    final ByteBuffer buf = ByteBuffer.wrap(o);
    final short length = buf.getShort();

    final byte[] bytes = new byte[length];
    buf.get(bytes);
    return new String(bytes);
}