| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class BERObjectId extends BERElement { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private int[] m_value = null; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 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 |
|
| 70 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 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 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public void write(OutputStream stream) throws IOException { |
| 124 |
stream.write(BERElement.OBJECTID); |
| 125 |
|
| 126 |
ByteArrayOutputStream contents_stream = new ByteArrayOutputStream(); |
| 127 |
|
| 128 |
|
| 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 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 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 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
private void writeSubIdentifier(OutputStream stream, int value) |
| 163 |
throws IOException { |
| 164 |
ByteArrayOutputStream sub_id_stream = new ByteArrayOutputStream(); |
| 165 |
|
| 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 |
|
| 173 |
stream.write(sub_id_buffer[i] | 0x80); |
| 174 |
} |
| 175 |
stream.write(sub_id_buffer[0]); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public int[] getValue() { |
| 183 |
return m_value; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public int getType() { |
| 191 |
return BERElement.OBJECTID; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 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 |
} |