// -*- 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: FileInputStream.jlc,v 1.8 2006-02-11 11:31:25 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/io/FileInputStream.h" #include "jakelib2/io/File.h" #include "jakelib2/lang/System.h" #ifdef HAVE_ERRNO_H # include #endif #ifdef HAVE_FCNTL_H # include #endif #ifdef HAVE_UNISTD_H # include #endif using namespace jakelib::lang; using namespace jakelib::io; JAKELIB_IMPLEMENT_CLASS("jakelib.io.FileInputStream", FileInputStream, InputStream) /*****************************************************************************\ * FileInputStream | *****************************************************************************/ FileInputStream::FileInputStream(File* file) { init(file->getPath()); } FileInputStream::FileInputStream(char* filename) { String _filename(filename); init(&_filename); } FileInputStream::FileInputStream(String* filename) { init(filename); } FileInputStream::~FileInputStream() { close(); } /*****************************************************************************\ * init | *****************************************************************************/ void FileInputStream::init(String* filename) { #if defined(JAKELIB_WIN32API) handle = CreateFile(JAKELIB_LATIN1(filename), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (handle == INVALID_HANDLE_VALUE) { throw new FileNotFoundException(System::explainErrorCode(GetLastError()) .. JAKELIB_AT2("jakelib.io.FileInputStream.init"), filename); } #else handle = open(JAKELIB_LATIN1(filename), O_RDONLY); if (handle == -1) { throw new FileNotFoundException(System::explainErrorCode(errno) .. JAKELIB_AT2("jakelib.io.FileInputStream.init"), filename); } #endif } /*****************************************************************************\ * read | *****************************************************************************/ int FileInputStream::read() { jbyte b; long r; r = read(&b, 0, 1); if (r == 1) return b & 0xff; else return -1; } /*****************************************************************************\ * read | *****************************************************************************/ int FileInputStream::read(jbyte* b, int offset, int len) { if (b == null) throw new NullPointerException(JAKELIB_AT2("jakelib.io.FileInputStream.read")); else if (len < 0 || offset < 0) throw new IndexOutOfBoundsException(JAKELIB_AT2("jakelib.io.FileInputStream.read")); else if (len == 0) return 0; #if defined(JAKELIB_WIN32API) DWORD r; if (!ReadFile(handle, &b[offset], len, &r, NULL)) { throw new IOException(System::explainErrorCode(GetLastError()) .. JAKELIB_AT2("jakelib.io.FileInputStream.read")); } if (r == 0) return -1; else return r; #else int r; if ((r = ::read(handle, (char*) &b[offset], len)) < 0) { throw new IOException(System::explainErrorCode(errno) .. JAKELIB_AT2("jakelib.io.FileInputStream.read")); } if (r == 0) return -1; else return r; #endif } /*****************************************************************************\ * close | *****************************************************************************/ void FileInputStream::close() { #if defined(JAKELIB_WIN32API) if (handle != INVALID_HANDLE_VALUE) { CloseHandle(handle); handle = INVALID_HANDLE_VALUE; } #else if (handle != -1) { ::close(handle); handle = -1; } #endif } /*****************************************************************************\ * reset | *****************************************************************************/ void FileInputStream::reset() { #if defined(JAKELIB_WIN32API) // FIXME #else lseek(handle, 0, SEEK_SET); #endif } /*****************************************************************************\ * available | *****************************************************************************/ int FileInputStream::available() { // FIXME return 0; } /*****************************************************************************\ * skip | *****************************************************************************/ int FileInputStream::skip(int n) { // FIXME return 0; }