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:Main.java

public final static byte[] toBytesP(final ByteBuffer bb) {
    return toBytes(bb, bb.position(), bb.remaining());
}

From source file:Main.java

public final static ByteBuffer toNALFileFormat(final ByteBuffer buffer) {
    ByteBuffer result = ByteBuffer.allocate(buffer.remaining());
    result.put(buffer);//from w ww . ja va  2 s. co  m
    result.flip();
    int length = 0;
    int position = -1;
    int remaining = result.remaining() - 3;
    for (int i = 0; i < remaining; ++i) {
        if (result.get(i) == 0x00 && result.get(i + 1) == 0x00 && result.get(i + 2) == 0x00
                && result.get(i + 3) == 0x01) {
            if (0 <= position) {
                result.putInt(position, length - 3);
            }
            position = i;
            length = 0;
        } else {
            ++length;
        }
    }
    result.putInt(position, length);
    return result;
}

From source file:Main.java

/**
 * Split int value into two byte buffer. First byte of int will be written to first byte buffer and second to second one. How many
 * bytes will be written to first buffer determines based on <code>buffer.remaining()</code> value
 * /*from   w w w. jav  a  2 s.  c  o  m*/
 * @param buffer
 *          to write first part of value
 * @param buffer1
 *          to write second part of value
 */
public static void splitIntToBuffers(ByteBuffer buffer, ByteBuffer buffer1, int iValue) {
    int remaining = buffer.remaining();
    int i;
    for (i = 0; i < remaining; ++i) {
        buffer.put((byte) (MASK & (iValue >>> SIZE_OF_BYTE_IN_BITS * (SIZE_OF_INT - i - 1))));
    }
    for (int j = 0; j < SIZE_OF_INT - remaining; ++j) {
        buffer1.put((byte) (MASK & (iValue >>> SIZE_OF_BYTE_IN_BITS * (SIZE_OF_INT - i - j - 1))));
    }
}

From source file:Main.java

/**
 * Split long value into two byte buffer. First byte of long will be written to first byte buffer and second to second one. How
 * many bytes will be written to first buffer determines based on <code>buffer.remaining()</code> value
 * //  ww  w . j  av  a  2  s.c  o m
 * @param buffer
 *          to write first part of value
 * @param buffer1
 *          to write second part of value
 */
public static void splitLongToBuffers(ByteBuffer buffer, ByteBuffer buffer1, long iValue) {
    int remaining = buffer.remaining();
    int i;
    for (i = 0; i < remaining; ++i) {
        buffer.put((byte) (iValue >> SIZE_OF_BYTE_IN_BITS * (SIZE_OF_LONG - i - 1)));
    }
    for (int j = 0; j < SIZE_OF_LONG - remaining; ++j) {
        buffer1.put((byte) (iValue >> SIZE_OF_BYTE_IN_BITS * (SIZE_OF_LONG - i - j - 1)));
    }
}

From source file:Main.java

public static int byteBufferToByteArray(ByteBuffer byteBuffer, byte[] target, int offset) {
    int remaining = byteBuffer.remaining();
    System.arraycopy(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), target, offset,
            remaining);/*from   w  w w .  j  a  v a 2 s  .c  o  m*/
    return remaining;
}

From source file:Main.java

private static String getString(ByteBuffer buffer) {
    StringBuilder sb = new StringBuilder(buffer.limit());
    while (buffer.remaining() > 0) {
        char c = (char) buffer.get();
        if (c == 0)
            break;
        sb.append(c);//from  ww  w  .  j  av a  2s .com
    }
    return sb.toString();
}

From source file:Main.java

public static String convert(final ByteBuffer src) {
    src.flip();//from w ww  .  j a v  a 2 s.c o m
    final StringBuilder buffer = new StringBuilder(src.remaining());
    while (src.hasRemaining()) {
        buffer.append((char) (src.get() & 0xff));
    }
    return buffer.toString();
}

From source file:Main.java

public final static void writeP(final OutputStream out, final ByteBuffer bb) throws IOException {
    out.write(bb.array(), bb.position(), bb.remaining());
}

From source file:Main.java

/**
 * Merge long value from two byte buffer. First bytes of long will be extracted from first byte buffer and second from second one.
 * How many bytes will be read from first buffer determines based on <code>buffer.remaining()</code> value
 * //w  w w. j  a va 2 s  .c  o  m
 * @param buffer
 *          to read first part of value
 * @param buffer1
 *          to read second part of value
 * @return merged value
 */
public static long mergeLongFromBuffers(ByteBuffer buffer, ByteBuffer buffer1) {
    long result = 0;
    int remaining = buffer.remaining();
    for (int i = 0; i < remaining; ++i) {
        result = result | (MASK & buffer.get());
        result = result << SIZE_OF_BYTE_IN_BITS;
    }
    for (int i = 0; i < SIZE_OF_LONG - remaining - 1; ++i) {
        result = result | (MASK & buffer1.get());
        result = result << SIZE_OF_BYTE_IN_BITS;
    }
    result = result | (MASK & buffer1.get());
    return result;
}

From source file:org.apache.commons.crypto.examples.CipherByteBufferExample.java

/**
 * Converts ByteBuffer to String/*w w w. j  a v a 2s  .co m*/
 * 
 * @param buffer input byte buffer
 * @return the converted string
 */
private static String asString(ByteBuffer buffer) {
    final ByteBuffer copy = buffer.duplicate();
    final byte[] bytes = new byte[Math.min(copy.remaining(), 50)];
    copy.get(bytes);
    return new String(bytes, StandardCharsets.UTF_8);
}