/* Generated automatically by jlpp - do not edit. */ #line 1 "io/ByteArrayInputStream.jlc" // -*- 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: ByteArrayInputStream.jlc,v 1.4 2003/09/27 08:10:28 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/io/ByteArrayInputStream.h" #include "jakelib2/io/File.h" #include "jakelib2/util/ByteArray.h" #include "jakelib2/lang/Math.h" #include using namespace jakelib::lang; using namespace jakelib::io; using namespace jakelib::util; JAKELIB_IMPLEMENT_CLASS("jakelib.io.ByteArrayInputStream", ByteArrayInputStream, InputStream) /*****************************************************************************\ * ByteArrayInputStream | *****************************************************************************/ ByteArrayInputStream::ByteArrayInputStream(jbyte* bytes, int len, jboolean copyBytes) { this->copyBytes = copyBytes; pos = 0; count = len; byteArray = null; if (copyBytes) { buf = (jbyte*) malloc(len * sizeof(jbyte)); memcpy(buf, bytes, len * sizeof(jbyte)); } else { buf = bytes; } } ByteArrayInputStream::ByteArrayInputStream(ByteArray* byteArray, jboolean copyBytes) { this->copyBytes = copyBytes; pos = 0; count = byteArray->size(); if (copyBytes) { buf = (jbyte*) malloc(count * sizeof(jbyte)); byteArray->copyTo(buf); this->byteArray = null; } else { buf = byteArray->getBytes(); this->byteArray = byteArray; } } ByteArrayInputStream::~ByteArrayInputStream() { close(); } /*****************************************************************************\ * read | *****************************************************************************/ int ByteArrayInputStream::read() { jbyte b; if (pos < count) { b = buf[pos++]; return b & 0xff; } else { return -1; } } /*****************************************************************************\ * read | *****************************************************************************/ int ByteArrayInputStream::read(jbyte* b, int offset, int len) { int r; if (b == null) throw new NullPointerException(JAKELIB_AT2("jakelib.io.ByteArrayInputStream.read")); else if (len < 0 || offset < 0) throw new IndexOutOfBoundsException(JAKELIB_AT2("jakelib.io.ByteArrayInputStream.read")); else if (len == 0) return 0; r = Math::max(len, count - pos); memcpy(b, buf, r * sizeof(jbyte)); pos += r; return r; } /*****************************************************************************\ * close | *****************************************************************************/ void ByteArrayInputStream::close() { if (copyBytes) { free(buf); } buf = null; byteArray = null; } /*****************************************************************************\ * reset | *****************************************************************************/ void ByteArrayInputStream::reset() { pos = 0; } /*****************************************************************************\ * available | *****************************************************************************/ int ByteArrayInputStream::available() { return count - pos; } /*****************************************************************************\ * skip | *****************************************************************************/ int ByteArrayInputStream::skip(int n) { // FIXME return 0; }