Search in sources :

Example 1 with InvalidMarkException

use of java.nio.InvalidMarkException in project hbase by apache.

the class NioByteString method newInput.

@Override
public InputStream newInput() {
    return new InputStream() {

        private final ByteBuffer buf = buffer.slice();

        @Override
        public void mark(int readlimit) {
            buf.mark();
        }

        @Override
        public boolean markSupported() {
            return true;
        }

        @Override
        public void reset() throws IOException {
            try {
                buf.reset();
            } catch (InvalidMarkException e) {
                throw new IOException(e);
            }
        }

        @Override
        public int available() throws IOException {
            return buf.remaining();
        }

        @Override
        public int read() throws IOException {
            if (!buf.hasRemaining()) {
                return -1;
            }
            return buf.get() & 0xFF;
        }

        @Override
        public int read(byte[] bytes, int off, int len) throws IOException {
            if (!buf.hasRemaining()) {
                return -1;
            }
            len = Math.min(len, buf.remaining());
            buf.get(bytes, off, len);
            return len;
        }
    };
}
Also used : ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Example 2 with InvalidMarkException

use of java.nio.InvalidMarkException in project robovm by robovm.

the class AbstractBufferTest method testClear.

public void testClear() {
    // save state
    int oldPosition = baseBuf.position();
    int oldLimit = baseBuf.limit();
    Buffer ret = baseBuf.clear();
    assertSame(ret, baseBuf);
    assertEquals(baseBuf.position(), 0);
    assertEquals(baseBuf.limit(), baseBuf.capacity());
    try {
        baseBuf.reset();
        //$NON-NLS-1$S
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // restore state
    baseBuf.limit(oldLimit);
    baseBuf.position(oldPosition);
}
Also used : Buffer(java.nio.Buffer) ByteBuffer(java.nio.ByteBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Example 3 with InvalidMarkException

use of java.nio.InvalidMarkException in project robovm by robovm.

the class LongBufferTest method testCompact.

public void testCompact() {
    // case: buffer is full
    buf.clear();
    buf.mark();
    loadTestData1(buf);
    LongBuffer ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), buf.capacity());
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0, buf.capacity());
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // case: buffer is empty
    buf.position(0);
    buf.limit(0);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 0);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0, buf.capacity());
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // case: normal
    assertTrue(buf.capacity() > 5);
    buf.position(1);
    buf.limit(5);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 4);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 1, 4);
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
}
Also used : LongBuffer(java.nio.LongBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Example 4 with InvalidMarkException

use of java.nio.InvalidMarkException in project robovm by robovm.

the class IntBufferTest method testSlice.

public void testSlice() {
    assertTrue(buf.capacity() > 5);
    buf.position(1);
    buf.limit(buf.capacity() - 1);
    IntBuffer slice = buf.slice();
    assertEquals(buf.isReadOnly(), slice.isReadOnly());
    assertEquals(buf.isDirect(), slice.isDirect());
    assertEquals(buf.order(), slice.order());
    assertEquals(slice.position(), 0);
    assertEquals(slice.limit(), buf.remaining());
    assertEquals(slice.capacity(), buf.remaining());
    try {
        slice.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // slice share the same content with buf
    if (!slice.isReadOnly()) {
        loadTestData1(slice);
        assertContentLikeTestData1(buf, 1, 0, slice.capacity());
        buf.put(2, 500);
        assertEquals(slice.get(1), 500);
    }
}
Also used : IntBuffer(java.nio.IntBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Example 5 with InvalidMarkException

use of java.nio.InvalidMarkException in project robovm by robovm.

the class InvalidMarkExceptionTest method test_Constructor.

/**
     *@tests {@link java.nio.InvalidMarkException#InvalidMarkException()}
     */
public void test_Constructor() {
    InvalidMarkException exception = new InvalidMarkException();
    assertNull(exception.getMessage());
    assertNull(exception.getLocalizedMessage());
    assertNull(exception.getCause());
}
Also used : InvalidMarkException(java.nio.InvalidMarkException)

Aggregations

InvalidMarkException (java.nio.InvalidMarkException)46 ByteBuffer (java.nio.ByteBuffer)19 Buffer (java.nio.Buffer)12 CharBuffer (java.nio.CharBuffer)4 DoubleBuffer (java.nio.DoubleBuffer)4 FloatBuffer (java.nio.FloatBuffer)4 IntBuffer (java.nio.IntBuffer)4 LongBuffer (java.nio.LongBuffer)4 ShortBuffer (java.nio.ShortBuffer)4 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ImageOutputStream (javax.imageio.stream.ImageOutputStream)1