@Override
  public void startDocument(
      XMLSerializer serializer,
      boolean fragment,
      int[] nsUriIndex2prefixIndex,
      NamespaceContextImpl nsContext)
      throws IOException, SAXException, XMLStreamException {
    super.startDocument(serializer, fragment, nsUriIndex2prefixIndex, nsContext);

    octetBufferIndex = 0;
    if (!fragment) {
      write(XML_DECL);
    }
    if (header != null) {
      textBuffer.set(header);
      textBuffer.write(this);
    }
  }
 @Override
 public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
   flushBuffer();
   super.endDocument(fragment);
 }