/**
   * Writes all data written up to the moment to string buffer.
   *
   * @param out
   * @throws IOException
   */
  public char[] toCharArray() {
    CharBuffer b = firstBuffer;

    if (b == null) {
      return new char[0];
    }

    CharBuffer l = b;

    while (l.getNext() != null) {
      l = l.getNext();
    }

    char[] result = new char[l.getTotalSize()];
    int index = 0;

    while (b != null) {
      int s = b.getUsedSize();

      System.arraycopy(b.getChars(), 0, result, index, s);
      index += s;
      b = b.getNext();
    }

    return result;
  }
  public void printTo(ServletOutputStream outputStream) throws IOException {
    CharBuffer b = firstBuffer;

    while (b != null) {
      outputStream.print(new String(b.getChars()));
      b = b.getNext();
    }
  }
  /**
   * Writes all data written up to the moment to out.
   *
   * @param out
   * @throws IOException
   */
  public void writeTo(Writer writer) throws IOException {
    CharBuffer b = firstBuffer;

    while (b != null) {
      writer.write(b.getChars(), 0, b.getUsedSize());
      b = b.getNext();
    }
  }
  /**
   * Returns instance of FastBufferOutputStream containing all data written to this writer.
   *
   * @return
   */
  public FastBufferOutputStream convertToOutputStream() {
    CharBuffer c = firstBuffer;
    ByteBuffer first = c.toByteBuffer();
    ByteBuffer b = first;

    while (c != null) {
      c = c.getNext();

      if (c == null) {
        break;
      }

      ByteBuffer n = c.toByteBuffer();

      b.setNext(n);
      b = n;
    }

    return new FastBufferOutputStream(first);
  }
  /**
   * Returns instance of FastBufferOutputStream containing all data written to this writer.
   *
   * @param encoding
   * @return
   * @throws UnsupportedEncodingException
   */
  public FastBufferOutputStream convertToOutputStream(String encoding)
      throws UnsupportedEncodingException {
    CharBuffer c = firstBuffer;
    ByteBuffer first = c.toByteBuffer(encoding);
    ByteBuffer b = first;

    while (c != null) {
      c = c.getNext();

      if (c == null) {
        break;
      }

      ByteBuffer n = c.toByteBuffer(encoding);

      b.setNext(n);
      b = n;
    }

    return new FastBufferOutputStream(first);
  }