// -*- 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: BufferedReader.jlc,v 1.6 2003/09/27 08:10:28 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/io/BufferedReader.h" #include "jakelib2/lang/Synchronizeable.h" using namespace jakelib::lang; using namespace jakelib::io; JAKELIB_IMPLEMENT_CLASS("jakelib.io.BufferedReader", BufferedReader, Reader) #ifndef WINOWS32 # include # include #endif /*****************************************************************************\ * BufferedReader | *****************************************************************************/ BufferedReader::BufferedReader(Reader* reader, int bufferSize) { nextChar = 0; nChars = 0; this->bufferSize = bufferSize; buffer = (jchar*) GC_MALLOC_ATOMIC(sizeof(jchar) * bufferSize); this->reader = reader; } /*****************************************************************************\ * ~BufferedReader | *****************************************************************************/ BufferedReader::~BufferedReader() { GC_FREE(buffer); } /*****************************************************************************\ * readLine | *****************************************************************************/ String* BufferedReader::readLine(jboolean skipLF) { int c; ensureOpen(); c = read(); if (c < 0) return null; StringBuffer buf; for (;;) { if (c == '\n') { return buf.toString(); } else if (c != '\r') buf.append((jchar) c); c = read(); if (c < 0) { return buf.toString(); } } } /*****************************************************************************\ * fill | *****************************************************************************/ void BufferedReader::fill() { if (nextChar < nChars) return; nextChar = 0; nChars = reader->read(buffer, 0, bufferSize); } /*****************************************************************************\ * ensureOpen | *****************************************************************************/ void BufferedReader::ensureOpen() { if (reader == null) throw new IOException(`"Reader closed"` .. JAKELIB_AT2("jakelib.io.BufferedReader.ensureOpen")); } /*****************************************************************************\ * read | *****************************************************************************/ jint BufferedReader::read() { ensureOpen(); if (nextChar >= nChars) { fill(); if (nextChar >= nChars) return -1; } return buffer[nextChar++]; } int BufferedReader::read(jchar* b, int offset, int len) { JAKELIB_SYNCHRONIZED(lock); ensureOpen(); if (b == null) throw new NullPointerException(JAKELIB_AT2("jakelib.io.BufferedReader.read")); else if (len < 0 || offset < 0) throw new IndexOutOfBoundsException(JAKELIB_AT2("jakelib.io.BufferedReader.read")); else if (len == 0) return 0; jint c = read(); if (c < 0) { return -1; } b[offset++] = (unsigned char) c; //try { int r = 1; for (r; r < len ; r++) { c = read(); if (c == -1) break; else b[offset ++] = (unsigned char) c; } //} //catch (IOException ee) {} return r; } /*****************************************************************************\ * markSupported | *****************************************************************************/ jboolean BufferedReader::markSupported() { return false; // return true; } /*****************************************************************************\ * ready | *****************************************************************************/ jboolean BufferedReader::ready() { return (nextChar < nChars); } /*****************************************************************************\ * close | *****************************************************************************/ void BufferedReader::close() { JAKELIB_SYNCHRONIZED(lock); if (reader != null) { reader->close(); reader = null; } } /*****************************************************************************\ * skip | *****************************************************************************/ int BufferedReader::skip(int n) { // FIXME return 0; } /*****************************************************************************\ * mark | *****************************************************************************/ void BufferedReader::mark(int readAheadLimit) { // FIXME } /*****************************************************************************\ * reset | *****************************************************************************/ void BufferedReader::reset() { // FIXME }