Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/ber/stream/BEROctetString.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap.ber.stream;
39
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.io.OutputStream;
43
44 /**
45 * This class is for the OctetString type.
46 * <P>See CCITT X.209.
47 *
48 * <pre>
49 * ENCODING RULE:
50 * Primitive Definite length.
51 * tag = 0x04
52 * length = (short or long form)
53 * one or more contents octets
54 * </pre>
55 *
56 * @version 1.0
57 */
58 public class BEROctetString extends BERElement {
59
60 /**
61 * Raw value of element
62 */
63 private byte[] m_value = null;
64
65 /**
66 * Constructs an octet string element containing a copy of
67 * the contents of buffer.
68 * @param buffer a UCS-2 String
69 */
70 public BEROctetString(String buffer) {
71 if (buffer == null)
72 return;
73
74 try {
75 m_value = buffer.getBytes( "UTF8" );
76 } catch (Throwable xxx){};
77 }
78
79 /**
80 * Constructs an octet string element containing a reference to
81 * buffer.
82 * @param buffer a byte array, which must be in UTF-8 format if
83 * it is string data
84 */
85 public BEROctetString(byte[] buffer) {
86 m_value = buffer;
87 }
88
89 /**
90 * Constructs an octet string element containing a
91 * subset of buffer.
92 * @param buffer buffer containing 'octets'
93 * @param start start of buffer range to copy
94 * @param end end of buffer range to copy
95 */
96 public BEROctetString(byte[] buffer, int start, int end) {
97 m_value = new byte[end - start];
98
99 for (int i = 0; i < end - start; i++)
100 m_value[i] = buffer[start + i];
101 }
102
103 /**
104 * Constructs an octet string element from an input stream
105 * (for constructed encoding)
106 * @param decoder a decode that understands the specific tags
107 * @param stream source
108 * @param bytes_read array of 1 int, incremented by number of bytes read
109 * @exception IOException failed to construct
110 */
111 public BEROctetString(BERTagDecoder decoder, InputStream stream,
112 int[] bytes_read) throws IOException {
113 int octet;
114 int contents_length = super.readLengthOctets(stream, bytes_read);
115 int[] component_length = new int[1];
116 BERElement element = null;
117
118 if (contents_length == -1) {
119 /* Constructed - indefinite length content octets. */
120 do {
121 component_length[0] = 0;
122 element = getElement(decoder,stream,component_length);
123 if (element != null) {
124 /* element is an octetstring - add it to the existing buffer */
125 BEROctetString octet_element = (BEROctetString)element;
126 byte[] octet_buffer = octet_element.getValue();
127 if (m_value == null) {
128 m_value = new byte[octet_buffer.length];
129 System.arraycopy(octet_buffer,0,
130 m_value,0,octet_buffer.length);
131 } else {
132 byte[] new_buffer = new byte[m_value.length +
133 octet_buffer.length];
134 System.arraycopy(m_value,0,new_buffer,0,m_value.length);
135 System.arraycopy(octet_buffer,0,
136 new_buffer,m_value.length,
137 octet_buffer.length);
138 m_value = new_buffer;
139 }
140 }
141 } while (element != null);
142 } else {
143 /* Definite length content octets string. */
144 bytes_read[0] += contents_length;
145 m_value = new byte[contents_length];
146 stream.read(m_value, 0, contents_length);
147 }
148 }
149
150 /**
151 * Constructs an octet string element from an input stream
152 * (for primitive encoding)
153 * @param stream source
154 * @param bytes_read array of 1 int, incremented by number of bytes read
155 * @exception IOException failed to construct
156 */
157 public BEROctetString(InputStream stream, int[] bytes_read)
158 throws IOException {
159 int contents_length = super.readLengthOctets(stream, bytes_read);
160
161 /* Definite length content octets string. */
162 if (contents_length > 0) {
163 m_value = new byte[contents_length];
164 for (int i = 0; i < contents_length; i++) {
165 m_value[i] = (byte) stream.read();
166 }
167 bytes_read[0] += contents_length;
168 }
169 }
170
171 /**
172 * Writes BER to stream
173 * @return number of bytes written to stream.
174 * @exception IOException failed to write
175 */
176 public void write(OutputStream stream) throws IOException {
177 stream.write((byte)BERElement.OCTETSTRING); /* OCTETSTRING tag */
178 if (m_value == null) {
179 sendDefiniteLength(stream, 0);
180 } else {
181 sendDefiniteLength(stream, m_value.length); /* length */
182 stream.write(m_value,0,m_value.length); /* contents */
183 }
184 }
185
186 /**
187 * Gets the element value.
188 * @param element value
189 */
190 public byte[] getValue() {
191 return m_value;
192 }
193
194 /**
195 * Gets the element type.
196 * @param element type
197 */
198 public int getType() {
199 return BERElement.OCTETSTRING;
200 }
201
202 /**
203 * Gets the string representation.
204 * NOTE: currently prints out values in decimal form.
205 * @return string representation of tag.
206 */
207 public String toString() {
208 if (m_value == null)
209 return "OctetString (null)";
210 String octets = "";
211 for (int i = 0; i < m_value.length; i++) {
212 if (i != 0)
213 octets = octets + " ";
214 octets = octets + byteToHexString(m_value[i]);
215 }
216 return "OctetString {" + octets + "}";
217 }
218 }