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.bimserver.collada.SupportFunctions.java

public static List<String> floatBufferToStringList(ByteBuffer buffer, Format formatter) {
    // Transform the array into a list.
    List<Float> list = new ArrayList<Float>();
    while (buffer.hasRemaining())
        list.add(new Float(buffer.getFloat()));
    // Get the data as a list of String objects.
    return SupportFunctions.listToStringList(list, formatter);
}

From source file:com.act.lcms.v2.fullindex.Searcher.java

private static Set<Long> unionIdBuffers(byte[][] idBytes) {
    /* TODO: this doesn't take advantage of the fact that all of the ids are in sorted order in every idBytes sub-array.
     * We should be able to exploit that.  For now, we'll just start by hashing the ids. */
    Set<Long> uniqueIds = new HashSet<>();
    for (int i = 0; i < idBytes.length; i++) {
        assert (idBytes[i] != null);
        ByteBuffer idsBuffer = ByteBuffer.wrap(idBytes[i]);
        while (idsBuffer.hasRemaining()) {
            uniqueIds.add(idsBuffer.getLong());
        }/* w ww  . j a  v  a 2 s.co m*/
    }
    return uniqueIds;
}

From source file:jenkins.plugins.publish_over_cifs.CifsHostConfiguration.java

@SuppressWarnings("PMD.EmptyCatchBlock")
private static String encode(final String raw) {
    if (raw == null)
        return null;
    final StringBuilder encoded = new StringBuilder(raw.length() * ESCAPED_BUILDER_SIZE_MULTIPLIER);
    final CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
    final CharBuffer buffer = CharBuffer.allocate(1);
    for (final char c : raw.toCharArray()) {
        if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            encoded.append(c);//from ww w .  j av a 2 s.  c  om
        } else {
            buffer.put(0, c);
            buffer.rewind();
            try {
                final ByteBuffer bytes = encoder.encode(buffer);
                while (bytes.hasRemaining()) {
                    final byte oneByte = bytes.get();
                    encoded.append('%');
                    encoded.append(toDigit((oneByte >> HI_TO_LOW_NIBBLE_BIT_SHIFT) & LOW_NIBBLE_BIT_MASK));
                    encoded.append(toDigit(oneByte & LOW_NIBBLE_BIT_MASK));
                }
            } catch (final CharacterCodingException cce) {
                throw new BapPublisherException(Messages.exception_encode_cce(cce.getLocalizedMessage()), cce);
            }
        }
    }
    return encoded.toString();
}

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

@SuppressWarnings({ "rawtypes", "unchecked" })
private static void testWriteParts(Charset charset, char[] chars, byte[] bytes, CAWriter writer) {
    writer.clearAndReset();//from  w w  w. j  a v a  2  s. c o  m
    SegmentingCallback s = SegmentingCallback.create(new DecodingOutputStream(charset),
            new WriterListener(writer));
    s.onContentStart(null);
    for (Range<Integer> r : generateCloseOpen(bytes.length, 13)) {
        ByteBuffer buf = ByteBuffer.wrap(bytes, r.lowerEndpoint(), r.upperEndpoint() - r.lowerEndpoint());
        s.onContent(null, buf);
        assertFalse(buf.hasRemaining());
    }
    s.onContentComplete(null);
    assertArrayEquals(chars, writer.toCharArray());
}

From source file:com.easemob.dataexport.utils.JsonUtils.java

public static Object fromByteBuffer(ByteBuffer byteBuffer, Class<?> clazz) {
    if ((byteBuffer == null) || !byteBuffer.hasRemaining()) {
        return null;
    }//  w ww.j a v a 2  s.c o m
    if (clazz == null) {
        clazz = Object.class;
    }

    Object obj = null;
    try {
        obj = smileMapper.readValue(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(),
                byteBuffer.remaining(), clazz);
    } catch (Exception e) {
        LOG.error("Error parsing SMILE bytes", e);
    }
    return obj;
}

From source file:com.act.lcms.MzMLParser.java

protected static List<Double> base64ToDoubleList(String b64) {
    byte[] decodedBytes = Base64.getDecoder().decode(b64);
    ByteBuffer buf = ByteBuffer.wrap(decodedBytes).order(ByteOrder.LITTLE_ENDIAN);
    List<Double> values = new ArrayList<>(decodedBytes.length / 8);
    while (buf.hasRemaining()) {
        values.add(buf.getDouble());//from   ww  w . j  a  v a  2s. co m
    }
    return values;
}

From source file:StreamUtil.java

/**
 * Copies the content read from InputStream to OutputStream. Uses the NIO Channels to copy.
 * @param is The InputStream that is read.
 * @param os The OutputStream where the data is written.
 * @throws IOException/*  w w w.ja  v  a 2 s  .c o m*/
 */
public static void copy(final InputStream is, final OutputStream os) throws IOException {
    final ReadableByteChannel inChannel = Channels.newChannel(is);
    final WritableByteChannel outChannel = Channels.newChannel(os);

    try {
        final ByteBuffer buffer = ByteBuffer.allocate(65536);
        while (true) {
            int bytesRead = inChannel.read(buffer);
            if (bytesRead == -1)
                break;
            buffer.flip();
            while (buffer.hasRemaining())
                outChannel.write(buffer);
            buffer.clear();
        }
    } finally {
        try {
            inChannel.close();
        } catch (IOException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
        try {
            outChannel.close();
        } catch (IOException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }
}

From source file:org.usergrid.utils.JsonUtils.java

public static JsonNode nodeFromByteBuffer(ByteBuffer byteBuffer) {
    if ((byteBuffer == null) || !byteBuffer.hasRemaining()) {
        return null;
    }//from w w w  .  ja  v a2s.com

    JsonNode obj = null;
    try {
        obj = smileMapper.readValue(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(),
                byteBuffer.remaining(), JsonNode.class);
    } catch (Exception e) {
        logger.error("Error parsing SMILE bytes", e);
    }
    return obj;
}

From source file:org.usergrid.utils.JsonUtils.java

public static Object fromByteBuffer(ByteBuffer byteBuffer, Class<?> clazz) {
    if ((byteBuffer == null) || !byteBuffer.hasRemaining()) {
        return null;
    }/*from w w  w  .j  a  v  a2 s .c  o  m*/
    if (clazz == null) {
        clazz = Object.class;
    }

    Object obj = null;
    try {
        obj = smileMapper.readValue(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(),
                byteBuffer.remaining(), clazz);
    } catch (Exception e) {
        logger.error("Error parsing SMILE bytes", e);
    }
    return obj;
}

From source file:org.carlspring.strongbox.storage.metadata.nuget.TempNupkgFile.java

/**
 * Copies data from one channel to another
 *
 * @param src/*  ww w .ja  va2  s.c o m*/
 *            channel source
 * @param dest
 *            destination channel
 * @throws IOException
 *             input / output error
 */
private static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);

    while (src.read(buffer) != -1) {
        buffer.flip();
        dest.write(buffer);
        buffer.compact();
    }

    buffer.flip();

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