Example usage for java.nio ByteBuffer remaining

List of usage examples for java.nio ByteBuffer remaining

Introduction

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

Prototype

public final int remaining() 

Source Link

Document

Returns the number of remaining elements in this buffer, that is limit - position .

Usage

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

/**
 * from to /*w  w w .ja  v a2s  . co  m*/
 * 
 * @param fromBuffer
 *            Buffer ? flush
 * @param toBuffer
 *            Buffer ? fill
 * @return number of bytes moved
 */
public static int put(ByteBuffer fromBuffer, ByteBuffer toBuffer) {
    int put;
    int remaining = fromBuffer.remaining();
    if (remaining > 0) { // 
        if (remaining <= toBuffer.remaining()) {
            toBuffer.put(fromBuffer);
            put = remaining;
            // from 
            fromBuffer.position(fromBuffer.limit());
        }
        // heap buffer
        else if (fromBuffer.hasArray()) {
            put = toBuffer.remaining();
            // ??
            toBuffer.put(fromBuffer.array(), fromBuffer.arrayOffset() + fromBuffer.position(), put);
            fromBuffer.position(fromBuffer.position() + put);
        }
        // direct buffer
        else {
            // ??
            put = toBuffer.remaining();
            ByteBuffer slice = fromBuffer.slice();
            slice.limit(put);
            toBuffer.put(slice);
            fromBuffer.position(fromBuffer.position() + put);
        }
    } else {
        put = 0;
    }
    return put;
}

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);/* www .  j a  v  a 2 s  . c om*/
    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:com.easemob.dataexport.utils.ConversionUtils.java

public static byte[] bytes(ByteBuffer bb) {
    byte[] b = new byte[bb.remaining()];
    bb.duplicate().get(b);//  w ww.  j  a v  a 2 s.c  o  m
    return b;
}

From source file:edu.umass.cs.utils.Util.java

/**
 * Transfer from src to dst without throwing exception if src.remaining() >
 * dst.remaining() but copying dst.remaining() bytes from src instead.
 *//*from   www .j a  v  a  2  s  .c o m*/
public static ByteBuffer put(ByteBuffer dst, ByteBuffer src) {
    if (src.remaining() < dst.remaining())
        return dst.put(src);
    int oldLimit = src.limit();
    src.limit(src.position() + dst.remaining());
    dst.put(src);
    src.limit(oldLimit);
    return dst;
    // byte[] buf = new byte[dst.remaining()];
    // src.get(buf);
    // return dst.put(buf);
}

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

public static Object fromByteBuffer(ByteBuffer byteBuffer, Class<?> clazz) {
    if ((byteBuffer == null) || !byteBuffer.hasRemaining()) {
        return null;
    }//from  w w w.  j  a v  a  2 s  .co  m
    if (clazz == null) {
        clazz = Object.class;
    }

    Object obj = null;
    try {
        obj = smileMapper.readValue(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(),
                byteBuffer.remaining(), clazz);
    } catch (Exception e) {
        LOG.error("Error parsing SMILE bytes", e);
    }
    return obj;
}

From source file:org.apache.cassandra.index.sasi.disk.TokenTreeTest.java

private static DecoratedKey dk(Long token) {
    ByteBuffer buf = ByteBuffer.allocate(8);
    buf.putLong(token);/*from ww  w.  ja v  a2 s  .c o  m*/
    buf.flip();
    Long hashed = MurmurHash.hash2_64(buf, buf.position(), buf.remaining(), 0);
    return new BufferDecoratedKey(new Murmur3Partitioner.LongToken(hashed), buf);
}

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

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

From source file:org.apache.cassandra.jmeter.AbstractCassandaTestElement.java

private static String bytesToHex(ByteBuffer bb) {
    char[] hexChars = new char[bb.remaining() * 2];
    int j = 0;//from www .  jav a  2  s.  c om
    while (bb.hasRemaining()) {
        int v = bb.get() & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        j++;
    }

    return "0x" + new String(hexChars);
}

From source file:org.apache.hadoop.hbase.ipc.IPCUtil.java

private static int write(final OutputStream dos, final Message header, final Message param,
        final ByteBuffer cellBlock, final int totalSize) throws IOException {
    // I confirmed toBytes does same as DataOutputStream#writeInt.
    dos.write(Bytes.toBytes(totalSize));
    // This allocates a buffer that is the size of the message internally.
    header.writeDelimitedTo(dos);//from w w w .  ja va  2  s. c o  m
    if (param != null)
        param.writeDelimitedTo(dos);
    if (cellBlock != null)
        dos.write(cellBlock.array(), 0, cellBlock.remaining());
    dos.flush();
    return totalSize;
}

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 {//w ww.  j av a2 s.com
        for (int i = buffer.position(); i < buffer.limit(); i++) {
            out.writeByte(buffer.get(i));
        }
    }
}