Package net.sf.cindy

Examples of net.sf.cindy.Buffer


            fail(e.toString());
        }
    }

    public void testSkip() {
        Buffer buffer = newBuffer(random.nextInt(100) + 10);
        buffer.skip(1);
        assertEquals(buffer.position(), 1);
        buffer.skip(2);
        assertEquals(buffer.position(), 3);
        buffer.skip(buffer.remaining());
        assertEquals(buffer.position(), buffer.limit());
    }
View Full Code Here


        buffer.skip(buffer.remaining());
        assertEquals(buffer.position(), buffer.limit());
    }

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

        // Test length 0
        byte[] pattern = new byte[0];
        assertEquals(0, buffer.indexOf(pattern));

        // Test length 1
        pattern = new byte[1];
        for (int i = 0; i < 10; i++) {
            random.nextBytes(pattern);
            assertEquals((int) (pattern[0] & 0xff), buffer.indexOf(pattern));
        }

        // Test length n
        for (int i = 0; i < 10; i++) {
            int pos = random.nextInt(250);
            pattern = new byte[5];
            for (int j = 0; j < pattern.length; j++) {
                pattern[j] = (byte) (pos + j);
            }
            assertEquals(pos, buffer.indexOf(pattern));
        }

        // Test wrong
        pattern = new byte[2];
        byte b = (byte) random.nextInt(256);
        pattern[0] = b;
        pattern[1] = (byte) (b - 1);
        assertEquals(-1, buffer.indexOf(pattern));

        buffer.clear();
        int randomPosition = random.nextInt(buffer.capacity());
        buffer.position(randomPosition);
        buffer = buffer.slice();
        assertEquals(0, buffer.indexOf(new byte[] { (byte) randomPosition }));
    }
View Full Code Here

        buffer = buffer.slice();
        assertEquals(0, buffer.indexOf(new byte[] { (byte) randomPosition }));
    }

    public void testDuplicate() {
        Buffer buffer = newBuffer(random.nextInt(100) + 8);
        buffer.putInt(0);
        Buffer duplicate = buffer.duplicate();
        assertEquals(buffer.position(), duplicate.position());
        assertEquals(buffer.limit(), duplicate.limit());
        assertEquals(buffer.capacity(), duplicate.capacity());

        int num = random.nextInt();
        buffer.putInt(num);
        assertEquals(8, buffer.position());
        assertEquals(4, duplicate.position());
        assertEquals(buffer.limit(), duplicate.limit());
        assertEquals(buffer.capacity(), duplicate.capacity());
        assertEquals(num, duplicate.getInt());
    }
View Full Code Here

        assertEquals(buffer.capacity(), duplicate.capacity());
        assertEquals(num, duplicate.getInt());
    }

    public void testSlice() {
        Buffer buffer = newBuffer(8);
        buffer.putInt(123);
        Buffer slice = buffer.slice();
        assertEquals(0, slice.position());
        assertEquals(4, slice.limit());
        assertEquals(4, slice.capacity());
        assertEquals(4, buffer.position());
        assertEquals(8, buffer.limit());
        assertEquals(8, buffer.capacity());

        int num = random.nextInt();
        buffer.putInt(num);
        assertEquals(8, buffer.position());
        assertEquals(num, slice.getInt());
        assertEquals(4, slice.position());

        buffer.flip();
        slice = buffer.slice();
        assertEquals(0, slice.position());
        assertEquals(8, slice.limit());
        assertEquals(123, slice.getInt());
        assertEquals(num, slice.getInt());
        assertFalse(slice.hasRemaining());
    }
View Full Code Here

        for (int i = 0; i < 10; i++) {
            char[] c = new char[random.nextInt(5000)];
            for (int j = 0; j < c.length; j++) {
                c[j] = (char) ('a' + random.nextInt(26));
            }
            Buffer buffer = newBuffer(c.length * 2);
            String s = new String(c);
            buffer.putString(s, Charset.UTF8);
            assertEquals(buffer.position(), s.length());

            buffer.putString(buffer.position(), s, Charset.UTF8);
            assertEquals(buffer.getString(buffer.position(), Charset.UTF8,
                    c.length), s);
            buffer.flip();
            assertEquals(buffer.getString(Charset.UTF8, c.length), s);
        }
    }
View Full Code Here

            assertEquals(buffer.getString(Charset.UTF8, c.length), s);
        }
    }

    public void testCompact() {
        Buffer buffer = newBuffer(400);

        buffer.putInt(0);
        buffer.compact();
        assertEquals(buffer.remaining(), 4);
        assertEquals(buffer.position(), 396);
        buffer.clear();

        for (int i = 0; i < 100; i++) {
            buffer.putInt(i);
        }
        buffer.flip();
        for (int i = 0; i < 100; i++) {
            assertEquals(i, buffer.getInt());
            buffer.compact();
            assertEquals(buffer.remaining(), 4);
            assertEquals(buffer.position(), 396);
            buffer.clear();
        }

        for (int i = 0; i < 256; i++) {
            buffer.put((byte) i);
        }
        while (buffer.hasRemaining())
            buffer.put((byte) 0);
        buffer.position(100);
        buffer.limit(200);

        Buffer b = buffer.slice();
        b.position(40);
        b.compact();
        assertEquals(b.position(), 60);
        assertEquals(b.limit(), 100);

        for (int i = 0; i < 60; i++) {
            assertEquals(b.get(i), (byte) (140 + i));
        }
    }
View Full Code Here

    private static final byte[] TOKEN = "\r\n\r\n".getBytes();
    private static final Pattern PATTERN = Pattern.compile(" ");

    public Object decode(Session session, Packet packet) throws Exception {
        Buffer buffer = packet.getContent();
        int index = buffer.indexOf(TOKEN);
        if (index >= 0) {
            String[] headers = buffer.getString(Charset.UTF8,
                    index + TOKEN.length).split("\r\n");

            HttpRequest request = new HttpRequest();
            parse(request, headers);

            String contentLen = request.getParam("Content-Length");
            if (contentLen != null) {
                int len = Integer.parseInt(contentLen);
                if (buffer.remaining() >= len) {
                    byte[] content = new byte[len];
                    buffer.get(content);
                    request.setContent(content);
                } else {
                    return null;
                }
            }
View Full Code Here

        session.setPacketEncoder(new BufferEncoder());
        session.setPacketDecoder(new PacketDecoder() {

            public Object decode(Session session, Packet packet)
                    throws Exception {
                Buffer content = packet.getContent();
                return content.getString(Charset.SYSTEM, content.remaining());
            }
        });

        // set session handler
        session.setSessionHandler(new SessionHandlerAdapter() {
View Full Code Here

        private FileChannel fc;
        private String fileName;

        public void objectReceived(Session session, Object obj)
                throws Exception {
            Buffer buffer = (Buffer) obj;
            try {
                if (fc == null) {
                    fileName = buffer.getString(Charset.UTF8, buffer
                            .remaining());
                    System.out.println("Receiving " + fileName + " from "
                            + session.getRemoteAddress());
                    fc = new RandomAccessFile(fileName, "rw").getChannel();
                } else {
                    while (buffer.hasRemaining())
                        buffer.write(fc);
                }
            } finally {
                buffer.release();
            }
        }
View Full Code Here

     * @version $id$
     */
    private static class FileTransferMessageDecoder implements PacketDecoder {

        public Object decode(Session session, Packet packet) throws Exception {
            Buffer buffer = packet.getContent();
            if (buffer.remaining() >= 2) {
                int len = buffer.getUnsignedShort();
                if (buffer.remaining() >= len) {
                    Buffer content = BufferFactory.allocate(len);
                    buffer.get(content);
                    return content.flip();
                }
            }
            return null;
        }
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.