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 Buffer limit(int newLimit) 

Source Link

Document

Sets the limit of this buffer.

Usage

From source file:org.grouplens.lenskit.data.dao.packed.BinaryRatingDAO.java

static BinaryRatingDAO fromBuffer(ByteBuffer buffer) {
    BinaryHeader header = BinaryHeader.fromHeader(buffer);
    assert buffer.position() >= BinaryHeader.HEADER_SIZE;
    ByteBuffer dup = buffer.duplicate();
    dup.limit(header.getRatingDataSize());

    ByteBuffer tableBuffer = buffer.duplicate();
    tableBuffer.position(tableBuffer.position() + header.getRatingDataSize());
    BinaryIndexTable utbl = BinaryIndexTable.fromBuffer(header.getUserCount(), tableBuffer);
    BinaryIndexTable itbl = BinaryIndexTable.fromBuffer(header.getItemCount(), tableBuffer);

    return new BinaryRatingDAO(null, header, dup.slice(), utbl, itbl);
}

From source file:com.spectralogic.ds3client.utils.IOUtils.java

public static long copy(final InputStream inputStream, final WritableByteChannel writableByteChannel,
        final int bufferSize, final String objName, final boolean isPutCommand) throws IOException {
    final byte[] buffer = new byte[bufferSize];
    final ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
    int len;//from  w w w .j  av a  2s. c  om
    long totalBytes = 0;

    final long startTime = PerformanceUtils.getCurrentTime();
    long statusUpdateTime = startTime;

    try {
        while ((len = inputStream.read(buffer)) != -1) {
            totalBytes += len;

            try {
                byteBuffer.position(0);
                byteBuffer.limit(len);
                writableByteChannel.write(byteBuffer);
            } catch (final Throwable t) {
                throw new UnrecoverableIOException(t);
            }

            final long curTime = PerformanceUtils.getCurrentTime();
            if (statusUpdateTime <= curTime) {
                PerformanceUtils.logMbpsStatus(startTime, curTime, totalBytes, objName, isPutCommand);
                statusUpdateTime += 60000D; //Only logs status once a minute
            }
        }
    } catch (final ConnectionClosedException e) {
        LOG.error("Connection closed trying to copy from stream to channel.", e);
    }

    return totalBytes;
}

From source file:Main.java

public static ByteBuffer cloneByteBuffer(final ByteBuffer original) {
    // Create clone with same capacity as original.
    final ByteBuffer clone = (original.isDirect()) ? ByteBuffer.allocateDirect(original.capacity())
            : ByteBuffer.allocate(original.capacity());

    // Create a read-only copy of the original.
    // This allows reading from the original without modifying it.
    final ByteBuffer readOnlyCopy = original.asReadOnlyBuffer();

    // Flip and read from the original.
    readOnlyCopy.flip();/*from   w  w w .  j  a  va2 s .com*/
    clone.put(readOnlyCopy);
    clone.position(original.position());
    clone.limit(original.limit());
    clone.order(original.order());
    return clone;
}

From source file:org.apache.tajo.storage.orc.OrcScanner.java

private static OrcProto.Footer extractFooter(ByteBuffer bb, int footerAbsPos, int footerSize,
        CompressionCodec codec, int bufferSize) throws IOException {
    bb.position(footerAbsPos);/* w w w. j  av  a 2s.  c o  m*/
    bb.limit(footerAbsPos + footerSize);
    return OrcProto.Footer.parseFrom(InStream.createCodedInputStream("footer",
            Lists.newArrayList(new BufferChunk(bb, 0)), footerSize, codec, bufferSize));
}

From source file:org.apache.tajo.storage.orc.OrcScanner.java

private static OrcProto.Metadata extractMetadata(ByteBuffer bb, int metadataAbsPos, int metadataSize,
        CompressionCodec codec, int bufferSize) throws IOException {
    bb.position(metadataAbsPos);/*from  w  w  w .ja v a 2 s  .  c o  m*/
    bb.limit(metadataAbsPos + metadataSize);
    return OrcProto.Metadata.parseFrom(InStream.createCodedInputStream("metadata",
            Lists.newArrayList(new BufferChunk(bb, 0)), metadataSize, codec, bufferSize));
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * flush?????limit,??position, position0 ,
 * ByteBuffer.flip()//w w  w. j  a v  a  2s.  c  om
 * 
 * @param buffer
 * @param position
 */
public static void flipToFlush(ByteBuffer buffer, int position) {
    buffer.limit(buffer.position());
    buffer.position(position);
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * ?flush?ByteBufferlimitposition0,?fill?
 * /*from  ww w .  j  a  v a 2 s  . co m*/
 * @param capacity
 * @return
 */
public static ByteBuffer allocate(int capacity) {
    ByteBuffer buff = ByteBuffer.allocate(capacity);
    buff.limit(0);
    return buff;
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static ByteBuffer allocateDirect(int capacity) {
    ByteBuffer buff = ByteBuffer.allocateDirect(capacity);
    buff.limit(0);
    return buff;// w  w  w  . j a  v a  2s .c o  m
}

From source file:net.darkmist.alib.io.BufferUtil.java

/**
 * Sane ByteBuffer slice//from w  w w .  jav a2  s.co m
 * @param buf the buffer to slice something out of
 * @param off The offset into the buffer
 * @param len the length of the part to slice out
 */
public static ByteBuffer slice(ByteBuffer buf, int off, int len) {
    ByteBuffer localBuf = buf.duplicate(); // so we don't mess up the position,etc
    logger.debug("off={} len={}", off, len);
    localBuf.position(off);
    localBuf.limit(off + len);
    logger.debug("pre-slice: localBuf.position()={} localBuf.limit()={}", localBuf.position(),
            localBuf.limit());
    localBuf = localBuf.slice();
    logger.debug("post-slice: localBuf.position()={} localBuf.limit()={}", localBuf.position(),
            localBuf.limit());
    return localBuf;
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * Decode a String representation./*from w w w . j ava 2  s.co m*/
 * 
 * @param buffer
 *            a byte buffer holding the string representation
 * @param position
 *            the starting position in {@code buffer} to start decoding from
 * @param length
 *            the number of bytes from {@code buffer} to use
 * @param charset
 *            the String encoding charset
 * @return the decoded string
 */
public static String string(ByteBuffer buffer, int position, int length, Charset charset)
        throws CharacterCodingException {
    ByteBuffer copy = buffer.duplicate();
    copy.position(position);
    copy.limit(copy.position() + length);
    return string(copy, charset);
}