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:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer buf = ByteBuffer.allocateDirect(10);
    buf.limit(7); // remaining=1
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.put((byte) 0xFF);
    bbuf.position(5);//  w  ww .j  a v a 2  s .  c o  m
    bbuf.put((byte) 0xFF);
    int pos = bbuf.position();
    int rem = bbuf.remaining();
    bbuf.limit(7);
    bbuf.rewind();
}

From source file:MainClass.java

public static void main(String[] args) {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/test/primes.bin");
    FileOutputStream outputFile = null;
    try {/*from  w  w w .  j  a  v  a 2s .co m*/
        outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    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());
        try {
            file.write(buf);
            primesWritten += longBuf.position();
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        longBuf.clear();
        buf.clear();
    }
    try {
        System.out.println("File written is " + file.size() + "bytes.");
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    int count = 100;

    long[] numbers = new long[count];

    for (int i = 0; i < numbers.length; i++) {
        numbers[i] = i;//from w w w  .  ja va 2  s.c  o  m

    }
    File aFile = new File("data.bin");
    FileOutputStream outputFile = null;
    try {
        outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    LongBuffer longBuf = buf.asLongBuffer();

    int numberWritten = 0;

    while (numberWritten < numbers.length) {
        longBuf.put(numbers, numberWritten, min(longBuf.capacity(), numbers.length - numberWritten));
        buf.limit(8 * longBuf.position());
        try {
            file.write(buf);
            numberWritten += longBuf.position();
        } catch (IOException e) {
            e.printStackTrace(System.err);
            System.exit(1);
        }
        longBuf.clear();
        buf.clear();
    }

    try {
        System.out.println("File written is " + file.size() + " bytes.");
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:org.apache.bookkeeper.bookie.Bookie.java

/**
 * @param args//  w w w .  j  a v a 2  s. c  o  m
 * @throws IOException
 * @throws InterruptedException
 */
public static void main(String[] args)
        throws IOException, InterruptedException, BookieException, KeeperException {
    Bookie b = new Bookie(new ServerConfiguration());
    b.start();
    CounterCallback cb = new CounterCallback();
    long start = MathUtils.now();
    for (int i = 0; i < 100000; i++) {
        ByteBuffer buff = ByteBuffer.allocate(1024);
        buff.putLong(1);
        buff.putLong(i);
        buff.limit(1024);
        buff.position(0);
        cb.incCount();
        b.addEntry(buff, cb, null, new byte[0]);
    }
    cb.waitZero();
    long end = MathUtils.now();
    System.out.println("Took " + (end - start) + "ms");
}

From source file:Main.java

protected static ByteBuffer renewBuffer(ByteBuffer buf) {
    buf.position(0);
    buf.limit(buf.capacity());

    return buf;
}

From source file:Main.java

static void memcpy(ByteBuffer dstBuffer, int dstByteOffset, ByteBuffer srcBuffer, int srcByteOffset,
        int length) {
    ByteBuffer dstDup = dstBuffer.duplicate();
    dstDup.position(dstByteOffset);/*from  w  w  w  .  j a  v a2s . co  m*/
    dstDup.limit(dstByteOffset + length);
    ByteBuffer srcDup = srcBuffer.duplicate();
    srcDup.position(srcByteOffset);
    srcDup.limit(srcByteOffset + length);
    dstDup.put(srcDup);
}

From source file:Main.java

public final static int copy(final ByteBuffer from, final int offset1, final ByteBuffer to, final int offset2,
        final int len) {
    System.arraycopy(from.array(), offset1, to.array(), offset2, len);
    to.limit(offset2 + len);
    return len;//ww w . ja va2s  .co  m
}

From source file:Main.java

/**
 * Discards data from the buffer up to the first SPS, where {@code data.position()} is interpreted
 * as the length of the buffer./*from   w  w w .  j a v a  2s  . co m*/
 * <p>
 * When the method returns, {@code data.position()} will contain the new length of the buffer. If
 * the buffer is not empty it is guaranteed to start with an SPS.
 *
 * @param data Buffer containing start code delimited NAL units.
 */
public static void discardToSps(ByteBuffer data) {
    int length = data.position();
    int consecutiveZeros = 0;
    int offset = 0;
    while (offset + 1 < length) {
        int value = data.get(offset) & 0xFF;
        if (consecutiveZeros == 3) {
            if (value == 1 && (data.get(offset + 1) & 0x1F) == H264_NAL_UNIT_TYPE_SPS) {
                // Copy from this NAL unit onwards to the start of the buffer.
                ByteBuffer offsetData = data.duplicate();
                offsetData.position(offset - 3);
                offsetData.limit(length);
                data.position(0);
                data.put(offsetData);
                return;
            }
        } else if (value == 0) {
            consecutiveZeros++;
        }
        if (value != 0) {
            consecutiveZeros = 0;
        }
        offset++;
    }
    // Empty the buffer if the SPS NAL unit was not found.
    data.clear();
}

From source file:Main.java

/**
 * Discards data from the buffer up to the first SPS, where {@code data.position()} is interpreted
 * as the length of the buffer./*from   w w  w  .  jav a  2 s .  co  m*/
 * <p>
 * When the method returns, {@code data.position()} will contain the new length of the buffer. If
 * the buffer is not empty it is guaranteed to start with an SPS.
 *
 * @param data Buffer containing start code delimited NAL units.
 */
public static void discardToSps(ByteBuffer data) {
    int length = data.position();
    int consecutiveZeros = 0;
    int offset = 0;
    while (offset + 1 < length) {
        int value = data.get(offset) & 0xFF;
        if (consecutiveZeros == 3) {
            if (value == 1 && (data.get(offset + 1) & 0x1F) == NAL_UNIT_TYPE_SPS) {
                // Copy from this NAL unit onwards to the start of the buffer.
                ByteBuffer offsetData = data.duplicate();
                offsetData.position(offset - 3);
                offsetData.limit(length);
                data.position(0);
                data.put(offsetData);
                return;
            }
        } else if (value == 0) {
            consecutiveZeros++;
        }
        if (value != 0) {
            consecutiveZeros = 0;
        }
        offset++;
    }
    // Empty the buffer if the SPS NAL unit was not found.
    data.clear();
}