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:org.springframework.boot.devtools.tunnel.payload.HttpTunnelPayload.java

/**
 * Return the {@link HttpTunnelPayload} for the given message or {@code null} if there
 * is no payload./*from   w  w  w.  j av  a  2  s .  c  o m*/
 * @param message the HTTP message
 * @return the payload or {@code null}
 * @throws IOException in case of I/O errors
 */
public static HttpTunnelPayload get(HttpInputMessage message) throws IOException {
    long length = message.getHeaders().getContentLength();
    if (length <= 0) {
        return null;
    }
    String seqHeader = message.getHeaders().getFirst(SEQ_HEADER);
    Assert.state(StringUtils.hasLength(seqHeader), "Missing sequence header");
    ReadableByteChannel body = Channels.newChannel(message.getBody());
    ByteBuffer payload = ByteBuffer.allocate((int) length);
    while (payload.hasRemaining()) {
        body.read(payload);
    }
    body.close();
    payload.flip();
    return new HttpTunnelPayload(Long.valueOf(seqHeader), payload);
}

From source file:com.stratagis.geoevent.adapter.nmeaplus.NmeaPlusInboundAdapter.java

private static List<byte[]> index(ByteBuffer in) {

    List<byte[]> messages = new ArrayList<byte[]>();
    for (int i = -1; in.hasRemaining();) {
        byte b = in.get();
        if (b == ((byte) '$')) // bom
        {/*from w  ww .j a  v a  2s.  c  om*/
            i = in.position();
            in.mark();
        } else if (b == ((byte) '\r') || b == ((byte) '\n')) // eom
        {
            if (i != -1) {
                byte[] message = new byte[in.position() - 1 - i];
                System.arraycopy(in.array(), i, message, 0, message.length);
                messages.add(message);
            }
            i = -1;
            in.mark();
        } else if (messages.isEmpty() && i == -1)
            in.mark();
    }
    return messages;
}

From source file:Main.java

public static void copy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocateDirect(CAPACITY);
    while (src.read(buffer) != -1) {
        buffer.flip();//from w ww . jav a 2 s.  c o m
        dest.write(buffer);
        buffer.compact();
    }

    buffer.flip();

    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}

From source file:Main.java

/**
 * @param columnarKeyBlockData/*from   w  w w .jav  a2s. c o m*/
 * @param columnarKeyStoreMetadata
 * @return
 * @author s71955 The high cardinality dimensions rows will be send in byte
 * array with its data length appended in the
 * ColumnarKeyStoreDataHolder byte array since high cardinality dim
 * data will not be part of MDKey/Surrogate keys. In this method the
 * byte array will be scanned and the length which is stored in
 * short will be removed.
 */
public static List<byte[]> readColumnarKeyBlockDataForNoDictionaryCols(byte[] columnarKeyBlockData) {
    List<byte[]> columnarKeyBlockDataList = new ArrayList<byte[]>(50);
    ByteBuffer noDictionaryValKeyStoreDataHolder = ByteBuffer.allocate(columnarKeyBlockData.length);
    noDictionaryValKeyStoreDataHolder.put(columnarKeyBlockData);
    noDictionaryValKeyStoreDataHolder.flip();
    while (noDictionaryValKeyStoreDataHolder.hasRemaining()) {
        short dataLength = noDictionaryValKeyStoreDataHolder.getShort();
        byte[] noDictionaryValKeyData = new byte[dataLength];
        noDictionaryValKeyStoreDataHolder.get(noDictionaryValKeyData);
        columnarKeyBlockDataList.add(noDictionaryValKeyData);
    }
    return columnarKeyBlockDataList;

}

From source file:com.tera.common.util.ConsolePrinter.java

/**
 * Convert data from given ByteBuffer to hex
 * /*from  w  w w.  j a  va 2s  .  co m*/
 * @param data
 * @return hex
 */
public static String toHex(ByteBuffer data) {
    StringBuilder result = new StringBuilder();
    int counter = 0;
    int b;
    while (data.hasRemaining()) {
        if (counter % 16 == 0)
            result.append(String.format("%04X: ", counter));

        b = data.get() & 0xff;
        result.append(String.format("%02X ", b));

        counter++;
        if (counter % 16 == 0) {
            result.append("  ");
            toText(data, result, 16);
            result.append("\n");
        }
    }
    int rest = counter % 16;
    if (rest > 0) {
        for (int i = 0; i < 17 - rest; i++) {
            result.append("   ");
        }
        toText(data, result, rest);
    }
    return result.toString();
}

From source file:org.bimserver.collada.SupportFunctions.java

public static List<String> intBufferToStringList(ByteBuffer buffer, Format formatter) {
    List<Integer> list = new ArrayList<Integer>();
    while (buffer.hasRemaining())
        list.add(new Integer(buffer.getInt()));
    // Get the data as a list of String objects.
    return SupportFunctions.listToStringList(list, formatter);
}

From source file:com.tinspx.util.io.callbacks.SegmentingCallbackTest.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private static void testWriteFull(Charset charset, char[] chars, byte[] bytes, CAWriter writer) {
    writer.clearAndReset();/*from w ww .j  a va  2 s . com*/
    SegmentingCallback s = SegmentingCallback.create(new DecodingOutputStream(charset),
            new WriterListener(writer));
    s.onContentStart(null);
    ByteBuffer buf = ByteBuffer.wrap(bytes);
    s.onContent(null, buf);
    assertFalse(buf.hasRemaining());
    s.onContentComplete(null);
    assertArrayEquals(chars, writer.toCharArray());
}

From source file:io.blobkeeper.file.util.FileUtils.java

public static long getCrc(@NotNull File file) {
    CRC32 crc = new CRC32();

    while (true) {
        ByteBuffer buffer = ByteBuffer.allocate(CHUNK_SIZE);
        while (buffer.hasRemaining()) {
            int bytes = 0;
            try {
                bytes = file.getFileChannel().read(buffer);
            } catch (IOException e) {
                log.error("Can't read blob file " + file, e);
                throw new IllegalArgumentException(e);
            }//from   w w  w  .j ava  2s.c o m
            if (bytes < 0) {
                break;
            }
        }
        buffer.flip();
        if (buffer.remaining() == 0) {
            break;
        } else {
            crc.update(buffer.array());
        }
    }

    return crc.getValue();
}

From source file:com.thinkberg.webdav.Util.java

public static long copyStream(final InputStream is, final OutputStream os) throws IOException {
    ReadableByteChannel rbc = Channels.newChannel(is);
    WritableByteChannel wbc = Channels.newChannel(os);

    int bytesWritten = 0;
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
    while (rbc.read(buffer) != -1) {
        buffer.flip();//  w w  w. ja v a2  s  . c  o  m
        bytesWritten += wbc.write(buffer);
        buffer.compact();
    }
    buffer.flip();
    while (buffer.hasRemaining()) {
        bytesWritten += wbc.write(buffer);
    }

    rbc.close();
    wbc.close();

    return bytesWritten;
}

From source file:com.tinspx.util.io.callbacks.SegmentingCallbackTest.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private static void testWriteSingle(Charset charset, char[] chars, byte[] bytes, CAWriter writer) {
    writer.clearAndReset();//from  w  ww  . j  a va 2 s .c  om
    SegmentingCallback s = SegmentingCallback.create(new DecodingOutputStream(charset),
            new WriterListener(writer));
    s.onContentStart(null);
    for (byte b : bytes) {
        ByteBuffer buf = ByteBuffer.wrap(new byte[] { b });
        s.onContent(null, buf);
        assertFalse(buf.hasRemaining());
    }
    s.onContentComplete(null);
    assertArrayEquals(chars, writer.toCharArray());
}