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.offbynull.portmapper.pcp.PcpOption.java

/**
 * Constructs a {@link PcpOption} object.
 * @param code option code/*from w ww .ja v  a  2 s.  c o  m*/
 * @param data option data
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if {@code code} is {@code < 0 || > 255}, or if the amount of data remaining in {@code data} is
 * {@code > 65535}
 */
public PcpOption(int code, ByteBuffer data) {
    Validate.inclusiveBetween(0, 255, code);
    Validate.inclusiveBetween(0, 65535, data.remaining());
    Validate.notNull(data);

    this.code = code;
    this.length = data.remaining();
    this.data = ByteBuffer.allocate(length + (length % 4)).put(data).asReadOnlyBuffer(); // NOPMD
}

From source file:org.apache.nifi.io.nio.consumer.AbstractStreamConsumer.java

@Override
public final void addFilledBuffer(final ByteBuffer buffer) {
    if (isConsumerFinished()) {
        buffer.clear();/*w w  w  .  j av a 2  s  .c  o m*/
        bufferPool.returnBuffer(buffer, buffer.remaining());
    } else {
        filledBuffers.add(buffer);
    }
}

From source file:com.navercorp.pinpoint.collector.receiver.thrift.udp.SpanStreamUDPPacketHandlerFactory.java

private byte[] getComponentData(ByteBuffer buffer, HeaderTBaseDeserializer deserializer) {
    if (buffer.remaining() < 2) {
        logger.warn("Can't available {} fixed buffer.", 2);
        return null;
    }//w  w w  .  ja v a2 s  .c o  m

    int componentSize = 0xffff & buffer.getShort();
    if (buffer.remaining() < componentSize) {
        logger.warn("Can't available {} fixed buffer.", buffer.remaining());
        return null;
    }

    byte[] componentData = new byte[componentSize];
    buffer.get(componentData);

    return componentData;
}

From source file:com.facebook.infrastructure.net.io.StartState.java

protected byte[] doRead(ByteBuffer buffer) throws IOException, ReadNotCompleteException {
    SocketChannel socketChannel = stream_.getStream();
    int bytesRead = socketChannel.read(buffer);
    if (bytesRead == -1 && buffer.remaining() > 0) {
        throw new IOException("Reached an EOL or something bizzare occured. Reading from: "
                + socketChannel.socket().getInetAddress() + " BufferSizeRemaining: " + buffer.remaining());
    }/* w  ww. jav a2s. co m*/
    if (buffer.remaining() == 0) {
        morphState();
    } else {
        throw new ReadNotCompleteException(
                "Specified number of bytes have not been read from the Socket Channel");
    }
    return ArrayUtils.EMPTY_BYTE_ARRAY;
}

From source file:gridool.util.GridMessageBuffer.java

public void read(final ByteBuffer buf) {
    if (msgSize < 0) {// read a message header (and data) here
        final int remaining = buf.remaining();
        if (remaining <= 0) {
            return;
        }/*w w w . j a  v a2s .c o  m*/
        final int missing = 4 - msgBuf.size();
        if (remaining < missing) {
            msgBuf.write(buf, remaining);
            return;
        } else {
            msgBuf.write(buf, missing);
            assert (msgBuf.size() <= 4) : msgBuf.size();
            if (msgBuf.size() == 4) {
                final int size = Primitives.getInt(msgBuf.getInternalArray(), 0);
                assert (size > 0) : size;
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Receiving message [id=" + this.hashCode() + ", size=" + size + ']');
                }
                this.msgSize = size;
                msgBuf.reset(size);
            }
        }
    }
    final int remaining = buf.remaining();
    if (remaining > 0) {
        final int missing = msgSize - msgBuf.size();
        if (missing > 0) {// read up to message size
            msgBuf.write(buf, remaining > missing ? missing : remaining);
            if (LOG.isTraceEnabled()) {
                LOG.trace("Receiving message [id=" + this.hashCode() + ", size=" + msgSize + ", msgBuf.size()="
                        + msgBuf.size() + "], filled=" + isFilled());
            }
        }
    }
}

From source file:org.apache.cassandra.dht.ByteOrderedPartitioner.java

public BytesToken getToken(ByteBuffer key) {
    if (key.remaining() == 0)
        return MINIMUM;
    return new BytesToken(key);
}

From source file:name.martingeisse.stackd.server.section.storage.CassandraSectionStorage.java

@Override
public Map<SectionDataId, byte[]> loadSectionRelatedObjects(final Collection<? extends SectionDataId> ids) {
    if (logger.isDebugEnabled()) {
        logger.debug("loading section-related objects: " + StringUtils.join(ids, ", "));
    }/*from  w w w.j  a va  2  s.  c o  m*/
    try {

        // convert the IDs to an array of strings
        Object[] idTexts = new String[ids.size()];
        {
            int i = 0;
            for (SectionDataId id : ids) {
                idTexts[i] = id.getIdentifierText();
                i++;
            }
        }

        // fetch the rows
        final Map<SectionDataId, byte[]> result = new HashMap<>();
        for (final Row row : fetch(QueryBuilder.in("id", idTexts))) {
            final String id = row.getString("id");
            final ByteBuffer dataBuffer = row.getBytes("data");
            final byte[] data = new byte[dataBuffer.remaining()];
            dataBuffer.get(data);
            result.put(new SectionDataId(id), data);
        }
        return result;

    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.cassandra.db.marshal.UUIDType.java

public UUID compose(ByteBuffer bytes) {

    bytes = bytes.slice();//from   www  .j ava2s.  c  om
    if (bytes.remaining() < 16)
        return new UUID(0, 0);
    return new UUID(bytes.getLong(), bytes.getLong());
}

From source file:org.apache.avro.file.XZCodec.java

private void writeAndClose(ByteBuffer data, OutputStream to) throws IOException {
    byte[] input = data.array();
    int offset = data.arrayOffset() + data.position();
    int length = data.remaining();
    try {/*  w ww . j a va2s  .co  m*/
        to.write(input, offset, length);
    } finally {
        to.close();
    }
}

From source file:org.apache.hadoop.hbase.io.hfile.bucket.FileMmapEngine.java

/**
 * Transfers data from the given byte buffer to file
 * @param srcBuffer the given byte buffer from which bytes are to be read
 * @param offset The offset in the file where the first byte to be written
 * @throws IOException/*from   w  w w .j  a  v a  2s  .  c o m*/
 */
@Override
public void write(ByteBuffer srcBuffer, long offset) throws IOException {
    assert srcBuffer.hasArray();
    bufferArray.putMultiple(offset, srcBuffer.remaining(), srcBuffer.array(), srcBuffer.arrayOffset());
}