Example usage for java.nio ByteBuffer slice

List of usage examples for java.nio ByteBuffer slice

Introduction

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

Prototype

public abstract ByteBuffer slice();

Source Link

Document

Returns a sliced buffer that shares its content with this buffer.

Usage

From source file:org.wso2.andes.amqp.AMQPUtils.java

/**
 * convert an AMQMessage to andes metadata
 *
 * @param amqMessage qpid amqMessage// w  w w.j a  v  a  2 s  .c  o m
 * @return andes message metadata
 * @throws AndesException
 */
public static AndesMessageMetadata convertAMQMessageToAndesMetadata(AMQMessage amqMessage) {
    MessageMetaData amqMetadata = amqMessage.getMessageMetaData();
    String queue = amqMetadata.getMessagePublishInfo().getRoutingKey().toString();

    final int bodySize = 1 + amqMetadata.getStorableSize();
    byte[] underlying = new byte[bodySize];
    underlying[0] = (byte) amqMetadata.getType().ordinal();
    java.nio.ByteBuffer buf = java.nio.ByteBuffer.wrap(underlying);
    buf.position(1);
    buf = buf.slice();
    amqMetadata.writeToBuffer(0, buf);
    AndesMessageMetadata metadata = new AndesMessageMetadata(amqMessage.getMessageId(), underlying, true);
    metadata.setArrivalTime(amqMessage.getArrivalTime());
    metadata.setMessageContentLength(amqMetadata.getContentSize());

    return metadata;
}

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

/**
 * ByteBuffer??/*from w  w w. j  a  v a2 s  .  c o m*/
 * @param buffer
 * @return
 */
public static Command unmarshall(ByteBuffer buffer) {
    if (buffer.remaining() < getHeaderSize()) {
        throw new RuntimeException("Wrong command.");
    }
    Command command = new Command();
    command.setType(buffer.getLong());
    command.setLength(buffer.getInt());
    // (payLoad)array()
    command.setPayLoad(buffer.slice());
    return command;
}

From source file:Main.java

/**
 * Relative <em>get</em> method for reading {@code size} number of bytes from the current
 * position of this buffer.// w  w  w.  j a  v a  2 s. com
 * <p>
 * <p>This method reads the next {@code size} bytes at this buffer's current position,
 * returning them as a {@code ByteBuffer} with start set to 0, limit and capacity set to
 * {@code size}, byte order set to this buffer's byte order; and then increments the position by
 * {@code size}.
 */
private static ByteBuffer getByteBuffer(final ByteBuffer source, final int size)
        throws BufferUnderflowException {
    if (size < 0) {
        throw new IllegalArgumentException("size: " + size);
    }
    final int originalLimit = source.limit();
    final int position = source.position();
    final int limit = position + size;
    if ((limit < position) || (limit > originalLimit)) {
        throw new BufferUnderflowException();
    }
    source.limit(limit);
    try {
        final ByteBuffer result = source.slice();
        result.order(source.order());
        source.position(limit);
        return result;
    } finally {
        source.limit(originalLimit);
    }
}

From source file:org.apache.usergrid.persistence.map.impl.MapSerializationImpl.java

private static Object deserializeMapEntryKey(ByteBuffer bb) {

    List<Object> stuff = new ArrayList<>();
    while (bb.hasRemaining()) {
        ByteBuffer data = CQLUtils.getWithShortLength(bb);
        if (stuff.size() == 0) {
            stuff.add(DataType.uuid().deserialize(data.slice(), ProtocolVersion.NEWEST_SUPPORTED));
        } else {/* w w w. ja  v a  2 s .  co  m*/
            stuff.add(DataType.text().deserialize(data.slice(), ProtocolVersion.NEWEST_SUPPORTED));
        }
        byte equality = bb.get(); // we don't use this but take the equality byte off the buffer

    }

    return stuff;

}

From source file:org.xenei.bloomgraph.bloom.sql.DBIO.java

/**
 * Convert the byte buffer to a input stream.
 * /* ww  w  .  j a  v a 2 s  .c  om*/
 * @param buffer
 *            the buffer to conver
 * @return the input stream.
 */
public static InputStream asInputStream(final ByteBuffer buffer) {
    final ByteBuffer buff = buffer.slice().order(ByteOrder.LITTLE_ENDIAN);
    if (buff.hasArray()) {
        // use heap buffer; no array is created; only the reference is used
        return new ByteArrayInputStream(buff.array());
    }
    return new ByteBufferInputStream(buff);
}

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

private static void dumpByteBuffer(ByteBuffer input)
        throws ArrayIndexOutOfBoundsException, IllegalArgumentException, IOException {
    input = input.slice();
    byte bytes[] = new byte[input.remaining()];
    input.get(bytes);/* ww  w .  j  ava2 s. co  m*/
    HexDump.dump(bytes, 0, System.out, 0);
}

From source file:com.easemob.dataexport.utils.ConversionUtils.java

public static boolean getBoolean(ByteBuffer bytes) {
    return bytes.slice().get() != 0;
}

From source file:com.easemob.dataexport.utils.ConversionUtils.java

public static long getLong(ByteBuffer bytes) {
    return bytes.slice().getLong();
}

From source file:com.easemob.dataexport.utils.ConversionUtils.java

public static int getInt(ByteBuffer bytes) {
    return bytes.slice().getInt();
}

From source file:com.easemob.dataexport.utils.ConversionUtils.java

public static float getFloat(ByteBuffer bytes) {
    return bytes.slice().getFloat();
}