Java HTTP Proxy Library Project Page
SourceForge.net Logo

com.wpg.proxy
Class SSLByteChannel

java.lang.Object
  extended by com.wpg.proxy.SSLByteChannel
All Implemented Interfaces:
java.io.Closeable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel, java.nio.channels.WritableByteChannel

public class SSLByteChannel
extends java.lang.Object
implements java.nio.channels.ByteChannel

Upgrade a ByteChannel for SSL.

Change Log:

This source code is given to the Public Domain. Do what you want with it. This software comes with no guarantees or warranties. Please visit http://perso.wanadoo.fr/reuse/sslbytechannel/ periodically to check for updates or to contribute improvements.

usage: KeyStore ks = KeyStore.getInstance("JKS"); File kf = new File("keystore"); ks.load(new FileInputStream(kf), "storepassword".toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, "keypassword".toCharArray()); TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); tmf.init(ks); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); SSLEngine engine = sslContext.createSSLEngine(); engine.setUseClientMode(false); engine.beginHandshake(); SSLByteChannel sslByteChannel = new SSLByteChannel(channel, engine);

Version:
1.0.0
Author:
David Crosson, david.crosson@wanadoo.fr

Constructor Summary
SSLByteChannel(java.nio.channels.ByteChannel wrappedChannel, javax.net.ssl.SSLEngine engine)
          Creates a new instance of SSLByteChannel
 
Method Summary
 void close()
          Ends SSL operation and close the wrapped byte channel
 boolean isOpen()
          Is the channel open ?
 int read(java.nio.ByteBuffer byteBuffer)
          Fill the given buffer with some bytes and return the number of bytes added in the buffer.
This method may return immediately with nothing added in the buffer.
 int write(java.nio.ByteBuffer byteBuffer)
          Write remaining bytes of the given byte buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SSLByteChannel

public SSLByteChannel(java.nio.channels.ByteChannel wrappedChannel,
                      javax.net.ssl.SSLEngine engine)
Creates a new instance of SSLByteChannel

Parameters:
wrappedChannel - The byte channel on which this ssl channel is built. This channel contains encrypted data.
engine - A SSLEngine instance that will remember SSL current context. Warning, such an instance CAN NOT be shared between multiple SSLByteChannel.
Method Detail

close

public void close()
           throws java.io.IOException
Ends SSL operation and close the wrapped byte channel

Specified by:
close in interface java.io.Closeable
Specified by:
close in interface java.nio.channels.Channel
Throws:
java.io.IOException - May be raised by close operation on wrapped byte channel

isOpen

public boolean isOpen()
Is the channel open ?

Specified by:
isOpen in interface java.nio.channels.Channel
Returns:
true if the channel is still open

read

public int read(java.nio.ByteBuffer byteBuffer)
         throws java.io.IOException
Fill the given buffer with some bytes and return the number of bytes added in the buffer.
This method may return immediately with nothing added in the buffer. This method must be use exactly in the same way of ByteChannel read operation, so be careful with buffer position, limit, ... Check corresponding javadoc.

Specified by:
read in interface java.nio.channels.ReadableByteChannel
Parameters:
byteBuffer - The buffer that will received read bytes
Returns:
The number of bytes read
Throws:
java.io.IOException - May be raised by ByteChannel read operation

write

public int write(java.nio.ByteBuffer byteBuffer)
          throws java.io.IOException
Write remaining bytes of the given byte buffer. This method may return immediately with nothing written. This method must be use exactly in the same way of ByteChannel write operation, so be careful with buffer position, limit, ... Check corresponding javadoc.

Specified by:
write in interface java.nio.channels.WritableByteChannel
Parameters:
byteBuffer - buffer with remaining bytes to write
Returns:
The number of bytes written
Throws:
java.io.IOException - May be raised by ByteChannel write operation

Java HTTP Proxy Library Project Page
SourceForge.net Logo