1 /*
2 * Copyright 2005 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.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 //~ Static fields/initializers -----------------------------------------------------------------
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 // TODO implement the streaming...
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 //~ Instance fields ----------------------------------------------------------------------------
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 //~ Constructors -------------------------------------------------------------------------------
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 // TODO : implement the streaming
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 // TODO : implement the streaming
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 // It will be a streamed OctetString.
108 // TODO : implement the streaming
109 this.bytes = new byte[bytes.length];
110
111 // We have to copy the data, because the parameter
112 // is not a copy.
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 // We have to copy the data, because the parameter
122 // is not a copy.
123 System.arraycopy( bytes, 0, this.bytes, 0, bytes.length );
124 }
125 }
126
127 //~ Methods ------------------------------------------------------------------------------------
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 // The current size is too small.
143 // We have to allocate more space
144 // It will be a streamed OctetString.
145 // TODO : implement the streaming
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 }