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:com.easemob.dataexport.utils.ConversionUtils.java

public static double getDouble(ByteBuffer bytes) {
    return bytes.slice().getDouble();
}

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

public static ByteBuffer serializeKeys(UUID ownerUUID, String ownerType, String mapName, String mapKey,
        int bucketNumber) {

    List<Object> keys = new ArrayList<>(4);
    keys.add(0, ownerUUID);/*from   w w w  . j av a 2 s. c  o  m*/
    keys.add(1, ownerType);
    keys.add(2, mapName);
    keys.add(3, mapKey);

    if (bucketNumber > 0) {
        keys.add(4, bucketNumber);
    }

    // UUIDs are 16 bytes, allocate the buffer accordingly
    int size = 16 + ownerType.getBytes().length + mapName.getBytes().length + mapKey.getBytes().length;
    if (bucketNumber > 0) {
        // ints are 4 bytes
        size += 4;
    }

    // we always need to add length for the 2 byte short and 1 byte equality
    size += keys.size() * 3;

    ByteBuffer stuff = ByteBuffer.allocate(size);

    for (Object key : keys) {

        ByteBuffer kb = DataType.serializeValue(key, ProtocolVersion.NEWEST_SUPPORTED);
        if (kb == null) {
            kb = ByteBuffer.allocate(0);
        }

        stuff.putShort((short) kb.remaining());
        stuff.put(kb.slice());
        stuff.put((byte) 0);

    }
    stuff.flip();
    return stuff.duplicate();

}

From source file:Main.java

/**
 * Returns new byte buffer whose content is a shared subsequence of this buffer's content
 * between the specified start (inclusive) and end (exclusive) positions. As opposed to
 * {@link ByteBuffer#slice()}, the returned buffer's byte order is the same as the source
 * buffer's byte order.//w  w  w  . jav a 2s.  co  m
 */
private static ByteBuffer sliceFromTo(final ByteBuffer source, final int start, final int end) {
    if (start < 0) {
        throw new IllegalArgumentException("start: " + start);
    }
    if (end < start) {
        throw new IllegalArgumentException("end < start: " + end + " < " + start);
    }
    final int capacity = source.capacity();
    if (end > source.capacity()) {
        throw new IllegalArgumentException("end > capacity: " + end + " > " + capacity);
    }
    final int originalLimit = source.limit();
    final int originalPosition = source.position();
    try {
        source.position(0);
        source.limit(end);
        source.position(start);
        final ByteBuffer result = source.slice();
        result.order(source.order());
        return result;
    } finally {
        source.position(0);
        source.limit(originalLimit);
        source.position(originalPosition);
    }
}

From source file:org.apache.usergrid.persistence.index.utils.ConversionUtils.java

public static Object object(Class<?> type, ByteBuffer bytes) {

    try {/*from   w  ww  . ja v  a  2  s.  c o  m*/
        if (Long.class.isAssignableFrom(type)) {
            return bytes.slice().getLong();
        } else if (UUID.class.isAssignableFrom(type)) {
            return uuid(bytes);
        } else if (String.class.isAssignableFrom(type)) {
            return string(bytes);
        } else if (Boolean.class.isAssignableFrom(type)) {
            return bytes.slice().get() != 0;
        } else if (Integer.class.isAssignableFrom(type)) {
            return bytes.slice().getInt();
        } else if (Double.class.isAssignableFrom(type)) {
            return bytes.slice().getDouble();
        } else if (Float.class.isAssignableFrom(type)) {
            return bytes.slice().getFloat();
        } else if (ByteBuffer.class.isAssignableFrom(type)) {
            return bytes.duplicate();
        } else if (byte[].class.isAssignableFrom(type)) {
            byte[] b = new byte[bytes.remaining()];
            bytes.slice().get(b);
            return b;
        }
    } catch (Exception e) {
        logger.error("Unable to get object from bytes for type {}", type.getName(), e);
    }
    return null;
}

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

public static Object object(Class<?> type, ByteBuffer bytes) {

    try {//from w w w  . j  a  v a2s .  co  m
        if (Long.class.isAssignableFrom(type)) {
            return bytes.slice().getLong();
        } else if (UUID.class.isAssignableFrom(type)) {
            return uuid(bytes);
        } else if (String.class.isAssignableFrom(type)) {
            return string(bytes);
        } else if (Boolean.class.isAssignableFrom(type)) {
            return bytes.slice().get() != 0;
        } else if (Integer.class.isAssignableFrom(type)) {
            return bytes.slice().getInt();
        } else if (Double.class.isAssignableFrom(type)) {
            return bytes.slice().getDouble();
        } else if (Float.class.isAssignableFrom(type)) {
            return bytes.slice().getFloat();
        } else if (ByteBuffer.class.isAssignableFrom(type)) {
            return bytes.duplicate();
        } else if (byte[].class.isAssignableFrom(type)) {
            byte[] b = new byte[bytes.remaining()];
            bytes.slice().get(b);
            return b;
        }
    } catch (Exception e) {
        logger.error("Unable to get object from bytes for type " + type.getName(), e);
    }
    return null;
}

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

public static UUID uuid(ByteBuffer bb) {
    if (bb == null) {
        return null;
    }/*from w  ww  .j  av  a 2 s  . c om*/
    if (bb.remaining() < 16) {
        return null;
    }
    bb = bb.slice();
    return new UUID(bb.getLong(), bb.getLong());
}

From source file:net.darkmist.alib.io.BufferUtil.java

/**
 * Sane ByteBuffer slice/*from   w  w w  . java  2 s.com*/
 * @param buf the buffer to slice something out of
 * @param off The offset into the buffer
 * @param len the length of the part to slice out
 */
public static ByteBuffer slice(ByteBuffer buf, int off, int len) {
    ByteBuffer localBuf = buf.duplicate(); // so we don't mess up the position,etc
    logger.debug("off={} len={}", off, len);
    localBuf.position(off);
    localBuf.limit(off + len);
    logger.debug("pre-slice: localBuf.position()={} localBuf.limit()={}", localBuf.position(),
            localBuf.limit());
    localBuf = localBuf.slice();
    logger.debug("post-slice: localBuf.position()={} localBuf.limit()={}", localBuf.position(),
            localBuf.limit());
    return localBuf;
}

From source file:org.lenskit.data.packed.BinaryRatingDAO.java

static BinaryRatingDAO fromBuffer(ByteBuffer buffer) {
    // start by reading header from the buffer
    BinaryHeader header = BinaryHeader.fromHeader(buffer);
    assert buffer.position() >= BinaryHeader.HEADER_SIZE;
    // the header read advanced the buffer position past the header; prepare a data slice
    // first slice to remove the header
    ByteBuffer data = buffer.slice();
    // then limit to the rating data size
    data.limit(header.getRatingDataSize());
    assert data.remaining() == header.getRatingDataSize();

    // prepare to read tables
    ByteBuffer tableBuffer = buffer.duplicate();
    // skip the header and the rating data
    tableBuffer.position(tableBuffer.position() + header.getRatingDataSize());
    // each of the following reads advances the buffer by the amount read
    BinaryIndexTable utbl = BinaryIndexTable.fromBuffer(header.getUserCount(), tableBuffer);
    BinaryIndexTable itbl = BinaryIndexTable.fromBuffer(header.getItemCount(), tableBuffer);

    return new BinaryRatingDAO(null, header, data, utbl, itbl, header.getRatingCount(), Long.MAX_VALUE);
}

From source file:org.grouplens.lenskit.data.dao.packed.BinaryRatingDAO.java

static BinaryRatingDAO fromBuffer(ByteBuffer buffer) {
    BinaryHeader header = BinaryHeader.fromHeader(buffer);
    assert buffer.position() >= BinaryHeader.HEADER_SIZE;
    ByteBuffer dup = buffer.duplicate();
    dup.limit(header.getRatingDataSize());

    ByteBuffer tableBuffer = buffer.duplicate();
    tableBuffer.position(tableBuffer.position() + header.getRatingDataSize());
    BinaryIndexTable utbl = BinaryIndexTable.fromBuffer(header.getUserCount(), tableBuffer);
    BinaryIndexTable itbl = BinaryIndexTable.fromBuffer(header.getItemCount(), tableBuffer);

    return new BinaryRatingDAO(null, header, dup.slice(), utbl, itbl);
}

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

/**
 * message content chunk received to the server
 *
 * @param messageID       id of message to which content belongs
 * @param offsetInMessage chunk offset/*from   w  w  w  . ja  v  a 2 s . c  o  m*/
 * @param src             Bytebuffer with content bytes
 */
public static AndesMessagePart messageContentChunkReceived(long messageID, int offsetInMessage,
        ByteBuffer src) {

    if (log.isDebugEnabled()) {
        log.debug("Content Part Received id " + messageID + ", offset " + offsetInMessage);
    }
    AndesMessagePart part = new AndesMessagePart();
    src = src.slice();
    final byte[] chunkData = new byte[src.limit()];
    src.duplicate().get(chunkData);

    part.setData(chunkData);
    part.setMessageID(messageID);
    part.setOffSet(offsetInMessage);

    return part;
}