Package net.sf.cindy

Examples of net.sf.cindy.Buffer


    public void testGetAndPut() {
        for (int i = 0; i < 10; i++) {
            byte[] b = new byte[random.nextInt(1000) + 100];
            random.nextBytes(b);

            Buffer buffer = newBuffer(b.length);
            buffer.put(b);
            assertEquals(b.length, buffer.position());
            buffer.flip();
            assertEquals(0, buffer.position());
            assertEquals(b.length, buffer.limit());
            assertEquals(b.length, buffer.capacity());
            assertEquals(b.length, buffer.remaining());
            assertTrue(buffer.hasRemaining());
            assertEquals(buffer.asByteBuffer(), ByteBuffer.wrap(b));

            random.nextBytes(b);
            buffer.put(ByteBuffer.wrap(b));
            assertEquals(b.length, buffer.position());
            assertEquals(b.length, buffer.limit());
            assertEquals(b.length, buffer.capacity());
            assertEquals(0, buffer.remaining());
            assertFalse(buffer.hasRemaining());
            buffer.flip();
            assertEquals(buffer.asByteBuffer(), ByteBuffer.wrap(b));
            assertEquals(0, buffer.position());
            assertEquals(b.length, buffer.limit());
            assertEquals(b.length, buffer.remaining());
            assertEquals(b.length, buffer.capacity());
            assertTrue(buffer.hasRemaining());

            Buffer b1 = newBuffer(b.length);
            random.nextBytes(b);
            b1.put(0, b);
            buffer.put(b1);
            assertEquals(b.length, buffer.position());
            assertEquals(b.length, buffer.limit());
            assertEquals(b.length, buffer.capacity());
            assertEquals(0, buffer.remaining());
            assertFalse(buffer.hasRemaining());
            buffer.flip();
            assertEquals(buffer.asByteBuffer(), ByteBuffer.wrap(b));
            assertEquals(0, buffer.position());
            assertEquals(b.length, buffer.limit());
            assertEquals(b.length, buffer.remaining());
            assertEquals(b.length, buffer.capacity());
            assertTrue(buffer.hasRemaining());

            ByteBuffer b2 = ByteBuffer.wrap(b);
            for (int j = 0; j < 100; j++) {
                int index = Math.max(0, random.nextInt(b.length) - 8);
                assertEquals(buffer.get(index), b2.get(index));
                assertEquals(buffer.getShort(index), b2.getShort(index));
                assertEquals(buffer.getChar(index), b2.getChar(index));
                assertEquals(buffer.getInt(index), b2.getInt(index));
                assertEquals(buffer.getLong(index), b2.getLong(index));
                assertEquals(new Float(buffer.getFloat(index)), new Float(b2
                        .getFloat(index)));
                assertEquals(new Double(buffer.getDouble(index)), new Double(b2
                        .getDouble(index)));
            }

            while (buffer.hasRemaining()) {
                assertEquals(b2.get(), buffer.get());
            }
            assertFalse(buffer.hasRemaining());
            assertEquals(b.length, buffer.position());
            assertEquals(b.length, buffer.limit());
            assertEquals(0, buffer.remaining());

            buffer.clear();
            assertEquals(0, buffer.position());
            assertEquals(b.length, buffer.limit());
            assertEquals(b.length, buffer.remaining());

            for (int j = 0; j < b.length; j++) {
                buffer.put(b[j]);
                assertEquals(j + 1, buffer.position());
                assertEquals(b.length, buffer.limit());
                assertEquals(b.length - j - 1, buffer.remaining());
            }

            for (int j = 0; j < 100; j++) {
                int index = Math.max(0, random.nextInt(b.length) - 8);
                short s = (short) random.nextInt();
                buffer.putShort(index, s);
                assertEquals(buffer.getShort(index), s);
                int in = random.nextInt();
                buffer.putInt(index, in);
                assertEquals(buffer.getInt(index), in);
                long l = random.nextLong();
                buffer.putLong(index, l);
                assertEquals(buffer.getLong(index), l);
                float f = random.nextFloat();
                buffer.putFloat(index, f);
                assertEquals(new Float(buffer.getFloat(index)), new Float(f));
                double d = random.nextDouble();
                buffer.putDouble(index, d);
                assertEquals(new Double(buffer.getDouble(index)), new Double(d));
            }
        }

        Buffer buffer = newBuffer(256);
        for (int i = 0; i < 256; i++) {
            buffer.put((byte) i);
        }
        buffer.flip();
        for (int i = 0; i < 5; i++) {
            byte[] b = new byte[random.nextInt(buffer.capacity())];
            int start = random.nextInt(buffer.capacity() - b.length);
            buffer.get(start, b);
            for (int j = 0; j < b.length; j++) {
                assertEquals(b[j], (byte) (start + j));
            }
        }
        for (int i = 0; i < 5; i++) {
            buffer.clear();
            byte[] b = new byte[random.nextInt(buffer.capacity())];
            int start = random.nextInt(buffer.capacity() - b.length);
            buffer.position(start);
            buffer.get(b);
            assertEquals(buffer.position(), start + b.length);
            for (int j = 0; j < b.length; j++) {
                assertEquals(b[j], (byte) (start + j));
            }
        }
    }
View Full Code Here


    public void testAppendByte() {
        BufferBuilder builder = new BufferBuilder(random.nextInt(256));
        for (int i = 0; i < 256; i++) {
            builder.append((byte) i);
        }
        Buffer buffer = builder.toBuffer();
        assertEquals(buffer.remaining(), 256);
        for (int i = 0; i < 256; i++) {
            assertEquals((byte) i, buffer.get());
        }
        builder.release();
    }
View Full Code Here

    public void testWrap() {
        byte[] b = new byte[random.nextInt(10000)];
        random.nextBytes(b);
        ByteBuffer b1 = ByteBuffer.wrap(b);

        Buffer buffer = ByteBufferBuffer.wrap(b1);
        assertEquals(0, buffer.position());
        assertEquals(b1.limit(), buffer.limit());
        assertEquals(b1.capacity(), buffer.capacity());
        assertEquals(b1.remaining(), buffer.remaining());
        for (int i = 0; i < b.length; i++) {
            assertEquals(b[i], buffer.get());
        }

        int offset = random.nextInt(b.length);
        int len = b.length - offset;
        b1 = ByteBuffer.wrap(b, offset, len);
        buffer = ByteBufferBuffer.wrap(b1);
        assertEquals(b1.position(), buffer.position());
        assertEquals(b1.limit(), buffer.limit());
        assertEquals(b1.capacity(), buffer.capacity());
        assertEquals(b1.remaining(), buffer.remaining());
        while (b1.hasRemaining()) {
            assertEquals(b1.get(), buffer.get());
        }
    }
View Full Code Here

    }

    public void testWrap() {
        byte[] b = new byte[random.nextInt(10000)];
        random.nextBytes(b);
        Buffer buffer = ByteArrayBuffer.wrap(b);
        assertEquals(0, buffer.position());
        assertEquals(b.length, buffer.limit());
        assertEquals(b.length, buffer.capacity());
        assertEquals(b.length, buffer.remaining());
        for (int i = 0; i < b.length; i++) {
            assertEquals(b[i], buffer.get());
        }

        int offset = random.nextInt(b.length);
        int len = b.length - offset;
        buffer = ByteArrayBuffer.wrap(b, offset, len);
        assertEquals(offset, buffer.position());
        assertEquals(b.length, buffer.limit());
        assertEquals(b.length, buffer.capacity());
        assertEquals(len, buffer.remaining());
        for (int i = offset; i < b.length; i++) {
            assertEquals(b[i], buffer.get());
        }
    }
View Full Code Here

    protected abstract Buffer newBuffer(int capacity);

    public void testCreate() {
        for (int i = 0; i < 5; i++) {
            int capacity = random.nextInt(1000) + 1;
            Buffer buffer = newBuffer(capacity);
            assertEquals(0, buffer.position());
            assertEquals(capacity, buffer.limit());
            assertEquals(capacity, buffer.capacity());
            assertEquals(capacity, buffer.remaining());
            assertTrue(buffer.hasRemaining());
        }
    }
View Full Code Here

            assertTrue(buffer.hasRemaining());
        }
    }

    public void testRelease() {
        Buffer buffer = newBuffer(10);
        assertFalse(buffer.isReleased());
        buffer.get();

        Buffer slice = buffer.slice();
        assertFalse(slice.isReleased());
        slice.get();

        Buffer duplicate = buffer.duplicate();
        assertFalse(duplicate.isReleased());
        duplicate.get();

        buffer.release();
        assertTrue(buffer.isReleased());
        assertTrue(slice.isReleased());
        assertTrue(duplicate.isReleased());
        try {
            buffer.get();
            fail();
        } catch (ReleasedBufferException e) {
        }
        try {
            slice.get();
            fail();
        } catch (ReleasedBufferException e) {
        }
        try {
            duplicate.get();
            fail();
        } catch (ReleasedBufferException e) {
        }

        buffer = newBuffer(10);
View Full Code Here

        assertFalse(buffer.isReleased());
        buffer.get();
    }

    public void testEquals() {
        Buffer buffer1 = newBuffer(256);
        Buffer buffer2 = newBuffer(256);
        for (int i = 0; i < 256; i++) {
            buffer1.put((byte) i);
            buffer2.put((byte) i);
        }
        assertEquals(buffer1, buffer2);
        buffer1.flip();
        assertFalse(buffer1.equals(buffer2));
        buffer2.flip();
        assertEquals(buffer1, buffer2);
    }
View Full Code Here

        buffer2.flip();
        assertEquals(buffer1, buffer2);
    }

    public void testAsByteBuffer() {
        Buffer buffer = newBuffer(256);
        for (int i = 0; i < 256; i++) {
            buffer.put((byte) i);
        }

        buffer.clear();
        ByteBuffer b1 = buffer.asByteBuffer();
        assertEquals(buffer.position(), b1.position());
        assertEquals(buffer.limit(), b1.limit());
        assertEquals(buffer.capacity(), b1.capacity());
        while (b1.hasRemaining())
            assertEquals(b1.get(), buffer.get());

        buffer.clear();
        buffer.limit(random.nextInt(buffer.capacity()));
        buffer.position(random.nextInt(buffer.limit()));
        ByteBuffer b2 = buffer.asByteBuffer();
        assertEquals(buffer.position(), b2.position());
        assertEquals(buffer.limit(), b2.limit());
        assertEquals(buffer.capacity(), b2.capacity());
        while (b2.hasRemaining())
            assertEquals(b2.get(), buffer.get());

        buffer.clear();
        buffer.limit(random.nextInt(buffer.capacity()));
        buffer.position(random.nextInt(buffer.limit()));
        buffer = buffer.slice();
        ByteBuffer b3 = buffer.asByteBuffer();
        assertEquals(buffer.position(), b3.position());
        assertEquals(buffer.limit(), b3.limit());
        assertEquals(buffer.capacity(), b3.capacity());
        while (b3.hasRemaining())
            assertEquals(b3.get(), buffer.get());
    }
View Full Code Here

    }

    public void testWrite() {
        byte[] b = new byte[random.nextInt(5000)];

        Buffer buffer = newBuffer(b.length);
        buffer.put(b);
        buffer.flip();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        WritableByteChannel channel = Channels.newChannel(baos);

        try {
            int writeCount = 0;
            while (true) {
                if (!buffer.hasRemaining())
                    break;
                int n = buffer.write(channel);
                if (n == -1)
                    break;
                writeCount += n;
            }
            assertEquals(b.length, writeCount);
View Full Code Here

        }
    }

    public void testRead() {
        byte[] b = new byte[random.nextInt(5000)];
        Buffer buffer = newBuffer(b.length);

        ByteArrayInputStream bais = new ByteArrayInputStream(b);
        ReadableByteChannel channel = Channels.newChannel(bais);

        try {
            int readCount = 0;
            while (true) {
                if (!buffer.hasRemaining())
                    break;
                int n = buffer.read(channel);
                if (n == -1)
                    break;
                readCount += n;
            }
            assertEquals(b.length, readCount);
            assertEquals(buffer.position(), b.length);
            buffer.flip();

            int i = 0;
            while (buffer.hasRemaining()) {
                assertEquals(buffer.get(), b[i++]);
            }
        } catch (IOException e) {
            fail(e.toString());
        }
    }
View Full Code Here

TOP

Related Classes of net.sf.cindy.Buffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.