slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 1 | /** |
| 2 | * This class file was automatically generated by jASN1 v1.7.2-SNAPSHOT (http://www.openmuc.org) |
| 3 | */ |
| 4 | |
| 5 | package org.onosproject.xran.codecs.ber.types; |
| 6 | |
| 7 | import com.fasterxml.jackson.annotation.JsonIgnore; |
| 8 | import com.fasterxml.jackson.annotation.JsonValue; |
| 9 | import org.onosproject.xran.codecs.ber.BerByteArrayOutputStream; |
| 10 | import org.onosproject.xran.codecs.ber.BerLength; |
| 11 | import org.onosproject.xran.codecs.ber.BerTag; |
| 12 | import org.onosproject.xran.codecs.ber.types.string.BerObjectDescriptor; |
| 13 | |
| 14 | import java.io.IOException; |
| 15 | import java.io.InputStream; |
| 16 | import java.io.Serializable; |
| 17 | |
| 18 | public class BerEmbeddedPdv implements Serializable { |
| 19 | |
| 20 | public static final BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.CONSTRUCTED, 11); |
| 21 | private static final long serialVersionUID = 1L; |
| 22 | @JsonIgnore public byte[] code = null; |
| 23 | private Identification identification = null; |
| 24 | private BerObjectDescriptor dataValueDescriptor = null; |
| 25 | private BerOctetString dataValue = null; |
| 26 | public BerEmbeddedPdv() { |
| 27 | } |
| 28 | |
| 29 | public BerEmbeddedPdv(byte[] code) { |
| 30 | this.code = code; |
| 31 | } |
| 32 | |
| 33 | public Identification getIdentification() { |
| 34 | return identification; |
| 35 | } |
| 36 | |
| 37 | public void setIdentification(Identification identification) { |
| 38 | this.identification = identification; |
| 39 | } |
| 40 | |
| 41 | public BerObjectDescriptor getDataValueDescriptor() { |
| 42 | return dataValueDescriptor; |
| 43 | } |
| 44 | |
| 45 | public void setDataValueDescriptor(BerObjectDescriptor dataValueDescriptor) { |
| 46 | this.dataValueDescriptor = dataValueDescriptor; |
| 47 | } |
| 48 | |
| 49 | public BerOctetString getDataValue() { |
| 50 | return dataValue; |
| 51 | } |
| 52 | |
| 53 | public void setDataValue(BerOctetString dataValue) { |
| 54 | this.dataValue = dataValue; |
| 55 | } |
| 56 | |
| 57 | public int encode(BerByteArrayOutputStream os) throws IOException { |
| 58 | return encode(os, true); |
| 59 | } |
| 60 | |
| 61 | public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException { |
| 62 | |
| 63 | if (code != null) { |
| 64 | for (int i = code.length - 1; i >= 0; i--) { |
| 65 | os.write(code[i]); |
| 66 | } |
| 67 | if (withTag) { |
| 68 | return tag.encode(os) + code.length; |
| 69 | } |
| 70 | return code.length; |
| 71 | } |
| 72 | |
| 73 | int codeLength = 0; |
| 74 | int sublength; |
| 75 | |
| 76 | codeLength += dataValue.encode(os, false); |
| 77 | // write tag: CONTEXT_CLASS, PRIMITIVE, 2 |
| 78 | os.write(0x82); |
| 79 | codeLength += 1; |
| 80 | |
| 81 | if (dataValueDescriptor != null) { |
| 82 | codeLength += dataValueDescriptor.encode(os, false); |
| 83 | // write tag: CONTEXT_CLASS, PRIMITIVE, 1 |
| 84 | os.write(0x81); |
| 85 | codeLength += 1; |
| 86 | } |
| 87 | |
| 88 | sublength = identification.encode(os); |
| 89 | codeLength += sublength; |
| 90 | codeLength += BerLength.encodeLength(os, sublength); |
| 91 | // write tag: CONTEXT_CLASS, CONSTRUCTED, 0 |
| 92 | os.write(0xA0); |
| 93 | codeLength += 1; |
| 94 | |
| 95 | codeLength += BerLength.encodeLength(os, codeLength); |
| 96 | |
| 97 | if (withTag) { |
| 98 | codeLength += tag.encode(os); |
| 99 | } |
| 100 | |
| 101 | return codeLength; |
| 102 | |
| 103 | } |
| 104 | |
| 105 | public int decode(InputStream is) throws IOException { |
| 106 | return decode(is, true); |
| 107 | } |
| 108 | |
| 109 | public int decode(InputStream is, boolean withTag) throws IOException { |
| 110 | int codeLength = 0; |
| 111 | int subCodeLength = 0; |
| 112 | BerTag berTag = new BerTag(); |
| 113 | |
| 114 | if (withTag) { |
| 115 | codeLength += tag.decodeAndCheck(is); |
| 116 | } |
| 117 | |
| 118 | BerLength length = new BerLength(); |
| 119 | codeLength += length.decode(is); |
| 120 | |
| 121 | int totalLength = length.val; |
| 122 | codeLength += totalLength; |
| 123 | |
| 124 | subCodeLength += berTag.decode(is); |
| 125 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 0)) { |
| 126 | subCodeLength += length.decode(is); |
| 127 | identification = new Identification(); |
| 128 | subCodeLength += identification.decode(is, null); |
| 129 | subCodeLength += berTag.decode(is); |
| 130 | } else { |
| 131 | throw new IOException("Tag does not match the mandatory sequence element tag."); |
| 132 | } |
| 133 | |
| 134 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) { |
| 135 | dataValueDescriptor = new BerObjectDescriptor(); |
| 136 | subCodeLength += dataValueDescriptor.decode(is, false); |
| 137 | subCodeLength += berTag.decode(is); |
| 138 | } |
| 139 | |
| 140 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 2)) { |
| 141 | dataValue = new BerOctetString(); |
| 142 | subCodeLength += dataValue.decode(is, false); |
| 143 | if (subCodeLength == totalLength) { |
| 144 | return codeLength; |
| 145 | } |
| 146 | } |
| 147 | throw new IOException("Unexpected end of sequence, length tag: " + totalLength + ", actual sequence length: " |
| 148 | + subCodeLength); |
| 149 | |
| 150 | } |
| 151 | |
| 152 | public void encodeAndSave(int encodingSizeGuess) throws IOException { |
| 153 | BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess); |
| 154 | encode(os, false); |
| 155 | code = os.getArray(); |
| 156 | } |
| 157 | |
| 158 | @JsonValue |
| 159 | @Override |
| 160 | public String toString() { |
| 161 | StringBuilder sb = new StringBuilder(); |
| 162 | appendAsString(sb, 0); |
| 163 | return sb.toString(); |
| 164 | } |
| 165 | |
| 166 | public void appendAsString(StringBuilder sb, int indentLevel) { |
| 167 | |
| 168 | sb.append("{"); |
| 169 | sb.append("\n"); |
| 170 | for (int i = 0; i < indentLevel + 1; i++) { |
| 171 | sb.append("\t"); |
| 172 | } |
| 173 | if (identification != null) { |
| 174 | sb.append("identification: "); |
| 175 | identification.appendAsString(sb, indentLevel + 1); |
| 176 | } else { |
| 177 | sb.append("identification: <empty-required-field>"); |
| 178 | } |
| 179 | |
| 180 | if (dataValueDescriptor != null) { |
| 181 | sb.append(",\n"); |
| 182 | for (int i = 0; i < indentLevel + 1; i++) { |
| 183 | sb.append("\t"); |
| 184 | } |
| 185 | sb.append("dataValueDescriptor: ").append(dataValueDescriptor); |
| 186 | } |
| 187 | |
| 188 | sb.append(",\n"); |
| 189 | for (int i = 0; i < indentLevel + 1; i++) { |
| 190 | sb.append("\t"); |
| 191 | } |
| 192 | if (dataValue != null) { |
| 193 | sb.append("dataValue: ").append(dataValue); |
| 194 | } else { |
| 195 | sb.append("dataValue: <empty-required-field>"); |
| 196 | } |
| 197 | |
| 198 | sb.append("\n"); |
| 199 | for (int i = 0; i < indentLevel; i++) { |
| 200 | sb.append("\t"); |
| 201 | } |
| 202 | sb.append("}"); |
| 203 | } |
| 204 | |
| 205 | public static class Identification implements Serializable { |
| 206 | |
| 207 | private static final long serialVersionUID = 1L; |
| 208 | |
| 209 | @JsonIgnore |
| 210 | public byte[] code = null; |
| 211 | private Syntaxes syntaxes = null; |
| 212 | private BerObjectIdentifier syntax = null; |
| 213 | private BerInteger presentationContextId = null; |
| 214 | private ContextNegotiation contextNegotiation = null; |
| 215 | private BerObjectIdentifier transferSyntax = null; |
| 216 | private BerNull fixed = null; |
| 217 | public Identification() { |
| 218 | } |
| 219 | public Identification(byte[] code) { |
| 220 | this.code = code; |
| 221 | } |
| 222 | |
| 223 | public Syntaxes getSyntaxes() { |
| 224 | return syntaxes; |
| 225 | } |
| 226 | |
| 227 | public void setSyntaxes(Syntaxes syntaxes) { |
| 228 | this.syntaxes = syntaxes; |
| 229 | } |
| 230 | |
| 231 | public BerObjectIdentifier getSyntax() { |
| 232 | return syntax; |
| 233 | } |
| 234 | |
| 235 | public void setSyntax(BerObjectIdentifier syntax) { |
| 236 | this.syntax = syntax; |
| 237 | } |
| 238 | |
| 239 | public BerInteger getPresentationContextId() { |
| 240 | return presentationContextId; |
| 241 | } |
| 242 | |
| 243 | public void setPresentationContextId(BerInteger presentationContextId) { |
| 244 | this.presentationContextId = presentationContextId; |
| 245 | } |
| 246 | |
| 247 | public ContextNegotiation getContextNegotiation() { |
| 248 | return contextNegotiation; |
| 249 | } |
| 250 | |
| 251 | public void setContextNegotiation(ContextNegotiation contextNegotiation) { |
| 252 | this.contextNegotiation = contextNegotiation; |
| 253 | } |
| 254 | |
| 255 | public BerObjectIdentifier getTransferSyntax() { |
| 256 | return transferSyntax; |
| 257 | } |
| 258 | |
| 259 | public void setTransferSyntax(BerObjectIdentifier transferSyntax) { |
| 260 | this.transferSyntax = transferSyntax; |
| 261 | } |
| 262 | |
| 263 | public BerNull getFixed() { |
| 264 | return fixed; |
| 265 | } |
| 266 | |
| 267 | public void setFixed(BerNull fixed) { |
| 268 | this.fixed = fixed; |
| 269 | } |
| 270 | |
| 271 | public int encode(BerByteArrayOutputStream os) throws IOException { |
| 272 | |
| 273 | if (code != null) { |
| 274 | for (int i = code.length - 1; i >= 0; i--) { |
| 275 | os.write(code[i]); |
| 276 | } |
| 277 | return code.length; |
| 278 | } |
| 279 | |
| 280 | int codeLength = 0; |
| 281 | if (fixed != null) { |
| 282 | codeLength += fixed.encode(os, false); |
| 283 | // write tag: CONTEXT_CLASS, PRIMITIVE, 5 |
| 284 | os.write(0x85); |
| 285 | codeLength += 1; |
| 286 | return codeLength; |
| 287 | } |
| 288 | |
| 289 | if (transferSyntax != null) { |
| 290 | codeLength += transferSyntax.encode(os, false); |
| 291 | // write tag: CONTEXT_CLASS, PRIMITIVE, 4 |
| 292 | os.write(0x84); |
| 293 | codeLength += 1; |
| 294 | return codeLength; |
| 295 | } |
| 296 | |
| 297 | if (contextNegotiation != null) { |
| 298 | codeLength += contextNegotiation.encode(os, false); |
| 299 | // write tag: CONTEXT_CLASS, CONSTRUCTED, 3 |
| 300 | os.write(0xA3); |
| 301 | codeLength += 1; |
| 302 | return codeLength; |
| 303 | } |
| 304 | |
| 305 | if (presentationContextId != null) { |
| 306 | codeLength += presentationContextId.encode(os, false); |
| 307 | // write tag: CONTEXT_CLASS, PRIMITIVE, 2 |
| 308 | os.write(0x82); |
| 309 | codeLength += 1; |
| 310 | return codeLength; |
| 311 | } |
| 312 | |
| 313 | if (syntax != null) { |
| 314 | codeLength += syntax.encode(os, false); |
| 315 | // write tag: CONTEXT_CLASS, PRIMITIVE, 1 |
| 316 | os.write(0x81); |
| 317 | codeLength += 1; |
| 318 | return codeLength; |
| 319 | } |
| 320 | |
| 321 | if (syntaxes != null) { |
| 322 | codeLength += syntaxes.encode(os, false); |
| 323 | // write tag: CONTEXT_CLASS, CONSTRUCTED, 0 |
| 324 | os.write(0xA0); |
| 325 | codeLength += 1; |
| 326 | return codeLength; |
| 327 | } |
| 328 | |
| 329 | throw new IOException("Error encoding CHOICE: No element of CHOICE was selected."); |
| 330 | } |
| 331 | |
| 332 | public int decode(InputStream is) throws IOException { |
| 333 | return decode(is, null); |
| 334 | } |
| 335 | |
| 336 | public int decode(InputStream is, BerTag berTag) throws IOException { |
| 337 | |
| 338 | int codeLength = 0; |
| 339 | BerTag passedTag = berTag; |
| 340 | |
| 341 | if (berTag == null) { |
| 342 | berTag = new BerTag(); |
| 343 | codeLength += berTag.decode(is); |
| 344 | } |
| 345 | |
| 346 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 0)) { |
| 347 | syntaxes = new Syntaxes(); |
| 348 | codeLength += syntaxes.decode(is, false); |
| 349 | return codeLength; |
| 350 | } |
| 351 | |
| 352 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) { |
| 353 | syntax = new BerObjectIdentifier(); |
| 354 | codeLength += syntax.decode(is, false); |
| 355 | return codeLength; |
| 356 | } |
| 357 | |
| 358 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 2)) { |
| 359 | presentationContextId = new BerInteger(); |
| 360 | codeLength += presentationContextId.decode(is, false); |
| 361 | return codeLength; |
| 362 | } |
| 363 | |
| 364 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 3)) { |
| 365 | contextNegotiation = new ContextNegotiation(); |
| 366 | codeLength += contextNegotiation.decode(is, false); |
| 367 | return codeLength; |
| 368 | } |
| 369 | |
| 370 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 4)) { |
| 371 | transferSyntax = new BerObjectIdentifier(); |
| 372 | codeLength += transferSyntax.decode(is, false); |
| 373 | return codeLength; |
| 374 | } |
| 375 | |
| 376 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 5)) { |
| 377 | fixed = new BerNull(); |
| 378 | codeLength += fixed.decode(is, false); |
| 379 | return codeLength; |
| 380 | } |
| 381 | |
| 382 | if (passedTag != null) { |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | throw new IOException("Error decoding CHOICE: Tag " + berTag + " matched to no item."); |
| 387 | } |
| 388 | |
| 389 | public void encodeAndSave(int encodingSizeGuess) throws IOException { |
| 390 | BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess); |
| 391 | encode(os); |
| 392 | code = os.getArray(); |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public String toString() { |
| 397 | StringBuilder sb = new StringBuilder(); |
| 398 | appendAsString(sb, 0); |
| 399 | return sb.toString(); |
| 400 | } |
| 401 | |
| 402 | public void appendAsString(StringBuilder sb, int indentLevel) { |
| 403 | |
| 404 | if (syntaxes != null) { |
| 405 | sb.append("syntaxes: "); |
| 406 | syntaxes.appendAsString(sb, indentLevel + 1); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | if (syntax != null) { |
| 411 | sb.append("syntax: ").append(syntax); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | if (presentationContextId != null) { |
| 416 | sb.append("presentationContextId: ").append(presentationContextId); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | if (contextNegotiation != null) { |
| 421 | sb.append("contextNegotiation: "); |
| 422 | contextNegotiation.appendAsString(sb, indentLevel + 1); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | if (transferSyntax != null) { |
| 427 | sb.append("transferSyntax: ").append(transferSyntax); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | if (fixed != null) { |
| 432 | sb.append("fixed: ").append(fixed); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | sb.append("<none>"); |
| 437 | } |
| 438 | |
| 439 | public static class Syntaxes implements Serializable { |
| 440 | |
| 441 | public static final BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.CONSTRUCTED, 16); |
| 442 | private static final long serialVersionUID = 1L; |
| 443 | @JsonIgnore public byte[] code = null; |
| 444 | private BerObjectIdentifier abstract_ = null; |
| 445 | private BerObjectIdentifier transfer = null; |
| 446 | |
| 447 | public Syntaxes() { |
| 448 | } |
| 449 | |
| 450 | public Syntaxes(byte[] code) { |
| 451 | this.code = code; |
| 452 | } |
| 453 | |
| 454 | public BerObjectIdentifier getAbstract() { |
| 455 | return abstract_; |
| 456 | } |
| 457 | |
| 458 | public void setAbstract(BerObjectIdentifier abstract_) { |
| 459 | this.abstract_ = abstract_; |
| 460 | } |
| 461 | |
| 462 | public BerObjectIdentifier getTransfer() { |
| 463 | return transfer; |
| 464 | } |
| 465 | |
| 466 | public void setTransfer(BerObjectIdentifier transfer) { |
| 467 | this.transfer = transfer; |
| 468 | } |
| 469 | |
| 470 | public int encode(BerByteArrayOutputStream os) throws IOException { |
| 471 | return encode(os, true); |
| 472 | } |
| 473 | |
| 474 | public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException { |
| 475 | |
| 476 | if (code != null) { |
| 477 | for (int i = code.length - 1; i >= 0; i--) { |
| 478 | os.write(code[i]); |
| 479 | } |
| 480 | if (withTag) { |
| 481 | return tag.encode(os) + code.length; |
| 482 | } |
| 483 | return code.length; |
| 484 | } |
| 485 | |
| 486 | int codeLength = 0; |
| 487 | codeLength += transfer.encode(os, false); |
| 488 | // write tag: CONTEXT_CLASS, PRIMITIVE, 1 |
| 489 | os.write(0x81); |
| 490 | codeLength += 1; |
| 491 | |
| 492 | codeLength += abstract_.encode(os, false); |
| 493 | // write tag: CONTEXT_CLASS, PRIMITIVE, 0 |
| 494 | os.write(0x80); |
| 495 | codeLength += 1; |
| 496 | |
| 497 | codeLength += BerLength.encodeLength(os, codeLength); |
| 498 | |
| 499 | if (withTag) { |
| 500 | codeLength += tag.encode(os); |
| 501 | } |
| 502 | |
| 503 | return codeLength; |
| 504 | |
| 505 | } |
| 506 | |
| 507 | public int decode(InputStream is) throws IOException { |
| 508 | return decode(is, true); |
| 509 | } |
| 510 | |
| 511 | public int decode(InputStream is, boolean withTag) throws IOException { |
| 512 | int codeLength = 0; |
| 513 | int subCodeLength = 0; |
| 514 | BerTag berTag = new BerTag(); |
| 515 | |
| 516 | if (withTag) { |
| 517 | codeLength += tag.decodeAndCheck(is); |
| 518 | } |
| 519 | |
| 520 | BerLength length = new BerLength(); |
| 521 | codeLength += length.decode(is); |
| 522 | |
| 523 | int totalLength = length.val; |
| 524 | codeLength += totalLength; |
| 525 | |
| 526 | subCodeLength += berTag.decode(is); |
| 527 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 0)) { |
| 528 | abstract_ = new BerObjectIdentifier(); |
| 529 | subCodeLength += abstract_.decode(is, false); |
| 530 | subCodeLength += berTag.decode(is); |
| 531 | } else { |
| 532 | throw new IOException("Tag does not match the mandatory sequence element tag."); |
| 533 | } |
| 534 | |
| 535 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) { |
| 536 | transfer = new BerObjectIdentifier(); |
| 537 | subCodeLength += transfer.decode(is, false); |
| 538 | if (subCodeLength == totalLength) { |
| 539 | return codeLength; |
| 540 | } |
| 541 | } |
| 542 | throw new IOException("Unexpected end of sequence, length tag: " + totalLength |
| 543 | + ", actual sequence length: " + subCodeLength); |
| 544 | |
| 545 | } |
| 546 | |
| 547 | public void encodeAndSave(int encodingSizeGuess) throws IOException { |
| 548 | BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess); |
| 549 | encode(os, false); |
| 550 | code = os.getArray(); |
| 551 | } |
| 552 | |
| 553 | @Override |
| 554 | public String toString() { |
| 555 | StringBuilder sb = new StringBuilder(); |
| 556 | appendAsString(sb, 0); |
| 557 | return sb.toString(); |
| 558 | } |
| 559 | |
| 560 | public void appendAsString(StringBuilder sb, int indentLevel) { |
| 561 | |
| 562 | sb.append("{"); |
| 563 | sb.append("\n"); |
| 564 | for (int i = 0; i < indentLevel + 1; i++) { |
| 565 | sb.append("\t"); |
| 566 | } |
| 567 | if (abstract_ != null) { |
| 568 | sb.append("abstract_: ").append(abstract_); |
| 569 | } else { |
| 570 | sb.append("abstract_: <empty-required-field>"); |
| 571 | } |
| 572 | |
| 573 | sb.append(",\n"); |
| 574 | for (int i = 0; i < indentLevel + 1; i++) { |
| 575 | sb.append("\t"); |
| 576 | } |
| 577 | if (transfer != null) { |
| 578 | sb.append("transfer: ").append(transfer); |
| 579 | } else { |
| 580 | sb.append("transfer: <empty-required-field>"); |
| 581 | } |
| 582 | |
| 583 | sb.append("\n"); |
| 584 | for (int i = 0; i < indentLevel; i++) { |
| 585 | sb.append("\t"); |
| 586 | } |
| 587 | sb.append("}"); |
| 588 | } |
| 589 | |
| 590 | } |
| 591 | |
| 592 | public static class ContextNegotiation implements Serializable { |
| 593 | |
| 594 | public static final BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.CONSTRUCTED, 16); |
| 595 | private static final long serialVersionUID = 1L; |
| 596 | @JsonIgnore public byte[] code = null; |
| 597 | private BerInteger presentationContextId = null; |
| 598 | private BerObjectIdentifier transferSyntax = null; |
| 599 | |
| 600 | public ContextNegotiation() { |
| 601 | } |
| 602 | |
| 603 | public ContextNegotiation(byte[] code) { |
| 604 | this.code = code; |
| 605 | } |
| 606 | |
| 607 | public BerInteger getPresentationContextId() { |
| 608 | return presentationContextId; |
| 609 | } |
| 610 | |
| 611 | public void setPresentationContextId(BerInteger presentationContextId) { |
| 612 | this.presentationContextId = presentationContextId; |
| 613 | } |
| 614 | |
| 615 | public BerObjectIdentifier getTransferSyntax() { |
| 616 | return transferSyntax; |
| 617 | } |
| 618 | |
| 619 | public void setTransferSyntax(BerObjectIdentifier transferSyntax) { |
| 620 | this.transferSyntax = transferSyntax; |
| 621 | } |
| 622 | |
| 623 | public int encode(BerByteArrayOutputStream os) throws IOException { |
| 624 | return encode(os, true); |
| 625 | } |
| 626 | |
| 627 | public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException { |
| 628 | |
| 629 | if (code != null) { |
| 630 | for (int i = code.length - 1; i >= 0; i--) { |
| 631 | os.write(code[i]); |
| 632 | } |
| 633 | if (withTag) { |
| 634 | return tag.encode(os) + code.length; |
| 635 | } |
| 636 | return code.length; |
| 637 | } |
| 638 | |
| 639 | int codeLength = 0; |
| 640 | codeLength += transferSyntax.encode(os, false); |
| 641 | // write tag: CONTEXT_CLASS, PRIMITIVE, 1 |
| 642 | os.write(0x81); |
| 643 | codeLength += 1; |
| 644 | |
| 645 | codeLength += presentationContextId.encode(os, false); |
| 646 | // write tag: CONTEXT_CLASS, PRIMITIVE, 0 |
| 647 | os.write(0x80); |
| 648 | codeLength += 1; |
| 649 | |
| 650 | codeLength += BerLength.encodeLength(os, codeLength); |
| 651 | |
| 652 | if (withTag) { |
| 653 | codeLength += tag.encode(os); |
| 654 | } |
| 655 | |
| 656 | return codeLength; |
| 657 | |
| 658 | } |
| 659 | |
| 660 | public int decode(InputStream is) throws IOException { |
| 661 | return decode(is, true); |
| 662 | } |
| 663 | |
| 664 | public int decode(InputStream is, boolean withTag) throws IOException { |
| 665 | int codeLength = 0; |
| 666 | int subCodeLength = 0; |
| 667 | BerTag berTag = new BerTag(); |
| 668 | |
| 669 | if (withTag) { |
| 670 | codeLength += tag.decodeAndCheck(is); |
| 671 | } |
| 672 | |
| 673 | BerLength length = new BerLength(); |
| 674 | codeLength += length.decode(is); |
| 675 | |
| 676 | int totalLength = length.val; |
| 677 | codeLength += totalLength; |
| 678 | |
| 679 | subCodeLength += berTag.decode(is); |
| 680 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 0)) { |
| 681 | presentationContextId = new BerInteger(); |
| 682 | subCodeLength += presentationContextId.decode(is, false); |
| 683 | subCodeLength += berTag.decode(is); |
| 684 | } else { |
| 685 | throw new IOException("Tag does not match the mandatory sequence element tag."); |
| 686 | } |
| 687 | |
| 688 | if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) { |
| 689 | transferSyntax = new BerObjectIdentifier(); |
| 690 | subCodeLength += transferSyntax.decode(is, false); |
| 691 | if (subCodeLength == totalLength) { |
| 692 | return codeLength; |
| 693 | } |
| 694 | } |
| 695 | throw new IOException("Unexpected end of sequence, length tag: " + totalLength |
| 696 | + ", actual sequence length: " + subCodeLength); |
| 697 | |
| 698 | } |
| 699 | |
| 700 | public void encodeAndSave(int encodingSizeGuess) throws IOException { |
| 701 | BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess); |
| 702 | encode(os, false); |
| 703 | code = os.getArray(); |
| 704 | } |
| 705 | |
| 706 | @Override |
| 707 | public String toString() { |
| 708 | StringBuilder sb = new StringBuilder(); |
| 709 | appendAsString(sb, 0); |
| 710 | return sb.toString(); |
| 711 | } |
| 712 | |
| 713 | public void appendAsString(StringBuilder sb, int indentLevel) { |
| 714 | |
| 715 | sb.append("{"); |
| 716 | sb.append("\n"); |
| 717 | for (int i = 0; i < indentLevel + 1; i++) { |
| 718 | sb.append("\t"); |
| 719 | } |
| 720 | if (presentationContextId != null) { |
| 721 | sb.append("presentationContextId: ").append(presentationContextId); |
| 722 | } else { |
| 723 | sb.append("presentationContextId: <empty-required-field>"); |
| 724 | } |
| 725 | |
| 726 | sb.append(",\n"); |
| 727 | for (int i = 0; i < indentLevel + 1; i++) { |
| 728 | sb.append("\t"); |
| 729 | } |
| 730 | if (transferSyntax != null) { |
| 731 | sb.append("transferSyntax: ").append(transferSyntax); |
| 732 | } else { |
| 733 | sb.append("transferSyntax: <empty-required-field>"); |
| 734 | } |
| 735 | |
| 736 | sb.append("\n"); |
| 737 | for (int i = 0; i < indentLevel; i++) { |
| 738 | sb.append("\t"); |
| 739 | } |
| 740 | sb.append("}"); |
| 741 | } |
| 742 | |
| 743 | } |
| 744 | |
| 745 | } |
| 746 | |
| 747 | } |