// -*- 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: InputStream.jlc,v 1.5 2003/09/27 08:10:28 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/io/InputStream.h" #include "jakelib2/io/OutputStream.h" #include "jakelib2/lang/Math.h" using namespace jakelib::lang; using namespace jakelib::io; JAKELIB_IMPLEMENT_CLASS("jakelib.io.InputStream", InputStream, Object) const long InputStream::SKIP_BUFFER_SIZE = 2048; jbyte* InputStream::skipBuffer = null; /*****************************************************************************\ * read | *****************************************************************************/ int InputStream::read(jbyte* b, int offset, int len) { if (b == null) throw new NullPointerException(); else if (offset < 0 || len < 0) throw new IndexOutOfBoundsException(); else if (len == 0) return 0; int c = read(); if (c == -1) { return -1; } b[offset] = (jbyte) c; int r = 1; try { for (; r < len ; r++) { c = read(); if (c == -1) break; if (b != null) b[offset + r] = (jbyte) c; } } catch (IOException ee) {} return r; } /*****************************************************************************\ * skip | *****************************************************************************/ int InputStream::skip(int n) { // FIXME return 0; } /*****************************************************************************\ * available | *****************************************************************************/ int InputStream::available() { return 0; } /*****************************************************************************\ * close | *****************************************************************************/ void InputStream::close() {} /*****************************************************************************\ * mark | *****************************************************************************/ void InputStream::mark(int readLimit) { throw new IOException(`"mark() not supported"` .. JAKELIB_AT2("jakelib.io.InputStream.mark")); } /*****************************************************************************\ * reset | *****************************************************************************/ void InputStream::reset() { throw new IOException(`"reset() not supported"` .. JAKELIB_AT2("jakelib.io.InputStream.reset")); } /*****************************************************************************\ * markSupported | *****************************************************************************/ jboolean InputStream::markSupported() { return false; } /*****************************************************************************\ * copyTo | *****************************************************************************/ void InputStream::copyTo(OutputStream* destination, int maxBytes) { jbyte* buffer = (jbyte*) GC_MALLOC_ATOMIC(8192); int bytesToRead = maxBytes; if (buffer == null) { throw new MemoryException(JAKELIB_AT2("jakelib.io.InputStream.copyTo")); } for (;;) { int bytesRead = read(buffer, 0, maxBytes == -1 ? 8192 : Math::min(8192, bytesToRead)); if (bytesRead <= 0) break; destination->write(buffer, 0, bytesRead); bytesToRead -= bytesRead; } GC_FREE(buffer); } void InputStream::copyTo(jakelib::io::OutputStream* destination) { copyTo(destination, -1); }