// -*- 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: InputStreamReader.jlc,v 1.6 2003/09/27 08:10:28 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/io/InputStreamReader.h" #include "jakelib2/lang/Synchronizer.h" #include "jakelib2/lang/Synchronizeable.h" #include "jakelib2/io/InputStream.h" #include "jakelib2/util/CharArray.h" #include "jakelib2/text/enc/ByteToCharConverter.h" using namespace jakelib::lang; using namespace jakelib::io; using namespace jakelib::util; using namespace jakelib::text::enc; JAKELIB_IMPLEMENT_CLASS("jakelib.io.InputStreamReader", InputStreamReader, Reader) const int InputStreamReader::bufferSize = 8192; /*****************************************************************************\ * InputStreamReader | *****************************************************************************/ InputStreamReader::InputStreamReader(InputStream* input, ByteToCharConverter* converter) : Reader() { init(input, converter); } InputStreamReader::InputStreamReader(InputStream* input, String* encoding) : Reader() { init(input, ByteToCharConverter::getConverter(encoding)); } InputStreamReader::~InputStreamReader() { close(); delete charBuffer; charBuffer = null; converter = null; lock = null; } /*****************************************************************************\ * init | *****************************************************************************/ void InputStreamReader::init(InputStream* input, ByteToCharConverter* converter) { if (input == null) throw new NullPointerException(`"input stream is null"` .. JAKELIB_AT2("jakelib.io.InputStreamReader.InputStreamReader")); this->input = input; charBuffer = new CharArray(bufferSize); if (converter != null) this->converter = converter; else this->converter = ByteToCharConverter::getDefault(); } /*****************************************************************************\ * read | *****************************************************************************/ jint InputStreamReader::read() { jchar c; if (read(&c, 0, 1) < 1) return -1; else return (jint) c; } int InputStreamReader::read(jchar* buf, int offset, int len) { JAKELIB_SYNCHRONIZED(lock); ensureOpen(); if (len == 0) return 0; return converter->convert(input, &buf[offset], len); } /*****************************************************************************\ * ready | *****************************************************************************/ jboolean InputStreamReader::ready() { // FIXME return false; } /*****************************************************************************\ * close | *****************************************************************************/ void InputStreamReader::close() { JAKELIB_SYNCHRONIZED(lock); if (input != null) { input->close(); input = null; } } /*****************************************************************************\ * ensureOpen | *****************************************************************************/ void InputStreamReader::ensureOpen() { if (input == null) throw new IOException(`"Input stream closed"` .. JAKELIB_AT2("jakelib.io.InputStreamReader.ensureOpen")); }