/**
   * Writes the given byte[] as base64 encoded binary to the output.
   *
   * <p>Being defined on this class allows this method to access the buffer directly, which
   * translates to a better performance.
   */
  public void text(byte[] data, int dataLen) throws IOException {
    closeStartTag();

    int start = 0;

    while (dataLen > 0) {
      // how many bytes (in data) can we write without overflowing the buffer?
      int batchSize = Math.min(((octetBuffer.length - octetBufferIndex) / 4) * 3, dataLen);

      // write the batch
      octetBufferIndex =
          DatatypeConverterImpl._printBase64Binary(
              data, start, batchSize, octetBuffer, octetBufferIndex);

      if (batchSize < dataLen) flushBuffer();

      start += batchSize;
      dataLen -= batchSize;
    }
  }
 @Override
 public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
   flushBuffer();
   super.endDocument(fragment);
 }