Example usage for java.nio ByteBuffer position

List of usage examples for java.nio ByteBuffer position

Introduction

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

Prototype

public final int position() 

Source Link

Document

Returns the position of this buffer.

Usage

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/>//from  w  w w  . j a va  2  s . co  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;
}

From source file:com.koda.integ.hbase.util.CacheableSerializer.java

@Override
public void write(ByteBuffer buf, Cacheable obj) throws IOException {
    if (deserializer.get() == null) {
        CacheableDeserializer<Cacheable> des = obj.getDeserializer();
        deserializer.compareAndSet(null, des);
    }// ww w  .j  av a2  s. com
    // Serializer does not honor current buffer position
    int len = obj.getSerializedLength();
    int pos = buf.position();
    obj.serialize(buf);
    buf.limit(len + pos);
    buf.position(len + pos);
}

From source file:io.protostuff.JsonOutput.java

/**
 * Writes a ByteBuffer field.//ww w. ja  va2s .com
 */
@Override
public void writeBytes(int fieldNumber, ByteBuffer value, boolean repeated) throws IOException {
    writeByteRange(false, fieldNumber, value.array(), value.arrayOffset() + value.position(), value.remaining(),
            repeated);
}

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

/**
 * Finishes a multiple-part operation. The data is encrypted or decrypted,
 * depending on how this cipher was initialized.
 * <p/>//from   w w  w  .  j a va  2 s. co  m
 * 
 * The result is stored in the output buffer. Upon return, the output buffer's
 * position will have advanced by n, where 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.
 * <p/>
 * 
 * Upon finishing, this method resets this cipher object to the state it was
 * in when previously initialized. That is, the object is available to encrypt
 * or decrypt more data.
 * <p/>
 * 
 * If any exception is thrown, this cipher object need to be reset before it 
 * can be used again.
 * 
 * @param output the output ByteBuffer
 * @return int number of bytes stored in <code>output</code>
 * @throws ShortBufferException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 */
public int doFinal(ByteBuffer output)
        throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
    checkState();
    Preconditions.checkArgument(output.isDirect(), "Direct buffer is required.");
    int len = OpensslCipherNative.doFinal(context, output, output.position(), output.remaining());
    output.position(output.position() + len);
    return len;
}

From source file:com.arpnetworking.tsdcore.sinks.KairosDbSink.java

private void addChunk(final ByteArrayOutputStream chunkStream, final ByteBuffer currentChunk,
        final Collection<byte[]> completedChunks) {
    final byte[] nextChunk = chunkStream.toByteArray();
    final int nextChunkSize = nextChunk.length;
    if (currentChunk.position() + nextChunkSize > _maxRequestSize) {
        if (currentChunk.position() > HEADER_BYTE_LENGTH) {
            // TODO(vkoskela): Add chunk size metric. [MAI-?]

            // Copy the relevant part of the buffer
            currentChunk.put(currentChunk.position() - 1, FOOTER);
            completedChunks.add(Arrays.copyOf(currentChunk.array(), currentChunk.position()));

            // Truncate all but the beginning '[' to prepare the next entries
            currentChunk.clear();//from  w  w w.  j  ava 2s  .  c o m
            currentChunk.put(HEADER);
        } else {
            CHUNK_TOO_BIG_LOGGER.warn().setMessage("First chunk too big").addData("sink", getName())
                    .addData("bufferLength", currentChunk.position()).addData("nextChunkSize", nextChunkSize)
                    .addData("maxRequestSIze", _maxRequestSize).log();
        }
    }

    currentChunk.put(nextChunk);
    currentChunk.put(SEPARATOR);
    chunkStream.reset();
}

From source file:com.l2jfree.security.NewCipher.java

/**
 * Enciphers buffer's contents in blocks of 8 bytes using a Blowfish key.<BR>
 * Buffer's position will not be changed. <BR>
 * <BR>//from  w ww  .j  a  va  2  s .c o m
 * If the last block contains less than 8 bytes, they are not enciphered. <BR>
 * <BR>
 * It is assumed that the packet's body starts at current position.
 * 
 * @param buf a byte buffer
 * @param size packet's size
 */
@Override
public void encipher(ByteBuffer buf, final int size) {
    encipher(buf, buf.position(), size);
}

From source file:com.l2jfree.security.NewCipher.java

/**
 * Deciphers buffer's contents in blocks of 8 bytes using a Blowfish key.<BR>
 * Buffer's position will not be changed. <BR>
 * <BR>/*from   www .java 2  s. co  m*/
 * If the last block contains less than 8 bytes, they are not deciphered. <BR>
 * <BR>
 * It is assumed that the packet's body starts at current position.
 * 
 * @param buf a byte buffer
 * @param size packet's size
 */
@Override
public void decipher(ByteBuffer buf, final int size) {
    decipher(buf, buf.position(), size);
}

From source file:com.github.neoio.net.message.staging.file.TestFileMessageStaging.java

@Test
public void test_primaryStage() throws Exception {
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    buffer.put("Hello World".getBytes());
    buffer.flip();//from w  ww.  ja  v  a 2  s.  c  o m
    staging.writePrimaryStaging(buffer, "Hello World".getBytes().length);

    buffer.clear();
    staging.getPrimaryStage().read(buffer);
    Assert.assertEquals("Hello World".getBytes().length, staging.getPrimaryStageSize());
    Assert.assertEquals("Hello World", new String(buffer.array(), 0, buffer.position()));

    staging.resetPrimaryStage();
    Assert.assertEquals(0, staging.getPrimaryStageSize());
}

From source file:org.eclipse.jgit.lfs.server.fs.LfsServerTest.java

/**
 * Creates a file with random content, repeatedly writing a random string of
 * 4k length to the file until the file has at least the specified length.
 *
 * @param f/*w w w. j  a v  a  2s.c om*/
 *            file to fill
 * @param size
 *            size of the file to generate
 * @return length of the generated file in bytes
 * @throws IOException
 */
protected long createPseudoRandomContentFile(Path f, long size) throws IOException {
    SecureRandom rnd = new SecureRandom();
    byte[] buf = new byte[4096];
    rnd.nextBytes(buf);
    ByteBuffer bytebuf = ByteBuffer.wrap(buf);
    try (FileChannel outChannel = FileChannel.open(f, StandardOpenOption.CREATE_NEW,
            StandardOpenOption.WRITE)) {
        long len = 0;
        do {
            len += outChannel.write(bytebuf);
            if (bytebuf.position() == 4096) {
                bytebuf.rewind();
            }
        } while (len < size);
    }
    return Files.size(f);
}

From source file:com.intel.chimera.cipher.Openssl.java

/**
 * Continues a multiple-part encryption or decryption operation. The data
 * is encrypted or decrypted, depending on how this cipher was initialized.
 * <p/>//from  w ww .j  av a 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();
    Utils.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required.");
    int len = OpensslNative.update(context, input, input.position(), input.remaining(), output,
            output.position(), output.remaining());
    input.position(input.limit());
    output.position(output.position() + len);
    return len;
}