public int encode(BerByteArrayOutputStream berOStream, boolean explicit) throws IOException {

    int codeLength;

    if (code != null) {
      codeLength = code.length;
      for (int i = code.length - 1; i >= 0; i--) {
        berOStream.write(code[i]);
      }
    } else {
      codeLength = 0;
      codeLength += confirmedServiceRequest.encode(berOStream, true);

      codeLength += invokeID.encode(berOStream, true);

      codeLength += BerLength.encodeLength(berOStream, codeLength);
    }

    if (explicit) {
      codeLength += id.encode(berOStream);
    }

    return codeLength;
  }
 public void encodeAndSave(int encodingSizeGuess) throws IOException {
   BerByteArrayOutputStream berOStream = new BerByteArrayOutputStream(encodingSizeGuess);
   encode(berOStream, false);
   code = berOStream.getArray();
 }