Example usage for java.nio Buffer hasRemaining

List of usage examples for java.nio Buffer hasRemaining

Introduction

In this page you can find the example usage for java.nio Buffer hasRemaining.

Prototype

public final boolean hasRemaining() 

Source Link

Document

Indicates if there are elements remaining in this buffer, that is if position < limit .

Usage

From source file:Main.java

/**
 * Creates an int array from the provided {@link IntBuffer} or {@link ShortBuffer}.
 * //  w  w w . j  a va  2  s . c o  m
 * @param buffer {@link Buffer} the data source. Should be either a {@link IntBuffer} 
 * or {@link ShortBuffer}.
 * @return int array containing the data of the buffer.
 */
public static int[] getIntArrayFromBuffer(Buffer buffer) {
    int[] array = null;
    if (buffer.hasArray()) {
        array = (int[]) buffer.array();
    } else {
        buffer.rewind();
        array = new int[buffer.capacity()];
        if (buffer instanceof IntBuffer) {
            ((IntBuffer) buffer).get(array);
        } else if (buffer instanceof ShortBuffer) {
            int count = 0;
            while (buffer.hasRemaining()) {
                array[count] = (int) (((ShortBuffer) buffer).get());
                ++count;
            }
        }
    }
    return array;
}

From source file:com.navercorp.pinpoint.common.buffer.FixedBufferTest.java

@Test
public void test_remaining() throws Exception {
    final byte[] bytes = new byte[BytesUtils.INT_BYTE_LENGTH];
    Buffer buffer = new FixedBuffer(bytes);
    Assert.assertEquals(buffer.remaining(), 4);
    Assert.assertTrue(buffer.hasRemaining());

    buffer.putInt(1234);//from  w  ww . ja va 2s .  c  o  m
    Assert.assertEquals(buffer.remaining(), 0);
    Assert.assertFalse(buffer.hasRemaining());

    buffer.setOffset(0);
    buffer.putShort((short) 12);
    Assert.assertEquals(buffer.remaining(), 2);
    Assert.assertTrue(buffer.hasRemaining());

    buffer.putByte((byte) 1);
    Assert.assertEquals(buffer.remaining(), 1);
    Assert.assertTrue(buffer.hasRemaining());
}