// -*- c++ -*-

/*
 * Jakelib2 - General purpose C++ library
 * Copyright (C) 2001 Florian Wolff  (florian@donuz.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: BufferedWriter.h,v 1.21 2003/09/11 11:58:19 florian Exp $
 */

#ifndef INCLUDE_JAKELIB2_IO_BUFFEREDWRITER_H
# define INCLUDE_JAKELIB2_IO_BUFFEREDWRITER_H
# include "Writer.h"

namespace jakelib {
  namespace io {

    /**
     * \c BufferedWriter provides a \c Writer that uses an internal
     * buffer to improve performance when subsequently writing single
     * characters or short character sequences.
     *
     * @class BufferedWriter
     * @version $Id: BufferedWriter.h,v 1.21 2003/09/11 11:58:19 florian Exp $
     * @author Florian 'Overflo' Wolff (florian@donuz.de)
     */
    class BufferedWriter : public Writer
    {
    public:

      /**
       * @brief Creates a new \c BufferedWriter from a given \c Writer object.
       * Uses the given \c bufferSize to allocate the output buffer.
       *
       * @param writer The \c Writer to write to
       * @param bufferSize The buffer size in bytes, default is 8kB.
       */
      BufferedWriter(Writer* writer, int bufferSize = 8192);

      ~BufferedWriter();

      void write(jchar* buf, int offset, int len);

      void write(jint c);

      void write(jakelib::lang::String* str);

      void write(char* str);

      void close();

      void flush();

      /**
       * Writes the sustem-dependend newline character sequence.
       */
      void newLine();
  
    private:
      /** The current buffer size */
      int bufferSize;

      /** The underlaying \c Writer */
      Writer* writer;

      /** The output buffer */
      jchar* buffer;

      /** The number of characters already in the output buffer */
      int nChars;

      /** Asserts the the \c Writer is not yet closed. */
      void ensureOpen();

      JAKELIB_DECLARE_CLASS(BufferedWriter);
    };

  }
}
#endif

