Package net.sf.cindy

Examples of net.sf.cindy.Buffer


            sendContent(session);
        }

        private void sendName(Session session) {
            ByteBuffer name = Charset.UTF8.encode(file.getName());
            Buffer content = BufferFactory.allocate(name.remaining() + 2)
                    .putUnsignedShort(name.remaining()).put(name).flip();
            session.send(content);
        }
View Full Code Here


                sendContent(session);
        }

        private void sendContent(Session session) throws IOException {
            for (int i = 1; i <= QUEUE_SIZE; i++) {
                Buffer buffer = BufferFactory.allocate(MESSAGE_SIZE);
                buffer.position(2);
                int readCount = buffer.read(fc);

                if (readCount == -1) { // end of file
                    buffer.release();
                    session.send(BufferFactory.allocate(0)).addListener(
                            new FutureListener() {

                                public void futureCompleted(Future future)
                                        throws Exception {
                                    future.getSession().close();
                                }
                            });
                    break;
                } else {
                    buffer.putUnsignedShort(0, readCount).flip();
                    session.send(buffer);
                }
            }
        }
View Full Code Here

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

        public Object decode(Session session, Packet packet) throws Exception {
            Buffer buffer = packet.getContent();
            int index = buffer.indexOf(TOKEN);
            if (index >= 0) {
                String s = buffer.getString(Charset.SYSTEM, index
                        - buffer.position());
                buffer.skip(TOKEN.length);
                return s;
            }
            return null;
        }
View Full Code Here

    }

    public Buffer compact() {
        checkReadonly();

        Buffer buffer = BufferFactory.allocate(remaining());
        batch(true, getIndex(0), buffer, buffer.capacity());
        buffer.position(0);

        int index = getIndex(0, 0);
        Entry entry = getEntry(index);
        int offset = index - entry.position;

        do {
            Buffer content = entry.buffer;
            buffer.limit(Math.min(buffer.capacity(), buffer.position()
                    + content.remaining() - offset));
            content.put(content.position() + offset, buffer);
            if (buffer.position() == buffer.capacity())
                break;
            offset = 0;
        } while ((entry = entry.next) != header);
View Full Code Here

        return batch(false, putIndex(index, length), src, length);
    }

    private short decodeShort(int index) {
        Entry entry = getEntry(index);
        Buffer buffer = entry.buffer;

        int idx = buffer.position() + index - entry.position;
        if (buffer.limit() - idx >= 2)
            return buffer.getShort(idx);
        return Bits.decodeShort(this, index);
    }
View Full Code Here

        return Bits.decodeShort(this, index);
    }

    private Buffer encodeShort(int index, short s) {
        Entry entry = getEntry(index);
        Buffer buffer = entry.buffer;

        int idx = buffer.position() + index - entry.position;
        if (buffer.limit() - idx >= 2)
            buffer.putShort(idx, s);
        else
            Bits.encodeShort(this, index, s);
        return this;
    }
View Full Code Here

        return encodeShort(putIndex(index, 2), s);
    }

    private int decodeInt(int index) {
        Entry entry = getEntry(index);
        Buffer buffer = entry.buffer;

        int idx = buffer.position() + index - entry.position;
        if (buffer.limit() - idx >= 4)
            return buffer.getInt(idx);
        return Bits.decodeInt(this, index);
    }
View Full Code Here

        return Bits.decodeInt(this, index);
    }

    private Buffer encodeInt(int index, int i) {
        Entry entry = getEntry(index);
        Buffer buffer = entry.buffer;

        int idx = buffer.position() + index - entry.position;
        if (buffer.limit() - idx >= 4)
            buffer.putInt(idx, i);
        else
            Bits.encodeInt(this, index, i);
        return this;
    }
View Full Code Here

        return encodeInt(putIndex(index, 4), i);
    }

    private long decodeLong(int index) {
        Entry entry = getEntry(index);
        Buffer buffer = entry.buffer;

        int idx = buffer.position() + index - entry.position;
        if (buffer.limit() - idx >= 8)
            return buffer.getLong(idx);
        return Bits.decodeLong(this, index);
    }
View Full Code Here

        return Bits.decodeLong(this, index);
    }

    private Buffer encodeLong(int index, long l) {
        Entry entry = getEntry(index);
        Buffer buffer = entry.buffer;

        int idx = buffer.position() + index - entry.position;
        if (buffer.limit() - idx >= 8)
            buffer.putLong(idx, l);
        else
            Bits.encodeLong(this, index, l);
        return this;
    }
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.