Package net.sf.cindy

Examples of net.sf.cindy.Buffer


    public Buffer putLong(int index, long l) {
        return encodeLong(putIndex(index, 8), l);
    }

    private String _getString(int i, Charset charset, int bufferLen) {
        Buffer buffer = BufferFactory.allocate(bufferLen);
        batch(true, i, buffer, bufferLen);
        buffer.position(0);
        try {
            return charset.decode(buffer.asByteBuffer());
        } finally {
            buffer.release();
        }
    }
View Full Code Here


    }

    public boolean equals(Object obj) {
        if (!(obj instanceof Buffer))
            return false;
        Buffer buffer = (Buffer) obj;
        if (this.remaining() != buffer.remaining())
            return false;
        int start = getIndex(0);
        int end = start + remaining();
        for (int i = start, j = buffer.position(); i < end; i++, j++) {
            if (_get(i) != buffer.get(j))
                return false;
        }
        return true;
    }
View Full Code Here

    public Packet encode(Session session, Object obj) throws Exception {
        HttpResponse response = (HttpResponse) obj;

        ByteBuffer header = Charset.UTF8.encode(response.toString());

        Buffer buffer = null;
        int contentLen = response.getContent().length;
        if (contentLen > 0) {
            buffer = BufferFactory.allocate(header.remaining()
                    + response.getContent().length);
            buffer.put(header).put(response.getContent()).flip();
        } else {
            buffer = BufferFactory.wrap(header);
        }

        return new DefaultPacket(buffer);
View Full Code Here

        while ((runnable = engine.getDelegatedTask()) != null)
            runnable.run();
    }

    private Packet decode(Session session, Packet packet) throws Exception {
        Buffer src = packet.getContent();
        ByteBuffer srcBuffer = src.asByteBuffer();

        int size = appBufferSize
                * (int) Math.ceil((double) (src.remaining() + 1)
                        / netBufferSize);
        Buffer dest = BufferFactory.allocate(size);
        ByteBuffer destBuffer = dest.asByteBuffer();

        boolean hasAppData = false;

        // decode
        while (true) {
            SSLEngineResult result = engine.unwrap(srcBuffer, destBuffer);
            Status status = result.getStatus();
            if (status == Status.OK) {
                HandshakeStatus handshakeStatus = result.getHandshakeStatus();
                if (handshakeStatus == HandshakeStatus.NOT_HANDSHAKING) {
                    hasAppData = true;
                    if (!srcBuffer.hasRemaining()) // decode completed
                        break;
                } else if (handshakeStatus == HandshakeStatus.NEED_TASK) {
                    runTask();
                } else if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
                    // unwrap again
                } else if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
                    // need send some handshake packet
                    session.flush(new HandshakePacket());
                    break;
                } else if (handshakeStatus == HandshakeStatus.FINISHED) {
                    handshakeCompleted();
                }
            } else if (status == Status.BUFFER_UNDERFLOW) {
                // need more content, wait next time
                break;
            } else if (status == Status.BUFFER_OVERFLOW) {
                // increase dest capacity
                destBuffer.flip();
                Buffer newDest = BufferFactory.allocate(
                        appBufferSize + destBuffer.remaining()).put(destBuffer);
                dest.release();
                dest = newDest;
                destBuffer = dest.asByteBuffer();
            } else if (status == Status.CLOSED) {
View Full Code Here

        if (decodedPacket != null)
            super.packetReceived(filterChain, decodedPacket);
    }

    private Packet encode(Session session, Packet packet) throws Exception {
        Buffer src = packet.getContent();
        ByteBuffer srcBuffer = src.asByteBuffer();

        int size = netBufferSize
                * (int) Math.ceil((double) (src.remaining() + 1)
                        / appBufferSize);
        Buffer dest = BufferFactory.allocate(size);
        ByteBuffer destBuffer = dest.asByteBuffer();

        // encode
        while (true) {
            SSLEngineResult result = engine.wrap(srcBuffer, destBuffer);
            Status status = result.getStatus();
            if (status == Status.OK) {
                HandshakeStatus handshakeStatus = result.getHandshakeStatus();
                if (handshakeStatus == HandshakeStatus.NOT_HANDSHAKING) {
                    if (!srcBuffer.hasRemaining()) // encode completed
                        break;
                } else if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
                    break; // wait receive
                } else if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
                    // wrap again
                } else if (handshakeStatus == HandshakeStatus.NEED_TASK) {
                    runTask();
                } else if (handshakeStatus == HandshakeStatus.FINISHED) {
                    handshakeCompleted();
                    break;
                }
            } else if (status == Status.BUFFER_OVERFLOW) {
                // increase dest capacity
                destBuffer.flip();
                Buffer newDest = BufferFactory.allocate(
                        netBufferSize + destBuffer.remaining()).put(destBuffer);
                dest.release();
                dest = newDest;
                destBuffer = dest.asByteBuffer();
            } else if (status == Status.CLOSED) {
View Full Code Here

                return new SelectableChannel[] { channel };
            }

            protected void read() throws IOException {
                while (true) {
                    Buffer buffer = BufferFactory.allocate(getReadPacketSize());
                    ByteBuffer byteBuffer = buffer.asByteBuffer();

                    try {
                        SocketAddress address = channel.receive(byteBuffer);
                        if (address == null) {
                            buffer.release();
                            break;
                        }
                        buffer.limit(byteBuffer.position());
                        getSessionFilterChain(false).packetReceived(
                                new DefaultPacket(buffer, address));
                    } catch (IOException e) {
                        buffer.release();
                        throw e;
                    }
                }
            }

            private boolean isConnected() {
                if (channel == null)
                    return false;
                return channel.isConnected();
            }

            protected void checkSendPacket(Packet packet) {
                if (!isConnected() && packet.getAddress() == null)
                    throw new RuntimeException(
                            "can't send packet to unconnected datagram channel without socket address");
            }

            protected boolean write(Packet packet) throws IOException {
                Buffer buffer = packet.getContent();
                int writeCount = 0;
                if (packet.getAddress() == null) // connected
                    writeCount = buffer.write(channel);
                else {
                    writeCount = channel.send(buffer.asByteBuffer(), packet
                            .getAddress());
                    buffer.skip(writeCount);
                }
                return writeCount != 0;
            }

        };
View Full Code Here

* @version $id$
*/
public class SerialDecoder implements PacketDecoder {

    public Object decode(Session session, Packet packet) throws Exception {
        final Buffer buffer = packet.getContent();
        if (buffer.remaining() < 4)
            return null;

        if (buffer.getShort() == ObjectStreamConstants.STREAM_MAGIC
                && buffer.getShort() == ObjectStreamConstants.STREAM_VERSION) {
            buffer.skip(-4);

            ObjectInputStream is = null;
            try {
                is = new ObjectInputStream(new InputStream() {

                    public int read() {
                        return buffer.hasRemaining() ? buffer.get() : -1;
                    }

                    public int read(byte[] bytes, int off, int len) {
                        if (buffer.hasRemaining()) {
                            len = Math.min(len, buffer.remaining());
                            buffer.get(bytes, off, len);
                            return len;
                        }
                        return -1;
                    }

View Full Code Here

* @version $id$
*/
public class SimplePacketDecoder implements PacketDecoder {

    public Object decode(Session session, Packet packet) throws Exception {
        Buffer buffer = packet.getContent();
        return new DefaultPacket(BufferFactory.allocate(buffer.remaining())
                .put(0, buffer), packet.getAddress());
    }
View Full Code Here

* @version $id$
*/
public class ByteBufferDecoder implements PacketDecoder {

    public Object decode(Session session, Packet packet) throws Exception {
        Buffer buffer = packet.getContent();
        ByteBuffer content = ByteBuffer.allocate(buffer.remaining());
        buffer.get(content);
        return content.flip();
    }
View Full Code Here

                    close();
                }
            }

            protected void read() throws IOException {
                Buffer buffer = BufferFactory.allocate(getReadPacketSize());
                int n = -1;
                int readCount = 0;

                try {
                    while ((n = buffer.read(channel)) >= 0) {
                        if (n == 0)
                            break;
                        readCount += n;
                    }
                } catch (IOException e) {
                    buffer.release();
                    throw e;
                }

                if (readCount > 0) {
                    buffer.flip();
                    getSessionFilterChain(false).packetReceived(
                            new DefaultPacket(buffer, address));
                }
                if (n < 0) // Connection closed
                    throw new ClosedChannelException();
            }

            protected boolean write(Packet packet) throws IOException {
                Buffer buffer = packet.getContent();
                while (true) {
                    int n = buffer.write(channel);
                    if (!buffer.hasRemaining())
                        return true;
                    else if (n == 0) {
                        // have more data, but the kennel buffer
                        // is full, wait next time to write
                        return false;
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.