View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     https://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  
21  package org.apache.directory.api.asn1.util;
22  
23  import java.nio.ByteBuffer;
24  
25  /**
26   * A buffer used to store an encoding PDU. It's auto-extended, and
27   * filled by the end.
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   */
31  public class Asn1Buffer
32  {
33      /** The buffer default size */
34      private static final int DEFAULT_SIZE = 1024;
35  
36      /** The current position in the buffer */
37      private int pos = 0;
38  
39      /** A buffer to store the encoded PDU */
40      private byte[] buffer;
41      
42      
43      /**
44       * Creates a new Asn1Buffer instance
45       */
46      public Asn1Buffer()
47      {
48          buffer = new byte[DEFAULT_SIZE];
49      }
50  
51  
52      /**
53       * @return The current position in the buffer
54       */
55      public int getPos()
56      {
57          return pos;
58      }
59  
60  
61      /**
62       * Set the current position in the buffer
63       * 
64       * @param pos The position to move the buffer to
65       */
66      public void setPos( int pos )
67      {
68          this.pos = pos;
69      }
70  
71  
72      /**
73       * Store a byte at the current position in the buffer
74       *
75       * @param b The byte to store
76       */
77      public void put( byte b )
78      {
79          if ( pos == buffer.length )
80          {
81              // The buffer needs to be reallocated, its too small
82              extend( 1 );
83          }
84  
85          pos++;
86          buffer[buffer.length - pos] = b;
87      }
88  
89  
90      /**
91       * Store some bytes at the current position in the buffer
92       *
93       * @param bytes The bytes to store
94       */
95      public void put( byte[] bytes )
96      {
97          if ( pos + bytes.length > buffer.length )
98          {
99              // The buffer needs to be reallocated, its too small
100             extend( bytes.length );
101         }
102 
103 
104         pos += bytes.length;
105         System.arraycopy( bytes, 0, buffer, buffer.length - pos, bytes.length );
106     }
107 
108 
109     /**
110      * Extend the buffer
111      * 
112      * @param size The new buffer size
113      */
114     private void extend( int size )
115     {
116         // The buffer needs to be reallocated, it's too small
117         int newSize = ( ( size + buffer.length ) / DEFAULT_SIZE ) * DEFAULT_SIZE;
118 
119         if ( size % DEFAULT_SIZE != 0 )
120         {
121             newSize += DEFAULT_SIZE;
122         }
123 
124         byte[] newBuffer = new byte[newSize];
125         System.arraycopy( buffer, 0, newBuffer, newSize - buffer.length, buffer.length );
126 
127         buffer = newBuffer;
128     }
129 
130 
131     /**
132      * @return The stored encoded PDU.
133      */
134     public ByteBuffer getBytes()
135     {
136         ByteBuffer result = ByteBuffer.allocate( pos );
137 
138         result.put( buffer, buffer.length - pos, pos );
139         result.flip();
140 
141         return result;
142     }
143 
144 
145     /**
146      * @return The buffer size (ie the maximum number of bytes that can be
147      * added to this bffder before it gets extended).
148      */
149     public int getSize()
150     {
151         return buffer.length;
152     }
153 
154 
155     /**
156      * Clear the position, emptying the buffer. If it has grown, reallocate it
157      * to its initial size.
158      */
159     public void clear()
160     {
161         if ( buffer.length > DEFAULT_SIZE )
162         {
163             buffer = new byte[DEFAULT_SIZE];
164         }
165 
166         pos = 0;
167     }
168 
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public String toString()
175     {
176         return "[" + buffer.length + ", " + pos + "] '"
177             + Asn1StringUtils.dumpBytes( buffer, buffer.length - pos, pos ) + '\'';
178     }
179 }