1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.asn1.ber ;
18
19
20 import java.nio.BufferOverflowException;
21
22 import org.apache.commons.lang.ArrayUtils;
23
24
25 /***
26 * Collects up to 4 tag octets.
27 *
28 * @author <a href="mailto:dev@directory.apache.org">
29 * Apache Directory Project</a>
30 * @version $Rev: 157644 $
31 */
32 public class TagOctetCollector
33 {
34 /*** the int used to store the tag octets */
35 private int intValue = 0 ;
36 /*** the number of octets currently stored */
37 private int _size = 0 ;
38
39
40 /***
41 * Puts an octet into this collector.
42 *
43 * @param octet the octet to put into the collector.
44 */
45 public void put( byte octet )
46 {
47 switch( _size )
48 {
49 case(0):
50 intValue = octet << 24 ;
51 _size = 1 ;
52 break ;
53 case(1):
54 intValue |= ( octet << 16 ) & 0x00FF0000 ;
55 _size = 2 ;
56 break ;
57 case(2):
58 intValue |= ( octet << 8 ) & 0x0000FF00 ;
59 _size = 3 ;
60 break ;
61 case(3):
62 intValue |= octet ;
63 _size = 4 ;
64 break ;
65 default:
66 throw new BufferOverflowException() ;
67 }
68 }
69
70
71 /***
72 * Clears all the tag octets resetting the tag and size to zero.
73 */
74 public void clear()
75 {
76 intValue = 0 ;
77 _size = 0 ;
78 }
79
80
81 /***
82 * Gets the number of octets stored by this TagOctetCollector
83 *
84 * @return
85 */
86 public int size()
87 {
88 return _size ;
89 }
90
91
92 /***
93 * Gets a unique integer value representing the tag octets.
94 *
95 * @return the integer value of the tag.
96 */
97 public int getIntValue()
98 {
99 return intValue ;
100 }
101
102
103 /***
104 * Gets the 4 octets for the tag.
105 *
106 * @return
107 */
108 public byte[] toArray()
109 {
110 byte[] octets = new byte[_size] ;
111
112 switch( _size )
113 {
114 case(0):
115 octets = ArrayUtils.EMPTY_BYTE_ARRAY ;
116 break ;
117 case(1):
118 octets[0] = ( byte ) ( ( intValue & 0xff000000 ) >> 24 ) ;
119 break ;
120 case(2):
121 octets[0] = ( byte ) ( ( intValue & 0xff000000 ) >> 24 ) ;
122 octets[1] = ( byte ) ( ( intValue & 0x00ff0000 ) >> 16 ) ;
123 break ;
124 case(3):
125 octets[0] = ( byte ) ( ( intValue & 0xff000000 ) >> 24 ) ;
126 octets[1] = ( byte ) ( ( intValue & 0x00ff0000 ) >> 16 ) ;
127 octets[2] = ( byte ) ( ( intValue & 0x0000ff00 ) >> 8 ) ;
128 break ;
129 case(4):
130 octets[0] = ( byte ) ( ( intValue & 0xff000000 ) >> 24 ) ;
131 octets[1] = ( byte ) ( ( intValue & 0x00ff0000 ) >> 16 ) ;
132 octets[2] = ( byte ) ( ( intValue & 0x0000ff00 ) >> 8 ) ;
133 octets[3] = ( byte ) ( intValue & 0x000000ff ) ;
134 break ;
135 default:
136 throw new IllegalArgumentException(
137 "Cannot support more than 4 octets" ) ;
138 }
139
140 return octets ;
141 }
142
143
144 /***
145 * Gets the byte at a specific index.
146 *
147 * @param index
148 * @return
149 * @throws IndexOutOfBoundsException
150 */
151 public byte get( int index )
152 {
153 byte octet ;
154
155 if ( index >= _size )
156 {
157 throw new IndexOutOfBoundsException( "accesing index " + index
158 + " with a size of " + _size ) ;
159 }
160
161 switch( index )
162 {
163 case(0):
164 octet = ( byte ) ( ( intValue & 0xff000000 ) >> 24 ) ;
165 break ;
166 case(1):
167 octet = ( byte ) ( ( intValue & 0x00ff0000 ) >> 16 ) ;
168 break ;
169 case(2):
170 octet = ( byte ) ( ( intValue & 0x0000ff00 ) >> 8 ) ;
171 break ;
172 case(3):
173 octet = ( byte ) ( intValue & 0x000000ff ) ;
174 break ;
175 default:
176 throw new IllegalArgumentException(
177 "Cannot support more than 4 octets" ) ;
178 }
179
180 return octet ;
181 }
182 }