/* Generated automatically by jlpp - do not edit. */ #include static jakelib::lang::String* jakelib2_strings[] = {null, null, null, null, null, null, null, null, null, null, null, null, null, null}; // "r" static jchar chars_jakelib2_str_0[] = {114}; // "rw" static jchar chars_jakelib2_str_1[] = {114,119}; // "Bad mode value: " static jchar chars_jakelib2_str_2[] = {66,97,100,32,109,111,100,101,32,118,97,108,117,101,58,32}; // "jakelib.io.RandomAccessFile.init" static jchar chars_jakelib2_str_3[] = {106,97,107,101,108,105,98,46,105,111,46,82,97,110,100,111,109,65,99,99,101,115,115,70,105,108,101,46,105,110,105,116}; // "File not found: " static jchar chars_jakelib2_str_4[] = {70,105,108,101,32,110,111,116,32,102,111,117,110,100,58,32}; // "jakelib.io.RandomAccessFile.init" static jchar chars_jakelib2_str_5[] = {106,97,107,101,108,105,98,46,105,111,46,82,97,110,100,111,109,65,99,99,101,115,115,70,105,108,101,46,105,110,105,116}; // "jakelib.io.RandomAccessFile.read" static jchar chars_jakelib2_str_6[] = {106,97,107,101,108,105,98,46,105,111,46,82,97,110,100,111,109,65,99,99,101,115,115,70,105,108,101,46,114,101,97,100}; // "jakelib.io.RandomAccessFile.read" static jchar chars_jakelib2_str_7[] = {106,97,107,101,108,105,98,46,105,111,46,82,97,110,100,111,109,65,99,99,101,115,115,70,105,108,101,46,114,101,97,100}; // "Unexpected end of stream" static jchar chars_jakelib2_str_8[] = {85,110,101,120,112,101,99,116,101,100,32,101,110,100,32,111,102,32,115,116,114,101,97,109}; // "jakelib.io.RandomAccessFile.readFully" static jchar chars_jakelib2_str_9[] = {106,97,107,101,108,105,98,46,105,111,46,82,97,110,100,111,109,65,99,99,101,115,115,70,105,108,101,46,114,101,97,100,70,117,108,108,121}; // "Unexpected end of stream" static jchar chars_jakelib2_str_10[] = {85,110,101,120,112,101,99,116,101,100,32,101,110,100,32,111,102,32,115,116,114,101,97,109}; // "jakelib.io.RandomAccessFile.readByte" static jchar chars_jakelib2_str_11[] = {106,97,107,101,108,105,98,46,105,111,46,82,97,110,100,111,109,65,99,99,101,115,115,70,105,108,101,46,114,101,97,100,66,121,116,101}; // "Unexpected end of stream" static jchar chars_jakelib2_str_12[] = {85,110,101,120,112,101,99,116,101,100,32,101,110,100,32,111,102,32,115,116,114,101,97,109}; // "jakelib.io.RandomAccessFile.readBoolean" static jchar chars_jakelib2_str_13[] = {106,97,107,101,108,105,98,46,105,111,46,82,97,110,100,111,109,65,99,99,101,115,115,70,105,108,101,46,114,101,97,100,66,111,111,108,101,97,110}; #line 1 "io/RandomAccessFile.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: 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 (!JAKELIB_ONDEMAND(jakelib2_strings[0], new jakelib::lang::String(chars_jakelib2_str_0, 0, 1))->equals(mode) && !JAKELIB_ONDEMAND(jakelib2_strings[1], new jakelib::lang::String(chars_jakelib2_str_1, 0, 2))->equals(mode)) throw new IllegalArgumentException(JAKELIB_ONDEMAND(jakelib2_strings[2], new jakelib::lang::String(chars_jakelib2_str_2, 0, 16)) ->plus( mode )->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[3], new jakelib::lang::String(chars_jakelib2_str_3, 0, 32))))); handle = fopen(name->latin1(), mode->latin1()); if (handle == null) throw new FileNotFoundException(JAKELIB_ONDEMAND(jakelib2_strings[4], new jakelib::lang::String(chars_jakelib2_str_4, 0, 16)) ->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[5], new jakelib::lang::String(chars_jakelib2_str_5, 0, 32)))), 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_ONDEMAND(jakelib2_strings[6], new jakelib::lang::String(chars_jakelib2_str_6, 0, 32)))); else if (len < 0 || offset < 0) throw new IndexOutOfBoundsException(JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[7], new jakelib::lang::String(chars_jakelib2_str_7, 0, 32)))); 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(JAKELIB_ONDEMAND(jakelib2_strings[8], new jakelib::lang::String(chars_jakelib2_str_8, 0, 24)) ->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[9], new jakelib::lang::String(chars_jakelib2_str_9, 0, 37))))); total_read += bytes_read; } } /*****************************************************************************\ * readByte | *****************************************************************************/ jbyte RandomAccessFile::readByte() { int b = read(); if (b == -1) throw new EOFException(JAKELIB_ONDEMAND(jakelib2_strings[10], new jakelib::lang::String(chars_jakelib2_str_10, 0, 24)) ->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[11], new jakelib::lang::String(chars_jakelib2_str_11, 0, 36))))); return b; } /*****************************************************************************\ * readBoolean | *****************************************************************************/ jboolean RandomAccessFile::readBoolean() { int b = read(); if (b == -1) throw new EOFException(JAKELIB_ONDEMAND(jakelib2_strings[12], new jakelib::lang::String(chars_jakelib2_str_12, 0, 24)) ->plus( JAKELIB_AT2(JAKELIB_ONDEMAND(jakelib2_strings[13], new jakelib::lang::String(chars_jakelib2_str_13, 0, 39))))); 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))); }