Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/ber/stream/BERObjectId.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.ByteArrayOutputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.io.OutputStream;
44 import java.util.StringTokenizer;
45 import java.util.Vector;
46
47 /**
48 * This class is for the Object ID object.
49 * <P>See CCITT X.209.
50 *
51 * @version 1.0
52 */
53 public class BERObjectId extends BERElement {
54 /**
55 * Values of each component of the OID
56 */
57 private int[] m_value = null;
58
59 /**
60 * Constructs an object ID element from an array of values.
61 * @param value object ID value as array of components
62 */
63 public BERObjectId(int[] value) {
64 m_value = new int[value.length];
65 System.arraycopy(value,0,m_value,0,value.length);
66 }
67
68 /**
69 * Constructs an object id element from a string.
70 * @param value object id value in format "2.100.3"
71 */
72 public BERObjectId(String value) {
73 StringTokenizer tokenizer = new StringTokenizer(value, ".");
74 m_value = new int[tokenizer.countTokens()];
75 for (int i=0; i<m_value.length; i++)
76 m_value[i] = Integer.parseInt(tokenizer.nextToken());
77 }
78
79 /**
80 * Constructs an object id element from an input stream.
81 * @param stream source
82 * @param bytes_read array of 1 int; value incremented by
83 * number of bytes read from stream
84 * @exception IOException failed to construct
85 */
86 public BERObjectId(InputStream stream, int[] bytes_read) throws IOException {
87 int contents_length = super.readLengthOctets(stream, bytes_read);
88
89 bytes_read[0] += contents_length;
90 int[] contents_read = new int[1];
91
92 Vector<Integer> oid = new Vector<>(10);
93 contents_read[0] = 0;
94 int sub_id = readSubIdentifier(stream, contents_read);
95
96 contents_length -= contents_read[0];
97 if (sub_id < 40)
98 oid.addElement(0);
99 else if (sub_id < 80)
100 oid.addElement(1);
101 else
102 oid.addElement(2);
103 oid.addElement(sub_id - (oid.elementAt(oid.size()-1).intValue() * 40));
104
105 while (contents_length > 0) {
106 contents_read[0] = 0;
107 sub_id = readSubIdentifier(stream, contents_read);
108
109 contents_length -= contents_read[0];
110 oid.addElement(sub_id);
111 }
112 m_value = new int[oid.size()];
113 for (int i = 0; i<oid.size(); i++)
114 m_value[i] = oid.elementAt(i).intValue();
115 }
116
117 /**
118 * Sends the BER encoding directly to a stream. Note that OID must
119 * have >= 2 identifier components (values).
120 * @param stream output stream
121 * @exception IOException failed to write
122 */
123 public void write(OutputStream stream) throws IOException {
124 stream.write(BERElement.OBJECTID);
125
126 ByteArrayOutputStream contents_stream = new ByteArrayOutputStream();
127
128 /* first subidentifier packs two component values */
129 writeSubIdentifier(contents_stream,m_value[0]*40 + m_value[1]);
130
131 for (int i = 2; i < m_value.length; i++) {
132 writeSubIdentifier(contents_stream,m_value[i]);
133 }
134 byte[] contents_buffer = contents_stream.toByteArray();
135 sendDefiniteLength(stream, contents_buffer.length);
136 stream.write(contents_buffer);
137 }
138
139 /**
140 * Reads a sub identifier from stream.
141 * @param stream input stream
142 * @param bytes_read array of 1 int; value incremented by
143 * number of bytes read from stream
144 */
145 private int readSubIdentifier(InputStream stream, int[] bytes_read)
146 throws IOException {
147 int octet;
148 int sub_id = 0;
149 do {
150 octet = stream.read();
151 bytes_read[0]++;
152 sub_id = (sub_id << 7) | (octet & 0x7F);
153 } while ((octet & 0x80) > 0);
154 return sub_id;
155 }
156
157 /**
158 * Sends the BER encoding of a sub identifier directly to stream.
159 * @param stream output stream
160 * @param value sub-identifier value
161 */
162 private void writeSubIdentifier(OutputStream stream, int value)
163 throws IOException {
164 ByteArrayOutputStream sub_id_stream = new ByteArrayOutputStream();
165 /* gather octets in reverse order */
166 while (value > 0) {
167 sub_id_stream.write(value & 0x7F);
168 value = value >> 7;
169 }
170 byte[] sub_id_buffer = sub_id_stream.toByteArray();
171 for (int i=sub_id_buffer.length-1; i>0; i--) {
172 /* all but last octet have bit 8 = 1 */
173 stream.write(sub_id_buffer[i] | 0x80);
174 }
175 stream.write(sub_id_buffer[0]); /* last octet has bit 8 = 0 */
176 }
177
178 /**
179 * Gets the element value.
180 * @return element value.
181 */
182 public int[] getValue() {
183 return m_value;
184 }
185
186 /**
187 * Gets the element type.
188 * @return element type.
189 */
190 public int getType() {
191 return BERElement.OBJECTID;
192 }
193
194 /**
195 * Gets the string representation.
196 * @return string representation of element.
197 */
198 public String toString() {
199 if (m_value == null)
200 return "ObjectIdentifier (null)";
201 String oid = "";
202 for (int i = 0; i < m_value.length; i++) {
203 if (i != 0)
204 oid = oid + " ";
205 oid = oid + m_value[i];
206 }
207 return "ObjectIdentifier {" + oid + "}";
208 }
209 }