Package org.apache.mina.common

Examples of org.apache.mina.common.ByteBuffer


        SSLHandler handler = getSSLSessionHandler(session);
        synchronized (handler) {
            if (!isSSLStarted(session) && handler.isInboundDone()) {
                handler.scheduleMessageReceived(nextFilter, message);
            } else {
                ByteBuffer buf = (ByteBuffer) message;
                if (SessionLog.isDebugEnabled(session)) {
                    SessionLog.debug(session, " Data Read: " + handler + " ("
                            + buf + ')');
                }

                try {
                    // forward read encrypted data to SSL handler
                    handler.messageReceived(nextFilter, buf.buf());

                    // Handle data to be forwarded to application or written to net
                    handleSSLData(nextFilter, handler);

                    if (handler.isInboundDone()) {
                        if (handler.isOutboundDone()) {
                            if (SessionLog.isDebugEnabled(session)) {
                                SessionLog.debug(session,
                                        " SSL Session closed.");
                            }

                            handler.destroy();
                        } else {
                            initiateClosure(nextFilter, session);
                        }

                        if (buf.hasRemaining()) {
                            // Forward the data received after closure.
                            handler.scheduleMessageReceived(nextFilter, buf);
                        }
                    }
                } catch (SSLException ssle) {
View Full Code Here


        // increase the count to as many as required to generate a long
        // string for input
        for (int i = 0; i < 10; i++) {
            strInput += "The quick brown fox jumps over the lazy dog.  ";
        }
        ByteBuffer byteInput = ByteBuffer.wrap(strInput.getBytes("UTF8"));

        // increase the count to have the compression and decompression
        // done using the same instance of Zlib
        for (int i = 0; i < 5; i++) {
            ByteBuffer byteCompressed = deflater.deflate(byteInput);
            ByteBuffer byteUncompressed = inflater.inflate(byteCompressed);
            String strOutput = byteUncompressed.getString(Charset.forName(
                    "UTF8").newDecoder());
            assertTrue(strOutput.equals(strInput));
        }
    }
View Full Code Here

                session.removeAttribute(DISABLE_ENCRYPTION_ONCE);
                handler.scheduleFilterWrite(nextFilter,
                        writeRequest);
            } else {
                // Otherwise, encrypt the buffer.
                ByteBuffer buf = (ByteBuffer) writeRequest.getMessage();

                if (SessionLog.isDebugEnabled(session)) {
                    SessionLog.debug(session, " Filtered Write: " + handler);
                }

                if (handler.isWritingEncryptedData()) {
                    // data already encrypted; simply return buffer
                    if (SessionLog.isDebugEnabled(session)) {
                        SessionLog.debug(session, "   already encrypted: "
                                + buf);
                    }
                    handler.scheduleFilterWrite(nextFilter,
                            writeRequest);
                } else if (handler.isHandshakeComplete()) {
                    // SSL encrypt
                    if (SessionLog.isDebugEnabled(session)) {
                        SessionLog.debug(session, " encrypt: " + buf);
                    }

                    int pos = buf.position();
                    handler.encrypt(buf.buf());
                    buf.position(pos);
                    ByteBuffer encryptedBuffer = new EncryptedBuffer(SSLHandler
                            .copy(handler.getOutNetBuffer()), buf);

                    if (SessionLog.isDebugEnabled(session)) {
                        SessionLog.debug(session, " encrypted buf: "
                                + encryptedBuffer);
View Full Code Here

        }
    }

    public void testCorruptedData() throws Exception {
        String strInput = "Hello World";
        ByteBuffer byteInput = ByteBuffer.wrap(strInput.getBytes("UTF8"));

        ByteBuffer byteCompressed = deflater.deflate(byteInput);
        // change the contents to something else. Since this doesn't check
        // for integrity, it wont throw an exception
        byteCompressed.put(5, (byte) 0xa);
        ByteBuffer byteUncompressed = inflater.inflate(byteCompressed);
        String strOutput = byteUncompressed.getString(Charset.forName("UTF8")
                .newDecoder());
        assertFalse(strOutput.equals(strInput));
    }
View Full Code Here

        if (SessionLog.isDebugEnabled(session)) {
            SessionLog.debug(session, " appBuffer: " + handler.getAppBuffer());
        }

        // forward read app data
        ByteBuffer readBuffer = SSLHandler.copy(handler.getAppBuffer());
        handler.getAppBuffer().clear();
        if (SessionLog.isDebugEnabled(session)) {
            SessionLog.debug(session, " app data read: " + readBuffer + " ("
                    + readBuffer.getHexDump() + ')');
        }

        handler.scheduleMessageReceived(nextFilter, readBuffer);
    }
View Full Code Here

        assertFalse(strOutput.equals(strInput));
    }

    public void testCorruptedHeader() throws Exception {
        String strInput = "Hello World";
        ByteBuffer byteInput = ByteBuffer.wrap(strInput.getBytes("UTF8"));

        ByteBuffer byteCompressed = deflater.deflate(byteInput);
        // write a bad value into the zlib header. Make sure that
        // the decompression fails
        byteCompressed.put(0, (byte) 0xca);
        try {
            inflater.inflate(byteCompressed);
        } catch (IOException e) {
            assertTrue(true);
            return;
View Full Code Here

    public void testFragments() throws Exception {
        String strInput = "";
        for (int i = 0; i < 10; i++) {
            strInput += "The quick brown fox jumps over the lazy dog.  ";
        }
        ByteBuffer byteInput = ByteBuffer.wrap(strInput.getBytes("UTF8"));
        ByteBuffer byteCompressed = null;

        for (int i = 0; i < 5; i++) {
            byteCompressed = deflater.deflate(byteInput);
            if (i == 0) {
                // decompress the first compressed output since it contains
                // the zlib header, which will not be generated for further
                // compressions done with the same instance
                ByteBuffer byteUncompressed = inflater.inflate(byteCompressed);
                String strOutput = byteUncompressed.getString(Charset.forName(
                        "UTF8").newDecoder());
                assertTrue(strOutput.equals(strInput));
            }
        }
        // check if the last compressed data block can be decompressed
        // successfully.
        ByteBuffer byteUncompressed = inflater.inflate(byteCompressed);
        String strOutput = byteUncompressed.getString(Charset.forName("UTF8")
                .newDecoder());
        assertTrue(strOutput.equals(strInput));
    }
View Full Code Here

        if (message instanceof InputStream) {

            InputStream inputStream = (InputStream) message;

            ByteBuffer byteBuffer = getNextByteBuffer(inputStream);
            if (byteBuffer == null) {
                // End of stream reached.
                writeRequest.getFuture().setWritten(true);
                nextFilter.messageSent(session, message);
            } else {
View Full Code Here

                .getAttribute(CURRENT_STREAM);

        if (inputStream == null) {
            nextFilter.messageSent(session, message);
        } else {
            ByteBuffer byteBuffer = getNextByteBuffer(inputStream);

            if (byteBuffer == null) {
                // End of stream reached.
                session.removeAttribute(CURRENT_STREAM);
                WriteFuture writeFuture = (WriteFuture) session
View Full Code Here

            }

            int msgLen = msgBuf.getShort() & 0xFFFF;

            // Get a read-only buffer view on the complete message
            ByteBuffer completeMessage =
                msgBuf.slice().asReadOnlyBuffer().limit(msgLen);

            // Advance the underlying message buffer
            msgBuf.skip(msgLen);
View Full Code Here

TOP

Related Classes of org.apache.mina.common.ByteBuffer

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.