@Override public void open() throws IOException { if (sslChannel == null) { super.open(); sslChannel = new SSLSocketChannel(sslContext, channel, true); } sslChannel.setTimeout(timeout); // SSLSocketChannel will check if already connected so we can safely call this sslChannel.connect(); sslOutputStream = new SSLSocketChannelOutputStream(sslChannel); }
@Override public boolean isClosed() { return sslSocketChannel.isClosed(); }
/** * Closes the underlying SSLSocketChannel, which will also close the OutputStream and connection */ @Override public void close() throws IOException { channel.close(); }
final SSLSocketChannel socketChannel = new SSLSocketChannel(sslContext, hostname, port, localAddress, true); socketChannel.connect(); commsSession.setUserDn(socketChannel.getDn()); } catch (final CertificateException ex) { throw new IOException(ex);
public SSLCommsSession(final SSLContext sslContext, final String hostname, final int port, final int timeoutMillis) throws IOException { final SocketChannel socketChannel = SocketChannel.open(); socketChannel.socket().connect(new InetSocketAddress(hostname, port), timeoutMillis); socketChannel.configureBlocking(false); sslSocketChannel = new SSLSocketChannel(sslContext, socketChannel,true); in = new SSLSocketChannelInputStream(sslSocketChannel); bufferedIn = new BufferedInputStream(in); out = new SSLSocketChannelOutputStream(sslSocketChannel); bufferedOut = new BufferedOutputStream(out); this.sslContext = sslContext; this.hostname = hostname; this.port = port; }
connect(); int copied = copyFromAppDataBuffer(buffer, offset, len); if (copied > 0) { return copied; final int bytesRead = readData(writableInBuffer); if (bytesRead < 0) { return -1; throw new IOException("Channel is closed"); case OK: { copied = copyFromAppDataBuffer(buffer, offset, len); if (copied == 0) { throw new IOException("Failed to decrypt data");
public void write(final int data) throws IOException { write(new byte[]{(byte) data}, 0, 1); }
public int read() throws IOException { final int bytesRead = read(oneByteBuffer); if (bytesRead == -1) { return -1; } return oneByteBuffer[0] & 0xFF; }
public void write(final byte[] data, final int offset, final int len) throws IOException { logger.debug("{} Writing {} bytes of data", this, len); if (!connected) { connect(); } int iterations = len / MAX_WRITE_SIZE; if (len % MAX_WRITE_SIZE > 0) { iterations++; } for (int i = 0; i < iterations; i++) { streamOutManager.clear(); final int itrOffset = offset + i * MAX_WRITE_SIZE; final int itrLen = Math.min(len - itrOffset, MAX_WRITE_SIZE); final ByteBuffer byteBuffer = ByteBuffer.wrap(data, itrOffset, itrLen); final BufferStateManager buffMan = new BufferStateManager(byteBuffer, Direction.READ); final Status status = encryptAndWriteFully(buffMan); switch (status) { case BUFFER_OVERFLOW: streamOutManager.ensureSize(engine.getSession().getPacketBufferSize()); appDataManager.ensureSize(engine.getSession().getApplicationBufferSize()); continue; case OK: continue; case CLOSED: throw new IOException("Channel is closed"); case BUFFER_UNDERFLOW: throw new AssertionError("Got Buffer Underflow but should not have..."); } } }
@Override public void setTimeout(final int millis) throws IOException { channel.setTimeout(millis); }
@Override public int getTimeout() throws IOException { return channel.getTimeout(); }
@Override public void interrupt() { sslSocketChannel.interrupt(); }
sslSocketChannel = new SSLSocketChannel(sslEngine, socketChannel);
connect(); int copied = copyFromAppDataBuffer(buffer, offset, len); if (copied > 0) { return copied; final int bytesRead = readData(writableInBuffer); if (bytesRead < 0) { return -1; throw new IOException("Channel is closed"); case OK: { copied = copyFromAppDataBuffer(buffer, offset, len); if (copied == 0) { throw new IOException("Failed to decrypt data");
@Override protected void write(byte[] data) throws IOException { sslChannel.write(data); }
@Override public int read() throws IOException { return channel.read(); }
public void write(final byte[] data, final int offset, final int len) throws IOException { logger.debug("{} Writing {} bytes of data", this, len); if (!connected) { connect(); } int iterations = len / MAX_WRITE_SIZE; if (len % MAX_WRITE_SIZE > 0) { iterations++; } for (int i = 0; i < iterations; i++) { streamOutManager.clear(); final int itrOffset = offset + i * MAX_WRITE_SIZE; final int itrLen = Math.min(len - itrOffset, MAX_WRITE_SIZE); final ByteBuffer byteBuffer = ByteBuffer.wrap(data, itrOffset, itrLen); final BufferStateManager buffMan = new BufferStateManager(byteBuffer, Direction.READ); final Status status = encryptAndWriteFully(buffMan); switch (status) { case BUFFER_OVERFLOW: streamOutManager.ensureSize(engine.getSession().getPacketBufferSize()); appDataManager.ensureSize(engine.getSession().getApplicationBufferSize()); continue; case OK: continue; case CLOSED: throw new IOException("Channel is closed"); case BUFFER_UNDERFLOW: throw new AssertionError("Got Buffer Underflow but should not have..."); } } }
@Override public void setTimeout(final long value, final TimeUnit timeUnit) { sslSocketChannel.setTimeout((int) TimeUnit.MILLISECONDS.convert(value, timeUnit)); }
@Override public long getTimeout(final TimeUnit timeUnit) { return timeUnit.convert(sslSocketChannel.getTimeout(), TimeUnit.MILLISECONDS); }
@Override public void interrupt() { channel.interrupt(); }