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.openteach.diamond.network.waverider.network.Packet.java

/**
 * ByteBuffer??/*from   ww w .jav a  2s .c om*/
 * @param buffer
 * @return
 */
public static Packet unmarshall(ByteBuffer buffer) {
    if (buffer.remaining() < getHeaderSize()) {
        throw new RuntimeException("Wrong packet.");
    }

    Packet packet = new Packet();
    byte[] str = new byte[NetWorkConstants.WAVERIDER_MAGIC.getBytes().length];
    buffer.get(str);
    packet.setMagic(new String(str));

    if (!NetWorkConstants.WAVERIDER_MAGIC.equals(packet.getMagic())) {
        throw new RuntimeException("Wrong packet.");
    }

    packet.setSequence(buffer.getLong());
    packet.setType(buffer.getLong());
    packet.setLength(buffer.getInt());
    packet.setPayLoad(buffer.slice());
    return packet;
}

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

/** trims size of bytebuffer to exactly number of bytes in it, to do not hold too much memory */
public static ByteBuffer minimalBufferFor(ByteBuffer buf) {
    return buf.capacity() > buf.remaining() || !buf.hasArray() ? ByteBuffer.wrap(getArray(buf)) : buf;
}

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

/**
 * You should almost never use this.  Instead, use the write* methods to avoid copies.
 *///from  ww  w  . j  a  v  a 2s  .  co m
public static byte[] getArray(ByteBuffer buffer) {
    int length = buffer.remaining();

    if (buffer.hasArray()) {
        int boff = buffer.arrayOffset() + buffer.position();
        return Arrays.copyOfRange(buffer.array(), boff, boff + length);
    }
    // else, DirectByteBuffer.get() is the fastest route
    byte[] bytes = new byte[length];
    buffer.duplicate().get(bytes);

    return bytes;
}

From source file:topoos.APIAccess.mime.HttpMultipart.java

/**
 * Encode./*from  ww  w  .j  av  a2  s. c  o  m*/
 *
 * @param charset the charset
 * @param string the string
 * @return the byte array buffer
 */
private static ByteArrayBuffer encode(final Charset charset, final String string) {
    ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
    ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
    bab.append(encoded.array(), encoded.position(), encoded.remaining());
    return bab;
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static ByteBuffer readShortByteArray(DataInput in) throws IOException {
    int length = readShortLength(in);
    ByteBuffer bb = ByteBuffer.allocate(length);
    in.readFully(bb.array(), bb.position(), bb.remaining());
    return bb;//from   www  .  j  a  va2  s .c  o m
}

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

public static boolean isPrefix(ByteBuffer prefix, ByteBuffer value) {
    if (prefix.remaining() > value.remaining()) {
        return false;
    }// w w w. j a  v a  2s  .  c o m

    int diff = value.remaining() - prefix.remaining();
    return prefix.equals(value.duplicate().limit(value.remaining() - diff));
}

From source file:com.github.blindpirate.gogradle.util.IOUtils.java

public static byte[] toByteArray(ByteBuffer buf) {
    ((Buffer) buf).position(0);//from ww w.ja v  a2 s .c om
    byte[] ret = new byte[buf.remaining()];
    buf.get(ret);
    return ret;
}

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

/**
 * @return a new copy of the data in @param buffer
 * USUALLY YOU SHOULD USE ByteBuffer.duplicate() INSTEAD, which creates a new Buffer
 * (so you can mutate its position without affecting the original) without copying the underlying array.
 *//*ww w  .  j  a  v a  2s . co  m*/
public static ByteBuffer clone(ByteBuffer buffer) {
    assert buffer != null;

    if (buffer.remaining() == 0) {
        return EMPTY_BYTE_BUFFER;
    }

    ByteBuffer clone = ByteBuffer.allocate(buffer.remaining());

    if (buffer.hasArray()) {
        System.arraycopy(buffer.array(), buffer.arrayOffset() + buffer.position(), clone.array(), 0,
                buffer.remaining());
    } else {
        clone.put(buffer.duplicate());
        clone.flip();
    }

    return clone;
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static ByteBuffer readByteArray(DataInput in) throws IOException {
    int length = in.readInt();
    if (length < 0) {
        throw new IOException("Corrupt (negative) value length encountered");
    }/*ww  w.  java2  s  .  c o  m*/

    ByteBuffer bb = ByteBuffer.allocate(length);
    if (length > 0) {
        in.readFully(bb.array(), bb.position(), bb.remaining());
    }
    return bb;
}

From source file:com.openteach.diamond.network.waverider.network.Packet.java

/**
 * /*w  w  w  . java2  s . com*/
 * @param command
 * @return
 */
public static Packet newDataPacket(Command command, Long sequence) {
    Packet packet = new Packet();
    packet.setSequence(sequence);
    packet.setType(PACKET_TYPE_DATA);
    ByteBuffer commandByteBuffer = command.marshall();
    packet.setLength(getHeaderSize() + commandByteBuffer.remaining());
    packet.setPayLoad(commandByteBuffer);
    //logger.info("New one packat");
    //packet.dump();
    return packet;
}