@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());
  }