1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.asn1new.primitives;
18
19 import java.io.Serializable;
20
21 import org.apache.asn1new.util.StringUtils;
22
23
24 /***
25 * Implement the Octet String primitive type.
26 *
27 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
28 */
29 public class OctetString implements Serializable
30 {
31 private static final long serialVersionUID = 1L;
32
33
34
35 /*** A null OctetString */
36 public static final OctetString EMPTY_STRING = new OctetString(0);
37
38 /*** A flag to mark the OctetString as Streamed (for OctetString larger than 1024 chars) */
39
40 public static final boolean STREAMED = true;
41
42 /*** The default length of an octet string */
43 private static final int DEFAULT_LENGTH = 1024;
44
45
46
47 /*** Tells if the OctetString is streamed or not */
48 private boolean isStreamed;
49
50 /*** The string is stored in a byte array */
51 private byte[] bytes;
52
53
54
55 /***
56 * Creates a OctetString with a specific length.
57 * @param length The OctetString length
58 */
59 public OctetString( int length )
60 {
61 if ( length > DEFAULT_LENGTH )
62 {
63
64 isStreamed = true;
65 bytes = new byte[length];
66 }
67 else
68 {
69 isStreamed = false;
70 bytes = new byte[length];
71 }
72 }
73
74 /***
75 * Creates a streamed OctetString with a specific length.
76 * Actually, it's just a simple OctetString.
77 * TODO Implement streaming.
78 * @param length The OctetString length
79 * @param isStreamed Tells if the OctetString must be streamed or not
80 */
81 public OctetString( int length, boolean isStreamed )
82 {
83 this.isStreamed = isStreamed;
84
85 if ( isStreamed )
86 {
87
88 bytes = new byte[length];
89 }
90 else
91 {
92 bytes = new byte[length];
93 }
94 }
95
96 /***
97 * Creates a OctetString with a value.
98 *
99 * @param bytes The value to store.
100 */
101 public OctetString( byte[] bytes )
102 {
103 if ( bytes.length > DEFAULT_LENGTH )
104 {
105 isStreamed = true;
106
107
108
109 this.bytes = new byte[bytes.length];
110
111
112
113 System.arraycopy( bytes, 0, this.bytes, 0, bytes.length );
114 }
115 else
116 {
117 isStreamed = false;
118
119 this.bytes = new byte[bytes.length];
120
121
122
123 System.arraycopy( bytes, 0, this.bytes, 0, bytes.length );
124 }
125 }
126
127
128
129 /***
130 * Set a new octetString in the OctetString. It will replace the old OctetString,
131 * and reset the current length with the new one.
132 *
133 * @param bytes The string to store
134 */
135 public void setData( byte[] bytes )
136 {
137 if ( bytes.length > DEFAULT_LENGTH )
138 {
139
140 if ( this.bytes.length < bytes.length )
141 {
142
143
144
145
146 this.bytes = new byte[bytes.length];
147 }
148
149 System.arraycopy( bytes, 0, this.bytes, 0, bytes.length );
150 }
151 else
152 {
153 System.arraycopy( bytes, 0, this.bytes, 0, bytes.length );
154 }
155 }
156
157 /***
158 * Get the data stored into the OctetString
159 * @return A byte array
160 */
161 public byte[] getValue()
162 {
163 return bytes;
164 }
165
166 /***
167 * Return a native String representation of the OctetString.
168 * @return A string representing the OctetString
169 */
170 public String toString()
171 {
172
173 StringBuffer sb = new StringBuffer();
174
175 for ( int i = 0; i < bytes.length; i++ )
176 {
177 if ( ( bytes[i] < 32 ) || ( bytes[i] > 127 ) )
178 {
179 sb.append( StringUtils.dumpByte( bytes[i] ) );
180 }
181 else
182 {
183 sb.append( (char)bytes[i] );
184 }
185 }
186
187 return sb.toString();
188 }
189
190 /***
191 * Tells if the OctetString is streamed or not
192 * @return <code>true</code> if the OctetString is streamed.
193 */
194 public boolean isStreamed()
195 {
196 return isStreamed;
197 }
198
199 /***
200 * @return Returns the length.
201 */
202 public int getNbBytes()
203 {
204 return bytes.length;
205 }
206 }