Example usage for java.nio ByteBuffer hasRemaining

List of usage examples for java.nio ByteBuffer hasRemaining

Introduction

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

Prototype

public final boolean hasRemaining() 

Source Link

Document

Indicates if there are elements remaining in this buffer, that is if position < limit .

Usage

From source file:Main.java

public static void showByteOrder(ByteBuffer bb) {
    System.out.println("Byte  Order: " + bb.order());
    while (bb.hasRemaining()) {
        System.out.print(bb.get() + "    ");
    }//  w w w.  ja v a  2  s .co  m
    System.out.println();
}

From source file:Main.java

public static boolean goToPrefix(final ByteBuffer buffer) {
    int presudo_prefix = 0xffffffff;
    while (buffer.hasRemaining()) {
        presudo_prefix = (presudo_prefix << 8) | (buffer.get() & 0xff);
        if (presudo_prefix == START_PREFIX_CODE) {
            return true;
        }/*from  w  w  w  .  j a  v  a  2 s.c om*/
    }
    return false;
}

From source file:de.zalando.spring.cloud.config.aws.kms.KmsTextEncryptor.java

private static String extractString(final ByteBuffer bb) {
    if (bb.hasRemaining()) {
        final byte[] bytes = new byte[bb.remaining()];
        bb.get(bytes, bb.arrayOffset(), bb.remaining());
        return new String(bytes);
    } else {/* w  ww . j a va 2s.  c o  m*/
        return EMPTY_STRING;
    }
}

From source file:Main.java

/**
 * Finds next Nth MPEG bitstream marker 0x000001xx and returns the data that
 * preceeds it as a ByteBuffer slice/*w  w  w.  j a  v  a 2 s  .  c  o  m*/
 * 
 * Segment byte order is always little endian
 * 
 * @param buf
 * @return
 */
public static final ByteBuffer gotoMarker(ByteBuffer buf, int n, int mmin, int mmax) {
    if (!buf.hasRemaining())
        return null;

    int from = buf.position();
    ByteBuffer result = buf.slice();
    result.order(ByteOrder.BIG_ENDIAN);

    int val = 0xffffffff;
    while (buf.hasRemaining()) {
        val = (val << 8) | (buf.get() & 0xff);
        if (val >= mmin && val <= mmax) {
            if (n == 0) {
                buf.position(buf.position() - 4);
                result.limit(buf.position() - from);
                break;
            }
            --n;
        }
    }
    return result;
}

From source file:Main.java

public static String convert(final ByteBuffer src) {
    src.flip();//from  w w  w  .  j a  v  a2 s .co  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 static void transfer(ReadableByteChannel in, WritableByteChannel out) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(4096);
    while (in.read(buffer) != -1) {
        buffer.flip();/* ww  w .j a va  2  s  .  c o m*/
        while (buffer.hasRemaining()) {
            out.write(buffer);
        }
        buffer.clear();
    }
}

From source file:Main.java

private static String urlencode(final String content, final Charset charset, final BitSet safechars,
        final boolean blankAsPlus) {
    if (content == null) {
        return null;
    }//from  w w w . j  ava  2  s . c  om
    StringBuilder buf = new StringBuilder();
    ByteBuffer bb = charset.encode(content);
    while (bb.hasRemaining()) {
        int b = bb.get() & 0xff;
        if (safechars.get(b)) {
            buf.append((char) b);
        } else if (blankAsPlus && b == ' ') {
            buf.append('+');
        } else {
            buf.append("%");
            char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
            char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
            buf.append(hex1);
            buf.append(hex2);
        }
    }
    return buf.toString();
}

From source file:IOUtilities.java

/**
 * Copy ALL available data from one stream into another
 * @param in/*from  w w w . ja v  a  2s .c o  m*/
 * @param out
 * @throws IOException
 */
public static void copy(InputStream in, OutputStream out) throws IOException {
    ReadableByteChannel source = Channels.newChannel(in);
    WritableByteChannel target = Channels.newChannel(out);

    ByteBuffer buffer = ByteBuffer.allocate(16 * 1024);
    while (source.read(buffer) != -1) {
        buffer.flip(); // Prepare the buffer to be drained
        while (buffer.hasRemaining()) {
            target.write(buffer);
        }
        buffer.clear(); // Empty buffer to get ready for filling
    }

    source.close();
    target.close();

}

From source file:Main.java

public static void writeFully(final WritableByteChannel channel, final ByteBuffer buf) throws IOException {
    do {//  w  w  w  . j  a  v  a  2s.  com
        int written = channel.write(buf);
        if (written < 0) {
            throw new EOFException();
        }
    } while (buf.hasRemaining());
}

From source file:org.apache.hadoop.hdfs.server.datanode.BlockMetadataHeader.java

/**
 * Read the header without changing the position of the FileChannel.
 *
 * @param fc The FileChannel to read.//from w  w w. j  ava2s. c o  m
 * @return the Metadata Header.
 * @throws IOException on error.
 */
public static BlockMetadataHeader preadHeader(FileChannel fc) throws IOException {
    final byte arr[] = new byte[getHeaderSize()];
    ByteBuffer buf = ByteBuffer.wrap(arr);

    while (buf.hasRemaining()) {
        if (fc.read(buf, 0) <= 0) {
            throw new EOFException("unexpected EOF while reading " + "metadata file header");
        }
    }
    short version = (short) ((arr[0] << 8) | (arr[1] & 0xff));
    DataChecksum dataChecksum = DataChecksum.newDataChecksum(arr, 2);
    return new BlockMetadataHeader(version, dataChecksum);
}