Example usage for java.nio ByteBuffer limit

List of usage examples for java.nio ByteBuffer limit

Introduction

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

Prototype

public final int limit() 

Source Link

Document

Returns the limit of this buffer.

Usage

From source file:cn.iie.haiep.hbase.value.Bytes.java

/**
 * Returns a new byte array, copied from the passed ByteBuffer.
 * @param bb A ByteBuffer//from  w ww.jav a  2  s  .  c om
 * @return the byte array
 */
public static byte[] toBytes(ByteBuffer bb) {
    int length = bb.limit();
    byte[] result = new byte[length];
    System.arraycopy(bb.array(), bb.arrayOffset(), result, 0, length);
    return result;
}

From source file:org.commoncrawl.util.shared.S3InputStream.java

@Override
public boolean contentAvailable(NIOHttpConnection theConnection, int itemId, String itemKey,
        NIOBufferList contentBuffer) {//from   w  ww.  j  a va 2  s . c  o m

    ByteBuffer buffer = null;
    IOException exception = null;
    //int receivedBytes = 0;
    try {
        while ((buffer = contentBuffer.read()) != null) {
            if (buffer.position() != 0) {
                buffer = buffer.slice();
            }
            //receivedBytes += buffer.remaining();
            buffer.position(buffer.limit());
            _bufferQueue.write(buffer);
        }
        _bufferQueue.flush();
    } catch (IOException e) {
        LOG.error(CCStringUtils.stringifyException(e));
        exception = e;
    }
    if (_bufferQueue.available() >= MAX_BUFFER_SIZE) {
        theConnection.disableReads();
        pausedConnection.set(theConnection);
    }
    //long nanoTimeStart = System.nanoTime();
    _writeLock.lock();
    //long nanoTimeEnd = System.nanoTime();
    //System.out.println("Received: " + receivedBytes + "for URI:" + uri + " Lock took:" + (nanoTimeEnd-nanoTimeStart));
    try {
        Condition writeCondition = _writeEvent.getAndSet(null);
        if (exception != null) {
            _eofCondition.set(true);
            _exception.set(exception);
        }
        if (writeCondition != null) {
            writeCondition.signal();
        }
    } finally {
        _writeLock.unlock();
    }
    return true;
}

From source file:com.slytechs.capture.file.editor.FileEditorImpl.java

public void replaceInPlace(final long global, final boolean copy) throws IOException {

    // existing record from buffer
    final ByteBuffer original = this.get(global, headerReader);

    // its length
    final int length = original.limit() - original.position();

    // create new buffer by copy of the original
    final PartialLoader loader = new MemoryCacheLoader(original, copy, headerReader);

    // now the replacement by region with the new buffer
    this.edits.replace(global, length, length, loader);

    this.autoflushChange(length * (copy ? 2 : 1));
}

From source file:org.dbmfs.custom.ApiFilesystem.java

public int write(String path, Object fh, boolean isWritepage, ByteBuffer buf, long offset)
        throws FuseException {
    log.info("write  path:" + path + " offset:" + offset + " isWritepage:" + isWritepage + " buf.limit:"
            + buf.limit());
    throw new FuseException("Read Only").initErrno(FuseException.EACCES);
}

From source file:org.apache.metron.profiler.hbase.SaltyRowKeyBuilderTest.java

/**
 * Build a row key that includes a single group that is an integer.
 */// www  .j  a va2  s  .  co  m
@Test
public void testRowKeyWithMixedGroups() throws Exception {
    // setup
    measurement.withGroups(Arrays.asList(200, "group1"));

    // the expected row key
    ByteBuffer buffer = ByteBuffer.allocate(100)
            .put(SaltyRowKeyBuilder.getSalt(measurement.getPeriod(), saltDivisor))
            .put(measurement.getProfileName().getBytes()).put(measurement.getEntity().getBytes())
            .put("200".getBytes()).put("group1".getBytes()).putLong(1635701L);

    buffer.flip();
    final byte[] expected = new byte[buffer.limit()];
    buffer.get(expected, 0, buffer.limit());

    // validate
    byte[] actual = rowKeyBuilder.rowKey(measurement);
    Assert.assertTrue(Arrays.equals(expected, actual));
}

From source file:org.apache.metron.profiler.hbase.SaltyRowKeyBuilderTest.java

/**
 * Build a row key that includes two groups.
 *///  w  ww  . j a  v  a  2 s  . c o  m
@Test
public void testRowKeyWithTwoGroups() throws Exception {
    // setup
    measurement.withGroups(Arrays.asList("group1", "group2"));

    // the expected row key
    ByteBuffer buffer = ByteBuffer.allocate(100)
            .put(SaltyRowKeyBuilder.getSalt(measurement.getPeriod(), saltDivisor))
            .put(measurement.getProfileName().getBytes()).put(measurement.getEntity().getBytes())
            .put("group1".getBytes()).put("group2".getBytes()).putLong(1635701L);

    buffer.flip();
    final byte[] expected = new byte[buffer.limit()];
    buffer.get(expected, 0, buffer.limit());

    // validate
    byte[] actual = rowKeyBuilder.rowKey(measurement);
    Assert.assertTrue(Arrays.equals(expected, actual));
}

From source file:com.buaa.cfs.nfs3.WriteCtx.java

public void writeData(DataOutputStream fos) throws IOException {
    Preconditions.checkState(fos != null);

    ByteBuffer dataBuffer;
    try {/*from   www  .  j a  v a 2 s.  co m*/
        dataBuffer = getData();
    } catch (Exception e1) {
        LOG.error("Failed to get request data offset:" + offset + " count:" + count + " error:" + e1);
        throw new IOException("Can't get WriteCtx.data");
    }

    byte[] data = dataBuffer.array();
    int position = dataBuffer.position();
    int limit = dataBuffer.limit();
    Preconditions.checkState(limit - position == count);
    // Modified write has a valid original count
    if (position != 0) {
        if (limit != getOriginalCount()) {
            throw new IOException("Modified write has differnt original size." + "buff position:" + position
                    + " buff limit:" + limit + ". " + toString());
        }
    }

    // Now write data
    //        fos.write(data, position, count);
}

From source file:org.apache.hadoop.crypto.OpensslCipher.java

/**
 * Continues a multiple-part encryption or decryption operation. The data
 * is encrypted or decrypted, depending on how this cipher was initialized.
 * <p/>//from ww w. j ava 2s  .c om
 * 
 * All <code>input.remaining()</code> bytes starting at 
 * <code>input.position()</code> are processed. The result is stored in
 * the output buffer.
 * <p/>
 * 
 * Upon return, the input buffer's position will be equal to its limit;
 * its limit will not have changed. The output buffer's position will have
 * advanced by n, when n is the value returned by this method; the output
 * buffer's limit will not have changed.
 * <p/>
 * 
 * If <code>output.remaining()</code> bytes are insufficient to hold the
 * result, a <code>ShortBufferException</code> is thrown.
 * 
 * @param input the input ByteBuffer
 * @param output the output ByteBuffer
 * @return int number of bytes stored in <code>output</code>
 * @throws ShortBufferException if there is insufficient space in the
 * output buffer
 */
public int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException {
    checkState();
    Preconditions.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required.");
    int len = update(context, input, input.position(), input.remaining(), output, output.position(),
            output.remaining());
    input.position(input.limit());
    output.position(output.position() + len);
    return len;
}

From source file:com.intel.chimera.codec.OpensslCipher.java

/**
 * Continues a multiple-part encryption or decryption operation. The data
 * is encrypted or decrypted, depending on how this cipher was initialized.
 * <p/>/*w ww.j  a v  a2  s  .c  o m*/
 * 
 * All <code>input.remaining()</code> bytes starting at 
 * <code>input.position()</code> are processed. The result is stored in
 * the output buffer.
 * <p/>
 * 
 * Upon return, the input buffer's position will be equal to its limit;
 * its limit will not have changed. The output buffer's position will have
 * advanced by n, when n is the value returned by this method; the output
 * buffer's limit will not have changed.
 * <p/>
 * 
 * If <code>output.remaining()</code> bytes are insufficient to hold the
 * result, a <code>ShortBufferException</code> is thrown.
 * 
 * @param input the input ByteBuffer
 * @param output the output ByteBuffer
 * @return int number of bytes stored in <code>output</code>
 * @throws ShortBufferException if there is insufficient space in the
 * output buffer
 */
public int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException {
    checkState();
    Preconditions.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required.");
    int len = OpensslCipherNative.update(context, input, input.position(), input.remaining(), output,
            output.position(), output.remaining());
    input.position(input.limit());
    output.position(output.position() + len);
    return len;
}