Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/ber/stream/BERIntegral.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 is the base class for integral types such as Integer and
46 * Enumerated.
47 * <P>See CCITT X.209.
48 *
49 * <pre>
50 * ENCODING RULE:
51 * Primitive Definite length.
52 * tag = << depends on type >>
53 * length = (short or long form)
54 * one or more contents octets hold integral value
55 * value in two's complement
56 *
57 * Example 1: (Integer - zero)
58 * 02 01 00
59 * Example 2: (Enumerated - 1)
60 * 0A 01 01
61 * Example 3: (Integer - 300, short form)
62 * 02 02 01 2C
63 * Example 4: (Integer - 300, long form)
64 * 02 84 00 00 01 2C
65 * </pre>
66 *
67 * @version 1.0
68 */
69 public abstract class BERIntegral extends BERElement {
70 /**
71 * Value of element
72 */
73 private int m_value;
74
75 /**
76 * Constructs an integral type with a value.
77 * @param value integer value
78 */
79 public BERIntegral(int value) {
80 m_value = value;
81 }
82
83 /**
84 * Constructs an integral element from an input stream.
85 * @param stream source
86 * @param bytes_read array of 1 int; value incremented by
87 * number of bytes read from stream
88 * @exception IOException failed to construct
89 */
90 public BERIntegral(InputStream stream, int[] bytes_read) throws IOException {
91 int contents_length = super.readLengthOctets(stream, bytes_read);
92
93 /* Definite length content octets string. */
94 if (contents_length > 0) {
95 boolean negative = false;
96 int octet = stream.read();
97 bytes_read[0]++;
98 if ((octet & 0x80) > 0) /* left-most bit is 1. */
99 negative = true;
100
101 for (int i = 0; i < contents_length; i++) {
102 if (i > 0) {
103 octet = stream.read();
104 bytes_read[0]++;
105 }
106 if (negative)
107 m_value = (m_value<<8) + (octet^0xFF)&0xFF;
108 else
109 m_value = (m_value<<8) + (octet&0xFF);
110 }
111 if (negative) /* convert to 2's complement */
112 m_value = (m_value + 1) * -1;
113 }
114 }
115
116
117 /**
118 * Writes BER to stream.
119 * @param stream output stream
120 * @exception IOException on failure to write
121 */
122 public void write(OutputStream stream) throws IOException {
123 int binary_value = m_value;
124 int num_content_octets = 0;
125 int total_ber_octets;
126 int offset=1;
127 int lead;
128 int i;
129
130 byte[] content_octets = new byte[10]; /* should be plenty big */
131 byte[] net_octets = new byte[10]; /* pse need this in network order */
132
133 /* Get content octets - need to determine length */
134 if (m_value == 0) {
135 num_content_octets = 1;
136 content_octets[offset] = (byte)0x00;
137 net_octets[offset] = (byte)0x00;
138 } else {
139
140 if (m_value < 0) /* convert from 2's complement */
141 binary_value = (m_value * -1) -1;
142
143 do {
144 if (m_value < 0)
145 content_octets[num_content_octets+offset] =
146 (byte)((binary_value^0xFF)&0xFF);
147 else
148 content_octets[num_content_octets+offset] =
149 (byte)(binary_value&0xFF);
150
151 binary_value = (binary_value>>8);
152 num_content_octets++;
153 } while (binary_value > 0);
154
155 /* pse 1/16/96 we've just created a string that is in non-network order
156 flip it for net order */
157 for (i=0; i<num_content_octets; i++)
158 net_octets[offset+num_content_octets-1-i] = content_octets[offset+i];
159
160 /* pse 1/16/96 if we have the value encoded and the leading encoding bit is set
161 then stuff in a leading zero byte */
162 lead = net_octets[offset];
163
164 if ((m_value > 0) && ((lead & 0x80) > 0)) {
165 offset = 0;
166 net_octets[offset] = (byte)0x00;
167 num_content_octets++;
168 }
169
170 }
171 stream.write(getType());
172 sendDefiniteLength(stream, num_content_octets);
173 stream.write(net_octets,offset,num_content_octets); /* contents */
174 }
175
176 /**
177 * Gets the integral value.
178 * @return element value.
179 */
180 public int getValue() {
181 return m_value;
182 }
183
184 /**
185 * Gets the element type.
186 * @return element type.
187 */
188 public abstract int getType();
189
190 /**
191 * Gets the string representation.
192 * @return string representation of tag.
193 */
194 public abstract String toString();
195 }
196