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.acciente.oacc.sql.internal.StrongCleanablePasswordEncryptor.java

private byte[] getCleanedBytes(char[] password) {
    final ByteBuffer byteBuffer = StandardCharsets.UTF_8
            .encode(CharBuffer.wrap(Normalizer.normalizeToNfc(password)));
    final byte[] byteArray = new byte[byteBuffer.remaining()];
    byteBuffer.get(byteArray);/*from  w  ww .  jav  a 2s .c  o  m*/
    Arrays.fill(byteBuffer.array(), (byte) 0);
    return byteArray;
}

From source file:com.bittorrent.mpetazzoni.client.storage.FileStorage.java

@Override
public int read(ByteBuffer buffer, long offset) throws IOException {
    int requested = buffer.remaining();

    if (offset + requested > this.size) {
        throw new IllegalArgumentException("Invalid storage read request!");
    }/*from ww  w .ja v  a 2  s.c  o m*/

    int bytes = this.channel.read(buffer, offset);
    if (bytes < requested) {
        throw new IOException("Storage underrun!");
    }

    return bytes;
}

From source file:com.flexive.core.stream.BinaryDownloadProtocol.java

/**
 * {@inheritDoc}/*from   w  w w  .j a v a2s  .co m*/
 */
@Override
public boolean sendStream(ByteBuffer buffer) throws IOException {
    if (buffer.remaining() <= 0 || bin == null)
        return true; //should not happen but possible ...
    if (pre_read > 0) {
        buffer.put(_buffer, 0, pre_read); //size should not matter since its only 100 bytes
        pre_read = 0;
    }
    int max = Math.min(buffer.remaining(), _buffer.length);
    int read = bin.read(_buffer, 0, max);
    if (read == -1) {
        return false;
    }
    buffer.put(_buffer, 0, read);
    return true;
}

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

public void validate(ByteBuffer bytes) {
    if ((bytes.remaining() != 0) && (bytes.remaining() != 16)) {
        throw new MarshalException("UUIDs must be exactly 16 bytes");
    }//from   w w  w. j  a v  a  2 s  .co m
}

From source file:org.apache.hadoop.hive.metastore.FileMetadataHandler.java

/**
 * Caches the file metadata for a particular file.
 * @param fileId File id./*from   w ww .j ava 2  s.  c o m*/
 * @param fs The filesystem of the file.
 * @param path Path to the file.
 */
public void cacheFileMetadata(long fileId, FileSystem fs, Path path) throws IOException, InterruptedException {
    // ORC is in ql, so we cannot do anything here. For now, all the logic is in the proxy.
    ByteBuffer[] cols = fileFormatProxy.getAddedColumnsToCache();
    ByteBuffer[] vals = (cols == null) ? null : new ByteBuffer[cols.length];
    ByteBuffer metadata = fileFormatProxy.getMetadataToCache(fs, path, vals);
    LOG.info("Caching file metadata for " + path + ", size " + metadata.remaining());
    store.storeFileMetadata(fileId, metadata, cols, vals);
}

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

public String getString(ByteBuffer bytes) {
    if (bytes.remaining() == 0) {
        return "";
    }//from  www .ja v a2s  .c om
    if (bytes.remaining() != 16) {
        throw new MarshalException("UUIDs must be exactly 16 bytes");
    }
    UUID uuid = compose(bytes);
    return uuid.toString();
}

From source file:com.github.cambierr.lorawanpacket.semtech.TxAck.java

public TxAck(byte[] _randoms, ByteBuffer _raw) throws MalformedPacketException {
    super(_randoms, PacketType.TX_ACK);

    byte[] txt = new byte[_raw.remaining()];
    _raw.get(txt);/* w ww. ja  v  a 2 s. co m*/

    JSONObject jo;

    try {
        jo = new JSONObject(new String(txt));
    } catch (JSONException ex) {
        throw new MalformedPacketException("malformed json");
    }

    if (!jo.has("txpk_ack") || !jo.get("txpk_ack").getClass().equals(JSONObject.class)) {
        throw new MalformedPacketException("malformed json");
    }

    if (!jo.getJSONObject("txpk_ack").has("error")) {
        throw new MalformedPacketException("malformed json");
    }

    error = Error.parse(jo.getJSONObject("txpk_ack").getString("error"));
}

From source file:me.xingrz.prox.tcp.tunnel.RemoteTunnel.java

/**
 * {@inheritDoc}// ww w  .j a  v a 2 s  . c  o  m
 * {@link me.xingrz.prox.tcp.tunnel.RemoteTunnel}  {@link #write(java.nio.ByteBuffer)}
 * ? {@link #connect(java.net.InetSocketAddress)} ??
 */
@Override
public boolean write(ByteBuffer buffer) {
    if (!isEstablished()) {
        logger.v("Buffered %d bytes of write since tunnel is not established", buffer.remaining());
        keepRemaining(buffer);
        return false;
    }

    return super.write(buffer);
}

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

@Override
public final void process() throws IOException {
    if (isConsumerFinished()) {
        return;//from   ww w .  j a v a2s.  com
    }
    if (streamEnded.get() && filledBuffers.isEmpty()) {
        consumerEnded.set(true);
        onConsumerDone();
        return;
    }
    final ByteBuffer buffer = filledBuffers.poll();
    if (buffer != null) {
        final int bytesToProcess = buffer.remaining();
        try {
            processBuffer(buffer);
        } finally {
            buffer.clear();
            bufferPool.returnBuffer(buffer, bytesToProcess);
        }
    }
}

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

@Override
public byte[] loadSectionRelatedObject(final SectionDataId id) {
    try {/*from w  w w .ja  v a 2  s  . c  om*/
        for (final Row row : fetch(QueryBuilder.eq("id", id.getIdentifierText()))) {
            final ByteBuffer dataBuffer = row.getBytes("data");
            final byte[] data = new byte[dataBuffer.remaining()];
            dataBuffer.get(data);
            return data;
        }
        return null;
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}