Java ByteBuffer Read read(SocketChannel p_channel, SSLEngine p_sslEngine, ByteBuffer p_inAppBuf, ByteBuffer p_inNetBuf)

Here you can find the source of read(SocketChannel p_channel, SSLEngine p_sslEngine, ByteBuffer p_inAppBuf, ByteBuffer p_inNetBuf)

Description

The buffer p_inAppBuf is flipped and contains decrypted data when this method returns.

The buffer p_inNetBuf is compacted when this method returns.

License

Apache License

Parameter

Parameter Description
p_inAppBuf A compacted buffer that will receive decrypted data.
p_inNetBuf An buffer that will receive network data.

Declaration

public static int read(SocketChannel p_channel, SSLEngine p_sslEngine, ByteBuffer p_inAppBuf,
        ByteBuffer p_inNetBuf) throws IOException 

Method Source Code

//package com.java2s;
/* $Id: SSLUtil.java 394 2009-03-21 20:28:26Z  $
 *
 * (C) Copyright 2008-2013 Alexander Veit
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at/*  ww w .j av a  2  s.  c o m*/
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.SocketChannel;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;

public class Main {
    /**
     * <p>The buffer <code>p_inAppBuf</code> is flipped and contains
     * decrypted data when this method returns.</p>
     * <p>The buffer <code>p_inNetBuf</code> is compacted when this
     * method returns. The caller must not modify the contents of this
     * buffer between two subsequent calls of this method</p>
     * @param p_inAppBuf A compacted buffer that will receive decrypted data.
     * @param p_inNetBuf An buffer that will receive network data.
     */
    public static int read(SocketChannel p_channel, SSLEngine p_sslEngine, ByteBuffer p_inAppBuf,
            ByteBuffer p_inNetBuf) throws IOException {
        final int l_iBytesBefore;
        final int l_iBytesAfter;

        // the buffers must be compacted
        assert p_inAppBuf.limit() == p_inAppBuf.capacity();
        assert p_inNetBuf.limit() == p_inNetBuf.capacity();

        l_iBytesBefore = p_inAppBuf.position();

        while (true) {
            final int l_iRead;
            final SSLEngineResult l_res;

            p_inNetBuf.flip();

            l_res = p_sslEngine.unwrap(p_inNetBuf, p_inAppBuf);

            p_inNetBuf.compact();

            // check status
            switch (l_res.getStatus()) {
            case OK:
                l_iBytesAfter = p_inAppBuf.position();

                p_inAppBuf.flip();

                return l_iBytesAfter - l_iBytesBefore;

            case BUFFER_UNDERFLOW:
                // read data
                l_iRead = p_channel.read(p_inNetBuf);

                if (l_iRead == -1) {
                    assert p_inNetBuf.position() == 0;
                    return -1;
                }

                break;

            case CLOSED:
                // the client sent an SSL close message
                return -1;

            case BUFFER_OVERFLOW:
                throw new AssertionError("SSL read status BUFFER_OVERFLOW.");

            default:
                throw new AssertionError("SSL read: unexpected status " + l_res.getStatus() + ".");
            }
        }
    }
}

Related

  1. read(ByteBuffer bb, int elementWidth)
  2. read(ByteBuffer buffer)
  3. read(ByteBuffer dest, ByteBuffer src)
  4. read(ByteBuffer src, int len)
  5. read(final ByteBuffer buffer, final byte marker)
  6. readableBytes(ByteBuffer buffer)
  7. readAlexString(ByteBuffer buffer)
  8. readAllLeftBytes(ByteBuffer buffer)
  9. readASCII(ByteBuffer buffer)