Java ByteBuffer Write write(WritableByteChannel socketChannel, SSLEngine engine, ByteBuffer plainOut, ByteBuffer cypherOut)

Here you can find the source of write(WritableByteChannel socketChannel, SSLEngine engine, ByteBuffer plainOut, ByteBuffer cypherOut)

Description

write

License

Open Source License

Declaration

public static void write(WritableByteChannel socketChannel, SSLEngine engine, ByteBuffer plainOut,
            ByteBuffer cypherOut) throws IOException 

Method Source Code


//package com.java2s;
// This software, the RabbitMQ Java client library, is triple-licensed under the

import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLException;
import java.io.IOException;
import java.nio.ByteBuffer;

import java.nio.channels.WritableByteChannel;

public class Main {
    public static void write(WritableByteChannel socketChannel, SSLEngine engine, ByteBuffer plainOut,
            ByteBuffer cypherOut) throws IOException {
        while (plainOut.hasRemaining()) {
            cypherOut.clear();//from w  w  w.jav  a 2 s. c  om
            SSLEngineResult result = engine.wrap(plainOut, cypherOut);
            switch (result.getStatus()) {
            case OK:
                cypherOut.flip();
                while (cypherOut.hasRemaining()) {
                    socketChannel.write(cypherOut);
                }
                break;
            case BUFFER_OVERFLOW:
                throw new SSLException("Buffer overflow occured after a wrap.");
            case BUFFER_UNDERFLOW:
                throw new SSLException("Buffer underflow occured after a wrap.");
            case CLOSED:
                throw new SSLException("Buffer closed");
            default:
                throw new IllegalStateException("Invalid SSL status: " + result.getStatus());
            }
        }
    }

    private static SSLEngineResult.HandshakeStatus wrap(ByteBuffer plainOut, ByteBuffer cipherOut,
            WritableByteChannel channel, SSLEngine sslEngine) throws IOException {
        SSLEngineResult.HandshakeStatus handshakeStatus = sslEngine.getHandshakeStatus();
        SSLEngineResult.Status status = sslEngine.wrap(plainOut, cipherOut).getStatus();
        switch (status) {
        case OK:
            handshakeStatus = runDelegatedTasks(sslEngine);
            cipherOut.flip();
            while (cipherOut.hasRemaining()) {
                channel.write(cipherOut);
            }
            cipherOut.clear();
            break;
        case BUFFER_OVERFLOW:
            throw new SSLException("Buffer overflow during handshake");
        default:
            throw new SSLException("Unexpected status " + status);
        }
        return handshakeStatus;
    }

    private static SSLEngineResult.HandshakeStatus runDelegatedTasks(SSLEngine sslEngine) {
        // FIXME run in executor?
        Runnable runnable;
        while ((runnable = sslEngine.getDelegatedTask()) != null) {
            runnable.run();
        }
        return sslEngine.getHandshakeStatus();
    }
}

Related

  1. write(SocketChannel p_channel, SSLEngine p_sslEngine, ByteBuffer p_outAppBuf, ByteBuffer p_outNetBuf)
  2. write(WritableByteChannel channel, ByteBuffer buffer, byte[] data)
  3. write(WritableByteChannel channel, ByteBuffer[] srcs)
  4. write(WritableByteChannel channel, ByteBuffer[] srcs, int offset, int length)
  5. write(WritableByteChannel out, ByteBuffer buffer)
  6. writeAll(ByteBuffer buf, WritableByteChannel channel)
  7. writeAll(ByteChannel channel, ByteBuffer buffer)
  8. writeAll(GatheringByteChannel ch, ByteBuffer... bbs)
  9. writeAllToChannel(List buffers, WritableByteChannel channel)