home *** CD-ROM | disk | FTP | other *** search
- /*
- * java.io.FileInputStream.c
- *
- * Copyright (c) 1996 Systems Architecture Research Centre,
- * City University, London, UK.
- *
- * See the file "license.terms" for information on usage and redistribution
- * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- *
- * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
- */
-
- #include "config.h"
- #include <sys/types.h>
- #include <stdio.h>
- #include <assert.h>
- #if defined(HAVE_IO_H)
- #include <io.h>
- #endif
- #include "defs.h"
- #include "files.h"
- #include "java.io.stubs/FileInputStream.h"
- #include "java.io.stubs/FileDescriptor.h"
- #include "kthread.h"
-
- /*
- * Open a file for input.
- */
- void
- java_io_FileInputStream_open(struct Hjava_io_FileInputStream* this, struct Hjava_lang_String* name)
- {
- char str[MAXPATHLEN];
- int fd;
-
- javaString2CString(name, str, sizeof(str));
- fd = threadedOpen(str, O_RDONLY|O_BINARY, 0);
- unhand(unhand(this)->fd)->fd = fd;
- if (fd < 0) {
- SignalError(0, "java.io.IOException", SYS_ERROR);
- }
- }
-
- /*
- * Close file.
- */
- void
- java_io_FileInputStream_close(struct Hjava_io_FileInputStream* this)
- {
- int r;
-
- if (unhand(unhand(this)->fd)->fd >= 0) {
- r = close(unhand(unhand(this)->fd)->fd);
- unhand(unhand(this)->fd)->fd = -1;
- if (r < 0) {
- SignalError(0, "java.io.IOException", SYS_ERROR);
- }
- }
- }
-
- /*
- * Read in bytes.
- */
- jint
- java_io_FileInputStream_readBytes(struct Hjava_io_FileInputStream* fh, HArrayOfByte* bytes, jint off, jint len)
- {
- jint ret;
-
- ret = threadedRead(unhand(unhand(fh)->fd)->fd, &unhand(bytes)->body[off], len);
- if (ret < 0) {
- SignalError(0, "java.io.IOException", SYS_ERROR);
- }
- return (ret > 0 ? ret : -1);
- }
-
- /*
- * Read a single byte.
- */
- jint
- java_io_FileInputStream_read(struct Hjava_io_FileInputStream* fh)
- {
- jint ret;
- unsigned char byte;
-
- ret = threadedRead(unhand(unhand(fh)->fd)->fd, &byte, 1);
- if (ret < 0) {
- SignalError(0, "java.io.IOException", SYS_ERROR);
- }
- return (ret > 0 ? byte : -1);
- }
-
- /*
- * Skip forward in stream.
- */
- jlong
- java_io_FileInputStream_skip(struct Hjava_io_FileInputStream* fh, jlong off)
- {
- off_t ret;
-
- ret = lseek(unhand(unhand(fh)->fd)->fd, jlong2off_t(off), 1);
- if (ret < 0) {
- SignalError(0, "java.io.IOException", SYS_ERROR);
- }
- return (off_t2jlong(ret));
- }
-
- /*
- * Return the number of bytes available to read without blocking.
- */
- jint
- java_io_FileInputStream_available(struct Hjava_io_FileInputStream* fh)
- {
- int fd;
- struct stat buf;
- int ret;
- off_t sret;
-
- fd = unhand(unhand(fh)->fd)->fd;
- ret = fstat(fd, &buf);
- sret = lseek(fd, 0, 1);
- if (ret < 0 || sret < 0) {
- SignalError(0, "java.io.IOException", SYS_ERROR);
- }
- return (buf.st_size - sret);
- }
-