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.amazonaws.services.sqs.MessageMD5ChecksumHandler.java

/**
 * Update the digest using a sequence of bytes that consists of the length
 * (in 4 bytes) of the input ByteBuffer and all the bytes it contains.
 *//* w  w w .  j a v a  2 s  .  c  o m*/
private static void updateLengthAndBytes(MessageDigest digest, ByteBuffer binaryValue) {
    // Rewind the ByteBuffer, in case that get/put operations were applied to
    // the unmarshalled BB before it's passed to this handler.
    binaryValue.rewind();
    int size = binaryValue.remaining();
    ByteBuffer lengthBytes = ByteBuffer.allocate(INTEGER_SIZE_IN_BYTES).putInt(size);
    digest.update(lengthBytes.array());
    digest.update(binaryValue);
}

From source file:org.apache.cassandra.utils.ByteBufferUtil.java

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

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

    return bytes;
}

From source file:org.apache.cassandra.utils.ByteBufferUtil.java

public static void writeWithShortLength(ByteBuffer buffer, DataOutput out) {
    int length = buffer.remaining();
    assert 0 <= length && length <= FBUtilities.MAX_UNSIGNED_SHORT : length;
    try {/*  ww  w.  ja  v a 2 s.  c o m*/
        out.writeByte((length >> 8) & 0xFF);
        out.writeByte(length & 0xFF);
        write(buffer, out); // writing data bytes to output source
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.serotonin.bacnet4j.util.sero.StreamUtils.java

public static void transfer(InputStream in, SocketChannel out) throws IOException {
    byte[] buf = new byte[1024];
    ByteBuffer bbuf = ByteBuffer.allocate(1024);
    int len;/*from www .j  a v  a 2s .c o m*/
    while ((len = in.read(buf)) != -1) {
        bbuf.put(buf, 0, len);
        bbuf.flip();
        while (bbuf.remaining() > 0)
            out.write(bbuf);
        bbuf.clear();
    }
}

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

public static void writeAll(ByteBuffer buf, WritableByteChannel channel) throws IOException {
    while (buf.remaining() > 0)
        channel.write(buf);//from   w w w .ja v a2  s  .  c om
}

From source file:org.apache.cassandra.utils.ByteBufferUtil.java

public static int compareUnsigned(ByteBuffer o1, ByteBuffer o2) {
    assert o1 != null;
    assert o2 != null;

    int minLength = Math.min(o1.remaining(), o2.remaining());
    for (int x = 0, i = o1.position(), j = o2.position(); x < minLength; x++, i++, j++) {
        if (o1.get(i) == o2.get(j))
            continue;
        // compare non-equal bytes as unsigned
        return (o1.get(i) & 0xFF) < (o2.get(j) & 0xFF) ? -1 : 1;
    }/* ww  w .  j a  v a  2s . co m*/

    return (o1.remaining() == o2.remaining()) ? 0 : ((o1.remaining() < o2.remaining()) ? -1 : 1);
}

From source file:org.apache.hadoop.hdfs.protocol.datatransfer.PacketReceiver.java

private static void doReadFully(ReadableByteChannel ch, InputStream in, ByteBuffer buf) throws IOException {
    if (ch != null) {
        readChannelFully(ch, buf);/*from  w  w w .j a va 2  s .c  o m*/
    } else {
        Preconditions.checkState(!buf.isDirect(), "Must not use direct buffers with InputStream API");
        IOUtils.readFully(in, buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
        buf.position(buf.position() + buf.remaining());
    }
}

From source file:com.antsdb.saltedfish.server.mysql.util.BufferUtils.java

public static void writeLenString(ByteBuf buf, String s, Charset encoder) {
    if (s == null) {
        writeFieldLength(buf, 0);/*from   w  w  w  . j  a v  a2s . co  m*/
        return;
    }
    ByteBuffer bb = encoder.encode(s);
    writeFieldLength(buf, bb.remaining());
    buf.writeBytes(bb);
}

From source file:net.pms.util.AudioUtils.java

private static void parseRealAudioMetaData(ByteBuffer buffer, DLNAMediaAudio audio, short version) {
    buffer.position(buffer.position() + (version == 4 ? 3 : 4)); // skip unknown
    byte b = buffer.get();
    if (b != 0) {
        byte[] title = new byte[Math.min(b & 0xFF, buffer.remaining())];
        buffer.get(title);/*from ww  w  .j  av a2 s .  c o m*/
        String titleString = new String(title, StandardCharsets.US_ASCII);
        audio.setSongname(titleString);
        audio.setAudioTrackTitleFromMetadata(titleString);
    }
    if (buffer.hasRemaining()) {
        b = buffer.get();
        if (b != 0) {
            byte[] artist = new byte[Math.min(b & 0xFF, buffer.remaining())];
            buffer.get(artist);
            audio.setArtist(new String(artist, StandardCharsets.US_ASCII));
        }
    }
}

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

public static boolean hasContent(ByteBuffer buff) {
    return buff != null && buff.remaining() > 0;
}