Java ByteBuffer Write write(SocketChannel p_channel, SSLEngine p_sslEngine, ByteBuffer p_outAppBuf, ByteBuffer p_outNetBuf)

Here you can find the source of write(SocketChannel p_channel, SSLEngine p_sslEngine, ByteBuffer p_outAppBuf, ByteBuffer p_outNetBuf)

Description

write

License

Apache License

Declaration

public static void write(SocketChannel p_channel, SSLEngine p_sslEngine, ByteBuffer p_outAppBuf,
            ByteBuffer p_outNetBuf) 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/*from   w ww. j av a2 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 {
    public static void write(SocketChannel p_channel, SSLEngine p_sslEngine, ByteBuffer p_outAppBuf,
            ByteBuffer p_outNetBuf) throws IOException {
        assert p_outNetBuf.limit() == p_outNetBuf.capacity();

        while (p_outAppBuf.hasRemaining()) {
            final SSLEngineResult l_res;

            // encrypt data
            l_res = p_sslEngine.wrap(p_outAppBuf, p_outNetBuf);

            switch (l_res.getStatus()) {
            case OK:
            case BUFFER_OVERFLOW:
                p_outNetBuf.flip();

                // send the encrypted data to the peer
                while (p_outNetBuf.hasRemaining()) // coward loop
                {
                    p_channel.write(p_outNetBuf);

                    if (p_outNetBuf.hasRemaining())
                        Thread.yield(); // should not occur in blocking mode
                }

                p_outNetBuf.clear();
                break;

            case BUFFER_UNDERFLOW:
                throw new AssertionError("SSL write status BUFFER_UNDERFLOW.");

            case CLOSED:
                throw new AssertionError("SSL write status CLOSED.");

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

Related

  1. write(ByteBuffer data, String filename, boolean append)
  2. write(final byte[] array, final int offset, final int end, final ByteBuffer outBB)
  3. write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length)
  4. write(MappedByteBuffer buffer, int pos, String asciString)
  5. write(SocketChannel channel, ByteBuffer b, PrimitiveIterator.OfInt iterator)
  6. write(WritableByteChannel channel, ByteBuffer buffer, byte[] data)
  7. write(WritableByteChannel channel, ByteBuffer[] srcs)
  8. write(WritableByteChannel channel, ByteBuffer[] srcs, int offset, int length)
  9. write(WritableByteChannel out, ByteBuffer buffer)