/**
   * Serialises out the String. There are special rules about where we can and can't split onto
   * Continue records.
   */
  public void serialize(ContinuableRecordOutput out) {
    int numberOfRichTextRuns = 0;
    int extendedDataSize = 0;
    if (isRichText() && field_4_format_runs != null) {
      numberOfRichTextRuns = field_4_format_runs.size();
    }
    if (isExtendedText() && field_5_ext_rst != null) {
      extendedDataSize = 4 + field_5_ext_rst.getDataSize();
    }

    // Serialise the bulk of the String
    // The writeString handles tricky continue stuff for us
    out.writeString(field_3_string, numberOfRichTextRuns, extendedDataSize);

    if (numberOfRichTextRuns > 0) {

      // This will ensure that a run does not split a continue
      for (int i = 0; i < numberOfRichTextRuns; i++) {
        if (out.getAvailableSpace() < 4) {
          out.writeContinue();
        }
        FormatRun r = field_4_format_runs.get(i);
        r.serialize(out);
      }
    }

    if (extendedDataSize > 0) {
      field_5_ext_rst.serialize(out);
    }
  }
  /**
   * construct a unicode string record and fill its fields, ID is ignored
   *
   * @param in the RecordInputstream to read the record from
   */
  public UnicodeString(RecordInputStream in) {
    field_1_charCount = in.readShort();
    field_2_optionflags = in.readByte();

    int runCount = 0;
    int extensionLength = 0;
    // Read the number of rich runs if rich text.
    if (isRichText()) {
      runCount = in.readShort();
    }
    // Read the size of extended data if present.
    if (isExtendedText()) {
      extensionLength = in.readInt();
    }

    boolean isCompressed = ((field_2_optionflags & 1) == 0);
    if (isCompressed) {
      field_3_string = in.readCompressedUnicode(getCharCount());
    } else {
      field_3_string = in.readUnicodeLEString(getCharCount());
    }

    if (isRichText() && (runCount > 0)) {
      field_4_format_runs = new ArrayList<FormatRun>(runCount);
      for (int i = 0; i < runCount; i++) {
        field_4_format_runs.add(new FormatRun(in));
      }
    }

    if (isExtendedText() && (extensionLength > 0)) {
      field_5_ext_rst = new ExtRst(new ContinuableRecordInput(in), extensionLength);
      if (field_5_ext_rst.getDataSize() + 4 != extensionLength) {
        System.err.println(
            "ExtRst was supposed to be "
                + extensionLength
                + " bytes long, but seems to actually be "
                + (field_5_ext_rst.getDataSize() + 4));
      }
    }
  }