@Override
  public void writeAFP(final OutputStream os, final AFPParserConfiguration config)
      throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    if (this.getName() != null) {
      baos.write(
          UtilCharacterEncoding.stringToByteArray(
              this.getName(), config.getAfpCharSet(), 8, Constants.EBCDIC_ID_FILLER));
    }

    // Even if we've not read any triplet - one or more triplets may have been added in the
    // meantime.
    // In this case we definitely have to write write the two "reserved bytes" on BDT and BRS
    // structured fields.
    if (this.triplets != null
        || this.isBeginResourceOrBeginDocument() && this.reserved8_9 != null) {
      baos.write(this.reserved8_9 != null ? this.reserved8_9 : new byte[] {0x00, 0x00});
    }

    if (this.triplets != null) {
      for (final Triplet triplet : this.triplets) {
        triplet.writeAFP(baos, config);
      }
    }

    this.writeFullStructuredField(os, baos.toByteArray());
  }
  @Override
  public void writeAFP(OutputStream os, AFPParserConfiguration config) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    baos.write(
        UtilCharacterEncoding.stringToByteArray(
            defaultGraphicCharacterGlobalID, cpIBM500, 8, Constants.EBCDIC_ID_FILLER));
    baos.write(DefaultCharacterUseFlag.toByte(defaultCharacterUseFlags));
    baos.write(cpiRepeatingGroupLength.val);
    baos.write(UtilBinaryDecoding.shortToByteArray(spaceCharacterSectionNumber, 1));
    baos.write(UtilBinaryDecoding.shortToByteArray(spaceCharacterCodePoint, 1));
    baos.write(CodePageUseFlag.toByte(codePageUseFlags));
    if (cpiRepeatingGroupLength == CPIRepeatingGroupLength.SingleByteCodePageUnicodeScalarValues
        || cpiRepeatingGroupLength
            == CPIRepeatingGroupLength.DoubleByteCodePageUnicodeScalarValues) {
      baos.write(UtilBinaryDecoding.longToByteArray(unicodeScalarValue, 4));
    }

    writeFullStructuredField(os, baos.toByteArray());
  }