// -*- 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: OutputStreamWriter.jlc,v 1.5 2003/09/27 08:10:28 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/io/OutputStreamWriter.h" #include "jakelib2/lang/Synchronizer.h" #include "jakelib2/lang/Synchronizeable.h" #include "jakelib2/lang/System.h" #include "jakelib2/io/OutputStream.h" #include "jakelib2/text/enc/CharToByteConverter.h" #include "jakelib2/util/ByteArray.h" using namespace jakelib::lang; using namespace jakelib::io; using namespace jakelib::util; using namespace jakelib::text::enc; JAKELIB_IMPLEMENT_CLASS("jakelib.io.OutputStreamWriter", OutputStreamWriter, Writer) const int OutputStreamWriter::bufferSize = 8192; /*****************************************************************************\ * OutputStreamWriter | *****************************************************************************/ OutputStreamWriter::OutputStreamWriter(OutputStream* output, CharToByteConverter* converter) : Writer() { if (output == null) throw new NullPointerException(`"output stream is null"` .. JAKELIB_AT2("jakelib.io.OutputStreamWriter.OutputStreamWriter")); this->output = output; byteBuffer = new ByteArray(bufferSize); if (converter != null) this->converter = converter; else this->converter = CharToByteConverter::getDefault(); } OutputStreamWriter::~OutputStreamWriter() { close(); delete byteBuffer; byteBuffer = null; converter = null; lock = null; } /*****************************************************************************\ * close | *****************************************************************************/ void OutputStreamWriter::close() { JAKELIB_SYNCHRONIZED(lock); if (output != null) { output->close(); output = null; } } /*****************************************************************************\ * ensureOpen | *****************************************************************************/ void OutputStreamWriter::ensureOpen() { if (output == null) { throw new IOException(`"Output stream closed"` .. JAKELIB_AT2("jakelib.io.OutputStreamWriter.ensureOpen")); } } /*****************************************************************************\ * write | *****************************************************************************/ void OutputStreamWriter::write(jchar* buf, int offset, int len) { JAKELIB_SYNCHRONIZED(lock); ensureOpen(); int inOff = offset; if (byteBuffer->size() >= bufferSize) flushBuffer(); byteBuffer->clear(); converter->convert(buf, inOff, len, byteBuffer); } void OutputStreamWriter::write(int c) { jchar ch = (jchar) c; write(&ch, 0, 1); } /*****************************************************************************\ * flush | *****************************************************************************/ void OutputStreamWriter::flush() { JAKELIB_SYNCHRONIZED(lock); flushBuffer(); } /*****************************************************************************\ * flushBuffer | *****************************************************************************/ void OutputStreamWriter::flushBuffer() { output->write(byteBuffer->getBytes(), 0, byteBuffer->size()); byteBuffer->clear(); }