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 Buffer position(int newPosition) 

Source Link

Document

Sets the position of this buffer.

Usage

From source file:com.mellanox.jxio.Msg.java

private ByteBuffer createSubBuffer(int position, int limit, ByteBuffer buf) {
    ByteBuffer sub;//  w w  w .  java  2  s.com
    buf.position(position);
    buf.limit(limit);
    sub = buf.slice();
    return sub;
}

From source file:org.sglover.alfrescoextensions.common.HasherImpl.java

private String getHash(ByteBuffer bytes, int start, int end, MessageDigest digest)
        throws NoSuchAlgorithmException {
    int saveLimit = bytes.limit();
    bytes.limit(end + 1);//from www.j a  v a  2  s  .  c o  m

    bytes.mark();
    bytes.position(start);

    digest.reset();
    digest.update(bytes);
    byte[] array = digest.digest();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < array.length; ++i) {
        sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
    }

    bytes.limit(saveLimit);
    bytes.reset();

    return sb.toString();
}

From source file:com.meidusa.venus.benchmark.FileLineRandomData.java

private final String readLine(ByteBuffer buffer) {
    if (closed)/*from  w  ww. j av a2  s.c  om*/
        throw new IllegalStateException("file closed..");
    ByteBuffer tempbuffer = localTempBuffer.get();
    tempbuffer.position(0);
    tempbuffer.limit(tempbuffer.capacity());
    byte c = -1;
    boolean eol = false;
    while (!eol) {
        switch (c = buffer.get()) {
        case -1:
        case '\n':
            eol = true;
            break;
        case '\r':
            eol = true;
            int cur = buffer.position();
            if ((buffer.get()) != '\n') {
                buffer.position(cur);
            }
            break;
        default:
            tempbuffer.put(c);
            break;
        }
    }

    if ((c == -1) && (tempbuffer.position() == 0)) {
        return null;
    }
    tempbuffer.flip();

    try {
        return new String(tempbuffer.array(), encoding);
    } catch (UnsupportedEncodingException e) {
        return new String(tempbuffer.array());
    }

}

From source file:edu.hawaii.soest.kilonalu.adcp.EnsembleHeader.java

/**
 *  A method that returns the offset for the given Data Type number.  
 *  For instance, The offset for Data Type #1 (FixedLeader) will be returned
 *  by calling this method with a dataTypeNumber argument of 1.
 *
 * @param dataTypeNumber  the number of the Data Type desired (as a ByteBuffer)
 *///w ww  . j  av  a  2s  .c  om
protected ByteBuffer getDataTypeOffset(int dataTypeNumber) {

    // prepare the ByteBuffer for reading
    this.dataTypeOffsets.limit(this.dataTypeOffsets.capacity());
    // read the offset by setting the position based on the desired type
    dataTypeOffsets.position(((dataTypeNumber * 2) - 2));
    byte[] offsetBytes = new byte[2];
    ByteBuffer dataTypeOffset = dataTypeOffsets.get(offsetBytes);
    dataTypeOffset.position(dataTypeOffset.position() - 2);
    return dataTypeOffset;
}

From source file:ch.cyberduck.core.cryptomator.CryptoInputStream.java

private int readNextChunk() throws IOException {
    final ByteBuffer ciphertextBuf = ByteBuffer.allocate(chunkSize);
    final int read = IOUtils.read(proxy, ciphertextBuf.array());
    if (read == 0) {
        return IOUtils.EOF;
    }//  w  w  w  . j av a2 s .co m
    ciphertextBuf.position(read);
    ciphertextBuf.flip();
    try {
        buffer = cryptor.fileContentCryptor().decryptChunk(ciphertextBuf, chunkIndexOffset++, header, true);
    } catch (CryptoException e) {
        throw new IOException(e.getMessage(), new CryptoAuthenticationException(e.getMessage(), e));
    }
    return read;
}

From source file:edu.tamu.tcat.crypto.bouncycastle.SecureTokenImpl.java

private byte[] createToken(ByteBuffer content) throws NoSuchAlgorithmException {
    byte[] bytes = new byte[content.remaining()];
    ByteBuffer tokenBytes = ByteBuffer.wrap(bytes);
    content.position(0);
    tokenBytes.put(content);//from   www.  jav  a  2  s  . c  om
    return bytes;
}

From source file:ja.lingo.engine.mergedindex.ChannelMergedIndex.java

public ChannelMergedIndex(String fileName, Map<String, IDictionaryIndex> indexFileNameToReaderMap)
        throws IOException {
    Arguments.assertNotNull("fileName", fileName);
    Arguments.assertNotNull("indexFileNameToReaderMap", indexFileNameToReaderMap);

    this.fileName = fileName;

    fisMappedByteBufferWrapper = new MappedByteBufferWrapper(fileName);

    ISliceReader sliceReader = new ByteBufferSliceReader(fisMappedByteBufferWrapper.getMappedByteBuffer());

    // read position + fill readerIntIdToReaderMap
    {//from  w  w w  . jav a2s  .c  o  m
        ByteBuffer buffer = sliceReader.getSlice(SLICE_READER_INT_ID_TO_READER_ID_MAP);
        byte[] bytes = new byte[buffer.limit()];
        buffer.position(0);
        buffer.get(bytes);

        try {
            readerIntIdToReaderMap = _deserializeReaderIdToReaderMap(
                    new DataInputStream(new ByteArrayInputStream(bytes)), indexFileNameToReaderMap);
        } catch (IOException e) {
            Files.close(fisMappedByteBufferWrapper);
            throw e;
        }
    }

    groupsIndexLengthIntReader = new IntBufferIntReader(
            sliceReader.getSlice(SLICE_GROUPS_INDEX_LENGTH_LIST).asIntBuffer());
    groupsIntReader = new IntBufferIntReader(sliceReader.getSlice(SLICE_GROUPS_LIST).asIntBuffer());
}

From source file:com.meidusa.venus.benchmark.FileLineRandomData.java

private void goNextNewLineHead(ByteBuffer buffer, int position) {
    if (closed)//from   w w w  .  j a  v  a2s. co  m
        throw new IllegalStateException("file closed..");
    buffer.position(position);
    boolean eol = false;
    int c = -1; // NOPMD by structchen on 13-10-21 ?12:22
    while (!eol) {
        switch (c = buffer.get()) {
        case -1:
        case '\n':
            eol = true;
            break;
        case '\r':
            eol = true;
            int cur = buffer.position();
            if ((buffer.get()) != '\n') {
                buffer.position(cur);
            }
            break;
        }
        if (!eol) {
            if (position > 0) {
                buffer.position(--position);
            } else {
                eol = true;
            }
        }
    }
}

From source file:com.spotify.heroic.metric.datastax.schema.legacy.MapSerializer.java

private <T> T next(ByteBuffer buffer, TypeSerializer<T> serializer) throws IOException {
    final short segment = buffer.getShort();
    final ByteBuffer slice = buffer.slice();
    slice.limit(segment);/*from   ww  w.jav a2 s.co m*/
    final T value = serializer.deserialize(slice);

    buffer.position(buffer.position() + segment);
    return value;
}

From source file:ch.cyberduck.core.sds.triplecrypt.CryptoInputStream.java

private int readNextChunk() throws IOException {
    final ByteBuffer ciphertextBuf = ByteBuffer.allocate(SDSSession.DEFAULT_CHUNKSIZE);
    final int read = IOUtils.read(proxy, ciphertextBuf.array());
    if (lastread == 0) {
        return IOUtils.EOF;
    }//from w  w  w  .  j a v  a  2  s  .com
    ciphertextBuf.position(read);
    ciphertextBuf.flip();
    try {
        final PlainDataContainer pDataContainer;
        if (read == 0) {
            final PlainDataContainer c1 = cipher
                    .processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
            final PlainDataContainer c2 = cipher.doFinal(new EncryptedDataContainer(null, tag));
            pDataContainer = new PlainDataContainer(ArrayUtils.addAll(c1.getContent(), c2.getContent()));
        } else {
            pDataContainer = cipher
                    .processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
        }
        final byte[] content = pDataContainer.getContent();
        buffer = ByteBuffer.allocate(content.length);
        buffer.put(content);
        buffer.flip();
        lastread = read;
        return content.length;
    } catch (CryptoException e) {
        throw new IOException(e);
    }
}