Buffer capacity - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Buffer capacity method.

This code example shows how to use the following methods:capacity, limit, position
        if (len == 0) {
            return -1; 
        } 
 
        ByteBuffer bb = ((this.bs == bs) ? this.bb : ByteBuffer.wrap(bs));
        bb.limit(Math.min(off + len, bb.capacity()));
        bb.position(off);
 
        this.bb = bb;
        this.bs = bs;
        int read = channel.read(bb, position);
        if (read > 0) {
            position += read;
        } 
        return read;
    } 
 
    @Override 
    public boolean markSupported() { 
        return true; 
9
CodeRank
This code example shows how to use the following methods:capacity, limit, position, remaining, isDirect
9
CodeRank
This code example shows how to use the following methods:capacity, flip, isDirect
   
  private void reallocPacketBuf(int atLeastCapacity) {
    // Realloc the buffer if this packet is longer than the previous 
    // one. 
    if (curPacketBuf == null ||
        curPacketBuf.capacity() < atLeastCapacity) {
      ByteBuffer newBuf;
      if (useDirectBuffers) {
        newBuf = bufferPool.getBuffer(atLeastCapacity);
      } else { 
        newBuf = ByteBuffer.allocate(atLeastCapacity);
      } 
      // If reallocing an existing buffer, copy the old packet length 
      // prefixes over 
      if (curPacketBuf != null) {
        curPacketBuf.flip();
        newBuf.put(curPacketBuf);
      } 
       
      returnPacketBufToPool(); 

8
CodeRank
This code example shows how to use the following methods:capacity
        @Override 
        public String toString()
        { 
            return "Region@" + System.identityHashCode(this) +
                   " allocs=" + allocCount.get() + "waste=" +
                   (data.capacity() - nextFreeOffset.get());
        } 
    } 
} 
8
CodeRank
This code example shows how to use the following methods:capacity, remaining
  DirectBufferPool pool = new DirectBufferPool();
   
  @Test 
  public void testBasics() { 
    ByteBuffer a = pool.getBuffer(100);
    assertEquals(100, a.capacity());
    assertEquals(100, a.remaining());
    pool.returnBuffer(a);
     
    // Getting a new buffer should return the same one 
    ByteBuffer b = pool.getBuffer(100);
    assertSame(a, b);
     
    // Getting a new buffer before returning "B" should 
    // not return the same one 
    ByteBuffer c = pool.getBuffer(100);
    assertNotSame(b, c);
    pool.returnBuffer(b);
    pool.returnBuffer(c);
  } 
8
CodeRank
This code example shows how to use the following methods:capacity, flip, limit, position, compact
		return length;
	} 
 
	@Override 
	public int put(byte[] chunk) {
		int startPosition = buf.position();
		int endPosition = buf.limit();
		//compact the buffer before every put operation... 
		int roomLeft = buf.capacity()-endPosition;
		if(chunk.length > roomLeft) {
			buf.compact();
			buf.flip();
			startPosition = buf.position();
			endPosition = buf.limit();
		} 
		 
		//prepare for more writing 
		buf.position(buf.limit());
		buf.limit(buf.capacity());
		buf.put(chunk);

7
CodeRank
This code example shows how to use the following methods:capacity, clear
   * the buffer may be recycled, so the user must not 
   * continue to use it in any way. 
   * @param buf the buffer to return 
   */ 
  public void returnBuffer(ByteBuffer buf) {
    buf.clear(); // reset mark, limit, etc
    int size = buf.capacity();
    Queue<WeakReference<ByteBuffer>> list = buffersBySize.get(size);
    if (list == null) {
      list = new ConcurrentLinkedQueue<WeakReference<ByteBuffer>>();
      Queue<WeakReference<ByteBuffer>> prev = buffersBySize.putIfAbsent(size, list);
      // someone else put a queue in the map before we did 
      if (prev != null) {
        list = prev;
      } 
    } 
    list.add(new WeakReference<ByteBuffer>(buf));
  } 
   
  /** 
7
CodeRank
This code example shows how to use the following methods:capacity, limit, position, hasArray, toString
	public void readChars(char[] buf, int off, int n) throws IOException {
		fBuffer.get(buf, off, n);
	} 
 
	public void readChars(StringBuffer buf, int n) throws IOException {
		int pos = fBuffer.position();
		if (fBuffer.hasArray()) {
			buf.append(fBuffer.array(), fBuffer.arrayOffset() + pos, n);
		} else { 
			fBuffer.limit(pos+n);
			String str = fBuffer.toString();
			assert str.length() == n;
			buf.append(str);
			fBuffer.limit(fBuffer.capacity());
		} 
		fBuffer.position(pos + n);
	} 
 
	public int length() { 
		return fBuffer.length();
	} 
7
CodeRank
This code example shows how to use the following methods:capacity, order
 		return channelBuffer;
	} 
 
	@Override 
	public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
		int size = nioBuffer.capacity();
		ChannelBuffer channelBuffer = ChannelBuffers.directBuffer(nioBuffer.order(), size);
		channelBuffer.writeBytes(nioBuffer);
		return channelBuffer;
	} 
 
	@Override 
	public ByteOrder getDefaultOrder() {
 		return ByteOrder.BIG_ENDIAN;
	} 
 
} 
7
CodeRank
This code example shows how to use the following methods:capacity, limit, position
 * Tests for some buffers from the java.nio package. 
 */ 
public class OldAndroidNIOTest extends TestCase {
 
    void checkBuffer(Buffer b) {
        assertTrue(0 <= b.position());
        assertTrue(b.position() <= b.limit());
        assertTrue(b.limit() <= b.capacity());
    } 
 
    public void testNIO_byte_array() throws Exception { 
        // Test byte array-based buffer 
        byteBufferTest(ByteBuffer.allocate(12));
    } 
 
    public void testNIO_direct() throws Exception { 
        // Test native heap-allocated buffer 
        byteBufferTest(ByteBuffer.allocateDirect(12));
    } 
 
7
CodeRank

Related Buffer capacity Questions & Answers:

capacity Method Overview
Returns this buffer's capacity.
See Code Examples for other Java 8 Buffer Methods: