View Javadoc

1   /*
2    * @(#) $Id: Asn1CodecDecoder.java 161723 2005-04-18 04:10:12Z trustin $
3    */
4   package org.apache.asn1.codec.mina;
5   
6   import org.apache.asn1.codec.DecoderException;
7   import org.apache.asn1.codec.stateful.DecoderCallback;
8   import org.apache.asn1.codec.stateful.StatefulDecoder;
9   import org.apache.mina.common.ByteBuffer;
10  import org.apache.mina.protocol.ProtocolDecoder;
11  import org.apache.mina.protocol.ProtocolDecoderOutput;
12  import org.apache.mina.protocol.ProtocolSession;
13  import org.apache.mina.protocol.ProtocolViolationException;
14  
15  /***
16   * Adapts {@link StatefulDecoder} to MINA <tt>ProtocolDecoder</tt>
17   * 
18   * @author Trustin Lee (trustin@apache.org)
19   * @version $Rev: 161723 $, $Date: 2005-04-18 06:10:12 +0200 (Mon, 18 Apr 2005) $, 
20   */
21  public class Asn1CodecDecoder implements ProtocolDecoder
22  {
23  
24      private final StatefulDecoder decoder;
25  
26      private final DecoderCallbackImpl callback = new DecoderCallbackImpl();
27  
28      public Asn1CodecDecoder( StatefulDecoder decoder )
29      {
30          decoder.setCallback( callback );
31          this.decoder = decoder;
32      }
33  
34      public void decode( ProtocolSession session, ByteBuffer in,
35                         ProtocolDecoderOutput out )
36              throws ProtocolViolationException
37      {
38          callback.decOut = out;
39          try
40          {
41              decoder.decode( in.buf() );
42          }
43          catch( DecoderException e )
44          {
45              throw new ProtocolViolationException( "Failed to decode.", e );
46          }
47      }
48  
49      private class DecoderCallbackImpl implements DecoderCallback
50      {
51          private ProtocolDecoderOutput decOut;
52  
53          public void decodeOccurred( StatefulDecoder decoder, Object decoded )
54          {
55              decOut.write( decoded );
56          }
57      }
58  }