| 1 |
/*
|
| 2 |
* @(#) $Id$
|
| 3 |
*/
|
| 4 |
package org.apache.asn1.codec.mina;
|
| 5 |
|
| 6 |
import java.util.Collection;
|
| 7 |
import java.util.Enumeration;
|
| 8 |
import java.util.Iterator;
|
| 9 |
|
| 10 |
import org.apache.asn1.codec.EncoderException;
|
| 11 |
import org.apache.asn1.codec.stateful.EncoderCallback;
|
| 12 |
import org.apache.asn1.codec.stateful.StatefulEncoder;
|
| 13 |
import org.apache.mina.common.ByteBuffer;
|
| 14 |
import org.apache.mina.protocol.ProtocolEncoder;
|
| 15 |
import org.apache.mina.protocol.ProtocolEncoderOutput;
|
| 16 |
import org.apache.mina.protocol.ProtocolSession;
|
| 17 |
import org.apache.mina.protocol.ProtocolViolationException;
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Adapts {@link StatefulEncoder} to MINA <tt>ProtocolEncoder</tt>
|
| 21 |
*
|
| 22 |
* @author Trustin Lee (trustin@apache.org)
|
| 23 |
* @version $Rev$, $Date$,
|
| 24 |
*/
|
| 25 |
public class Asn1CodecEncoder implements ProtocolEncoder
|
| 26 |
{
|
| 27 |
private final StatefulEncoder encoder;
|
| 28 |
|
| 29 |
private final EncoderCallbackImpl callback = new EncoderCallbackImpl();
|
| 30 |
|
| 31 |
public Asn1CodecEncoder( StatefulEncoder encoder )
|
| 32 |
{
|
| 33 |
encoder.setCallback( callback );
|
| 34 |
this.encoder = encoder;
|
| 35 |
}
|
| 36 |
|
| 37 |
public void encode( ProtocolSession session, Object message,
|
| 38 |
ProtocolEncoderOutput out )
|
| 39 |
throws ProtocolViolationException
|
| 40 |
{
|
| 41 |
callback.encOut = out;
|
| 42 |
try
|
| 43 |
{
|
| 44 |
encoder.encode( message );
|
| 45 |
}
|
| 46 |
catch( EncoderException e )
|
| 47 |
{
|
| 48 |
throw new ProtocolViolationException( "Encoding failed.", e );
|
| 49 |
}
|
| 50 |
}
|
| 51 |
|
| 52 |
private class EncoderCallbackImpl implements EncoderCallback
|
| 53 |
{
|
| 54 |
private ProtocolEncoderOutput encOut;
|
| 55 |
|
| 56 |
public void encodeOccurred( StatefulEncoder codec, Object encoded )
|
| 57 |
{
|
| 58 |
if( encoded instanceof java.nio.ByteBuffer )
|
| 59 |
{
|
| 60 |
java.nio.ByteBuffer buf = ( java.nio.ByteBuffer ) encoded;
|
| 61 |
ByteBuffer wrappedBuf = ByteBuffer.wrap( buf );
|
| 62 |
wrappedBuf.acquire(); // acquire once more to prvent leak
|
| 63 |
encOut.write( wrappedBuf );
|
| 64 |
}
|
| 65 |
else if( encoded instanceof Object[] )
|
| 66 |
{
|
| 67 |
Object[] bufArray = ( Object[] ) encoded;
|
| 68 |
for( int i = 0; i < bufArray.length; i ++ )
|
| 69 |
{
|
| 70 |
this.encodeOccurred( codec, bufArray[ i ] );
|
| 71 |
}
|
| 72 |
|
| 73 |
encOut.mergeAll();
|
| 74 |
}
|
| 75 |
else if( encoded instanceof Iterator )
|
| 76 |
{
|
| 77 |
Iterator it = ( Iterator ) encoded;
|
| 78 |
while( it.hasNext() )
|
| 79 |
{
|
| 80 |
this.encodeOccurred( codec, it.next() );
|
| 81 |
}
|
| 82 |
|
| 83 |
encOut.mergeAll();
|
| 84 |
}
|
| 85 |
else if( encoded instanceof Collection )
|
| 86 |
{
|
| 87 |
Iterator it = ( ( Collection ) encoded ).iterator();
|
| 88 |
while( it.hasNext() )
|
| 89 |
{
|
| 90 |
this.encodeOccurred( codec, it.next() );
|
| 91 |
}
|
| 92 |
|
| 93 |
encOut.mergeAll();
|
| 94 |
}
|
| 95 |
else if( encoded instanceof Enumeration )
|
| 96 |
{
|
| 97 |
Enumeration e = ( Enumeration ) encoded;
|
| 98 |
while( e.hasMoreElements() )
|
| 99 |
{
|
| 100 |
this.encodeOccurred( codec, e.nextElement() );
|
| 101 |
}
|
| 102 |
|
| 103 |
encOut.mergeAll();
|
| 104 |
}
|
| 105 |
else
|
| 106 |
{
|
| 107 |
throw new IllegalArgumentException(
|
| 108 |
"Encoded result is not a ByteBuffer: " +
|
| 109 |
encoded.getClass() );
|
| 110 |
}
|
| 111 |
}
|
| 112 |
}
|
| 113 |
}
|