View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.asn1.ber ;
18  
19  
20  import java.nio.ByteBuffer;
21  
22  import org.apache.asn1.codec.DecoderException;
23  import org.apache.asn1.codec.stateful.AbstractStatefulDecoder;
24  
25  
26  /***
27   * A BER TLV Length component decoder.  This decoder delivers a Length instance
28   * to its callback.  For efficiency the same Length object is reused.  Callback
29   * implementations should not copy the handle to the Length object delivered but
30   * should copy the data if they need it over the long term.
31   *
32   * @author <a href="mailto:dev@directory.apache.org">
33   * Apache Directory Project</a>
34   * @version $Rev: 157644 $
35   */
36  public class LengthDecoder extends AbstractStatefulDecoder
37  {
38      /*** the Length component decoded from a BER TLV Tuple */
39      private final Length length = new Length() ;
40      
41      
42      /* (non-Javadoc)
43       * @see org.apache.asn1.codec.stateful.StatefulDecoder#decode(
44       * java.lang.Object)
45       */
46      public void decode( Object encoded ) throws DecoderException
47      {
48          ByteBuffer buf = ( ByteBuffer ) encoded ;
49          
50          while ( buf.hasRemaining() )
51          {
52              byte octet = buf.get() ;
53              length.add( octet ) ;
54              
55              if ( length.isFixated() )
56              {
57                  decodeOccurred( length ) ;
58                  length.clear() ;
59                  return ;
60              }
61          }
62      }
63  }