Example usage for javax.net.ssl SSLEngineResult bytesProduced

List of usage examples for javax.net.ssl SSLEngineResult bytesProduced

Introduction

In this page you can find the example usage for javax.net.ssl SSLEngineResult bytesProduced.

Prototype

int bytesProduced

To view the source code for javax.net.ssl SSLEngineResult bytesProduced.

Click Source Link

Usage

From source file:org.apache.hadoop.ipc.ServerRpcSSLEngineImpl.java

@Override
public int read(ReadableByteChannel channel, ByteBuffer buffer, Server.Connection connection)
        throws IOException {
    int netRead = channel.read(clientNetBuffer);
    if (netRead == -1) {
        return -1;
    }// ww w  .  ja  v a2 s.c o m

    int read = 0;
    SSLEngineResult unwrapResult;
    do {
        clientNetBuffer.flip();
        unwrapResult = sslEngine.unwrap(clientNetBuffer, clientAppBuffer);
        clientNetBuffer.compact();

        if (unwrapResult.getStatus().equals(SSLEngineResult.Status.OK)) {
            read += unwrapResult.bytesProduced();
            clientAppBuffer.flip();

            while (clientAppBuffer.hasRemaining()) {
                byte currentByte = clientAppBuffer.get();
                try {
                    buffer.put(currentByte);
                } catch (BufferOverflowException ex) {
                    if (buffer.capacity() < maxUnWrappedDataLength) {
                        buffer = enlargeUnwrappedBuffer(buffer, currentByte);
                        connection.setSslUnwrappedBuffer(buffer);
                    } else {
                        LOG.error("Buffer overflow clientAppBuffer position: " + clientAppBuffer.position()
                                + " but buffer capacity " + buffer.capacity(), ex);
                        throw ex;
                    }
                }
            }
            clientAppBuffer.compact();
        } else if (unwrapResult.getStatus().equals(SSLEngineResult.Status.BUFFER_UNDERFLOW)) {
            read += unwrapResult.bytesProduced();
            break;
        } else if (unwrapResult.getStatus().equals(SSLEngineResult.Status.BUFFER_OVERFLOW)) {
            clientAppBuffer = enlargeApplicationBuffer(clientAppBuffer);
        } else if (unwrapResult.getStatus().equals(SSLEngineResult.Status.CLOSED)) {
            sslEngine.closeOutbound();
            doHandshake();
            read = -1;
            break;
        } else {
            throw new IOException("SSLEngine UNWRAP invalid status: " + unwrapResult.getStatus());
        }
    } while (clientNetBuffer.position() != 0);

    return read;
}