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.BinaryIndexTable.java

/**
 * Create a binary index table.//from  ww  w.  j  a v  a2  s .c o m
 * @param nentries The number of entries in the table.
 * @param buffer The table buffer.  Its position will be advanced to the end of the table.
 * @return The index table.
 */
public static BinaryIndexTable fromBuffer(int nentries, ByteBuffer buffer) {
    logger.debug("reading table of {} entries", nentries);
    long[] keys = new long[nentries];
    int[] offsets = new int[nentries];
    int[] sizes = new int[nentries];
    int nextExpectedOffset = 0;
    for (int i = 0; i < nentries; i++) {
        keys[i] = buffer.getLong();
        if (i > 0 && keys[i - 1] >= keys[i]) {
            logger.error("key {} is not greater than previous key {}", keys[i], keys[i - 1]);
            throw new IllegalArgumentException("corrupted index table");
        }
        offsets[i] = buffer.getInt();
        sizes[i] = buffer.getInt();
        if (offsets[i] != nextExpectedOffset) {
            logger.error("expected offset {}, got {}", nextExpectedOffset, offsets[i]);
            throw new IllegalArgumentException("corrupted index table");
        }
        nextExpectedOffset += sizes[i];
    }
    if (buffer.remaining() < nextExpectedOffset) {
        throw new IllegalArgumentException("buffer not large enough");
    }
    int end = buffer.position() + nextExpectedOffset * 4;
    ByteBuffer dup = buffer.duplicate();
    dup.limit(end);
    buffer.position(end);
    LongKeyDomain dom = LongKeyDomain.wrap(keys, keys.length, true);
    return new BinaryIndexTable(dom, offsets, sizes, dup.asIntBuffer());
}

From source file:org.apache.hadoop.util.NativeJerasure.java

private static ByteBuffer directify(byte[] readBufs, int dataStart, int dataLen) {
    ByteBuffer newBuf = null;
    newBuf = ByteBuffer.allocateDirect(dataLen);
    newBuf.position(0);/*from  w  ww .ja v  a  2 s  .  co  m*/
    newBuf.mark();
    newBuf.put(readBufs, dataStart, dataLen);
    newBuf.reset();
    newBuf.limit(dataLen);
    return newBuf;
}

From source file:MainClass.java

private static void createFile() throws Exception {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/primes.bin");
    FileOutputStream outputFile = new FileOutputStream(aFile);
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    LongBuffer longBuf = buf.asLongBuffer();
    int primesWritten = 0;
    while (primesWritten < primes.length) {
        longBuf.put(primes, primesWritten, min(longBuf.capacity(), primes.length - primesWritten));
        buf.limit(8 * longBuf.position());

        file.write(buf);/*from  w ww . java  2  s .com*/
        primesWritten += longBuf.position();
        longBuf.clear();
        buf.clear();
    }
    System.out.println("File written is " + file.size() + "bytes.");
    outputFile.close();
}

From source file:org.lenskit.data.packed.BinaryIndexTable.java

/**
 * Create a binary index table./*www  . j a  v  a 2s . co  m*/
 * @param nentries The number of entries in the table.
 * @param buffer The table buffer.  Its position will be advanced to the end of the table.
 * @return The index table.
 */
public static BinaryIndexTable fromBuffer(int nentries, ByteBuffer buffer) {
    logger.debug("reading table of {} entries", nentries);
    long[] keys = new long[nentries];
    int[] offsets = new int[nentries];
    int[] sizes = new int[nentries];
    // Read the index table's header (IDs, offsets, and counts/sizes).
    int nextExpectedOffset = 0;
    for (int i = 0; i < nentries; i++) {
        keys[i] = buffer.getLong();
        if (i > 0 && keys[i - 1] >= keys[i]) {
            logger.error("key {} is not greater than previous key {}", keys[i], keys[i - 1]);
            throw new IllegalArgumentException("corrupted index table");
        }
        offsets[i] = buffer.getInt();
        sizes[i] = buffer.getInt();
        if (offsets[i] != nextExpectedOffset) {
            logger.error("expected offset {}, got {}", nextExpectedOffset, offsets[i]);
            throw new IllegalArgumentException("corrupted index table");
        }
        nextExpectedOffset += sizes[i];
    }

    // Set up the integer store
    if (buffer.remaining() < nextExpectedOffset * 4) {
        throw new IllegalArgumentException("buffer not large enough");
    }
    int end = buffer.position() + nextExpectedOffset * 4;
    ByteBuffer dup = buffer.duplicate();
    dup.limit(end);
    // update input indexStore's position
    buffer.position(end);
    // create index table object
    LongKeyDomain dom = LongKeyDomain.wrap(keys, keys.length, true);
    return new BinaryIndexTable(dom, offsets, sizes, dup.asIntBuffer());
}

From source file:com.liveramp.commons.util.BytesUtils.java

public static ByteBuffer byteBufferDeepCopy(ByteBuffer src, ByteBuffer dst) {
    if (dst == null || dst.capacity() < src.remaining()) {
        dst = byteBufferDeepCopy(src);// ww  w.  j a  v  a  2s .  c om
    } else {
        dst.rewind();
        dst.limit(src.remaining());
        dst.put(src.slice());
        dst.flip();
    }
    return dst;
}

From source file:com.smartitengineering.cms.api.impl.Utils.java

public static String readStringInUTF8(DataInput in) throws IOException, UnsupportedEncodingException {
    int allocationBlockSize = 2000;
    int capacity = allocationBlockSize;
    int length = 0;
    ByteBuffer buffer = ByteBuffer.allocate(allocationBlockSize);
    boolean notEof = true;
    while (notEof) {
        try {/*w  w  w  .  ja v  a 2 s . c om*/
            buffer.put(in.readByte());
            if (++length >= capacity) {
                capacity += allocationBlockSize;
                buffer.limit(capacity);
            }
        } catch (EOFException ex) {
            notEof = false;
        }
    }
    String string = StringUtils.newStringUtf8(Arrays.copyOf(buffer.array(), length));
    return string;
}

From source file:Main.java

/**
 * Finds next Nth MPEG bitstream marker 0x000001xx and returns the data that
 * preceeds it as a ByteBuffer slice//from www  .  j a v  a  2 s.  c  o m
 * 
 * Segment byte order is always little endian
 * 
 * @param buf
 * @return
 */
public static final ByteBuffer gotoMarker(ByteBuffer buf, int n, int mmin, int mmax) {
    if (!buf.hasRemaining())
        return null;

    int from = buf.position();
    ByteBuffer result = buf.slice();
    result.order(ByteOrder.BIG_ENDIAN);

    int val = 0xffffffff;
    while (buf.hasRemaining()) {
        val = (val << 8) | (buf.get() & 0xff);
        if (val >= mmin && val <= mmax) {
            if (n == 0) {
                buf.position(buf.position() - 4);
                result.limit(buf.position() - from);
                break;
            }
            --n;
        }
    }
    return result;
}

From source file:org.apache.hadoop.util.NativeStair.java

private static ByteBuffer directify(byte[] readBufs, int dataStart, int dataLen) {
    //LOG.info("directify starts: " + System.nanoTime());
    ByteBuffer newBuf = null;
    newBuf = ByteBuffer.allocateDirect(dataLen);
    newBuf.position(0);//from w  w w.  j  a va2  s  .  co m
    newBuf.mark();
    newBuf.put(readBufs, dataStart, dataLen);
    newBuf.reset();
    newBuf.limit(dataLen);
    //LOG.info("directify ends: " + System.nanoTime());
    return newBuf;
}

From source file:com.linkedin.databus.core.DbusEventPart.java

/**
 * Decodes a bytebuffer and returns DbusEventPart. Preserves the ByteBuffer position.
 * @param buf// w w  w.  j a  v  a2 s.  c om
 * @return
 */
public static DbusEventPart decode(ByteBuffer buf) {
    int pos = buf.position();
    int dataLen = buf.getInt(pos);
    if (dataLen < 0) {
        throw new UnsupportedOperationException("Data length " + dataLen + " not supported");
    }
    short attributes = buf.getShort(pos + AttributesOffset);
    short schemaVersion = (short) (attributes >> VERSION_SHIFT);
    SchemaDigestType schemaDigestType = digestType(attributes);
    int digestLen = digestLen(schemaDigestType);
    byte[] digest = new byte[digestLen];
    for (int i = 0; i < digestLen; i++) {
        digest[i] = buf.get(pos + AttributesOffset + AttributesLen + i);
    }

    // NOTE - this will create a new ByteBuffer object pointing to the
    // same memory. So the position of this new BB is beginning of the data
    // and limit is set to the end of the data.
    ByteBuffer dataBuf = buf.asReadOnlyBuffer();
    dataBuf.position(pos + AttributesOffset + AttributesLen + digestLen);
    dataBuf.limit(dataBuf.position() + dataLen);

    return new DbusEventPart(schemaDigestType, digest, schemaVersion, dataBuf);
}

From source file:io.mycat.util.ByteBufferUtil.java

public static ByteBuffer readBytes(ByteBuffer bb, int length) {
    ByteBuffer copy = bb.duplicate();
    copy.limit(copy.position() + length);
    bb.position(bb.position() + length);
    return copy;//  w  w  w.  ja  va  2 s.co m
}