// -*- 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: RandomAccessFile.jlc,v 1.5 2003/09/27 08:10:28 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/io/RandomAccessFile.h" #include "jakelib2/io/File.h" using namespace jakelib::lang; using namespace jakelib::io; JAKELIB_IMPLEMENT_CLASS("jakelib.io.RandomAccessFile", RandomAccessFile, Object); #pragma javasyntax /*****************************************************************************\ * RandomAccessFile | *****************************************************************************/ RandomAccessFile::RandomAccessFile(String* name, String* mode) { init(name, mode); } RandomAccessFile::RandomAccessFile(File* file, String* mode) { init(file->getPath(), mode); } RandomAccessFile::~RandomAccessFile() { close(); } /*****************************************************************************\ * init | *****************************************************************************/ void RandomAccessFile::init(String* name, String* mode) { if (!"r"->equals(mode) && !"rw"->equals(mode)) throw new IllegalArgumentException("Bad mode value: " .. mode .. JAKELIB_AT2("jakelib.io.RandomAccessFile.init")); handle = fopen(name->latin1(), mode->latin1()); if (handle == null) throw new FileNotFoundException("File not found: " .. JAKELIB_AT2("jakelib.io.RandomAccessFile.init"), name); } /*****************************************************************************\ * close | *****************************************************************************/ void RandomAccessFile::close() { if (handle != null) { fclose(handle); handle = null; } } /*****************************************************************************\ * seek | *****************************************************************************/ void RandomAccessFile::seek(int pos) { fseek(handle, SEEK_SET, pos); } /*****************************************************************************\ * skipBytes | *****************************************************************************/ int RandomAccessFile::skipBytes(int n) { if (n > 0) { long pos = ftell(handle); fseek(handle, SEEK_CUR, pos); return ftell(handle) - pos; } else { return 0; } } /*****************************************************************************\ * read | *****************************************************************************/ int RandomAccessFile::read() { jbyte b; long r; r = read(&b, 0, 1); if (r == 1) return b & 0xff; else return -1; } /*****************************************************************************\ * read | *****************************************************************************/ int RandomAccessFile::read(jbyte* buf, int offset, int len) { int r; if (buf == null) throw new NullPointerException(JAKELIB_AT2("jakelib.io.RandomAccessFile.read")); else if (len < 0 || offset < 0) throw new IndexOutOfBoundsException(JAKELIB_AT2("jakelib.io.RandomAccessFile.read")); else if (len == 0) return 0; r = fread(&buf[offset], 1, len, handle); if (r > 0) return r; else return -1; } /*****************************************************************************\ * readFully | *****************************************************************************/ void RandomAccessFile::readFully(jbyte* buf, int offset, int len) { int total_read = 0; while (total_read < len) { int bytes_read = read(buf, offset + total_read, len - total_read); if (bytes_read == -1) throw new EOFException("Unexpected end of stream" .. JAKELIB_AT2("jakelib.io.RandomAccessFile.readFully")); total_read += bytes_read; } } /*****************************************************************************\ * readByte | *****************************************************************************/ jbyte RandomAccessFile::readByte() { int b = read(); if (b == -1) throw new EOFException("Unexpected end of stream" .. JAKELIB_AT2("jakelib.io.RandomAccessFile.readByte")); return b; } /*****************************************************************************\ * readBoolean | *****************************************************************************/ jboolean RandomAccessFile::readBoolean() { int b = read(); if (b == -1) throw new EOFException("Unexpected end of stream" .. JAKELIB_AT2("jakelib.io.RandomAccessFile.readBoolean")); return b != 0; } /*****************************************************************************\ * readChar | *****************************************************************************/ jchar RandomAccessFile::readChar() { jbyte buf[2]; readFully(buf, 0, 2); return (jchar) ((buf[0] << 8) | (buf[1] & 0xff)); } /*****************************************************************************\ * readInt | *****************************************************************************/ jint RandomAccessFile::readInt() { jbyte buf[4]; readFully(buf, 0, 4); return (((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) | ((buf[2] & 0xff) << 8) | (buf[3] & 0xff)); } /*****************************************************************************\ * readLong | *****************************************************************************/ jlong RandomAccessFile::readLong() { jbyte buf[8]; readFully(buf, 0, 8); return (((jlong)(buf[0] & 0xff) << 56) | ((jlong)(buf[1] & 0xff) << 48) | ((jlong)(buf[2] & 0xff) << 40) | ((jlong)(buf[3] & 0xff) << 32) | ((jlong)(buf[4] & 0xff) << 24) | ((jlong)(buf[5] & 0xff) << 16) | ((jlong)(buf[6] & 0xff) << 8) | ((jlong)(buf[7] & 0xff))); }