1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
// java.io.FileDescriptor
// An implementation of the Java Language Specification section 22.26
// Written by Charles Briscoe-Smith; refer to the file LEGAL for details.
package java.io;
class Errno {
static void throwError(String filename) throws IOException
{
int errno=errno();
if (errno==ENOENT) {
if (filename==null) throw new FileNotFoundException();
throw new FileNotFoundException(filename);
}
String err=strerror(errno);
if (filename==null) throw new IOException(err);
throw new IOException(filename+": "+err);
}
static native int errno() "return errno;" ;
static final int ENOENT=getENOENT();
private static native int getENOENT() "return ENOENT;" ;
static String strerror(int errno)
{
byte[] buf=new byte[strerrorlen(errno)];
strerrorcopy(errno, buf);
return new String(buf, 0);
}
private static native int strerrorlen(int errno)
"return strlen(strerror(l_errno));"
;
private static native void strerrorcopy(int errno, byte[] buf)
"char *buf=&l_buf->component[0];"
"const char *str=strerror(l_errno);"
"while (*str) {"
"*buf++=*str++;"
"}"
;
}
public final class FileDescriptor {
public static final FileDescriptor in=new FileDescriptor(0);
public static final FileDescriptor out=new FileDescriptor(1);
public static final FileDescriptor err=new FileDescriptor(2);
private FileDescriptor(int fd) { this.fd=(short) fd; }
private static byte[] stringToBytes(String s)
throws NullPointerException
{
int len=s.length();
byte[] buf=new byte[len+1];
s.getBytes(0, len, buf, 0);
buf[len]=0;
return buf;
}
static FileDescriptor openread(String name)
throws NullPointerException, IOException
{
int fd=openread(stringToBytes(name));
if (fd<0) Errno.throwError(name);
return new FileDescriptor(fd);
}
static FileDescriptor openwrite(String name)
throws NullPointerException, IOException
{
int fd=openwrite(stringToBytes(name));
if (fd<0) Errno.throwError(name);
return new FileDescriptor(fd);
}
static FileDescriptor openreadwrite(String name)
throws NullPointerException, IOException
{
int fd=openreadwrite(stringToBytes(name));
if (fd<0) Errno.throwError(name);
return new FileDescriptor(fd);
}
private short fd;
int fd() { return fd; }
public boolean valid() { return fd>0; }
native int read(byte[] buffer, int off, int len)
"return read(this->f_fd, &l_buffer->component[l_off], l_len);"
;
native int write(byte[] buffer, int off, int len)
"return write(this->f_fd, &l_buffer->component[l_off], l_len);"
;
native int close()
"return close(this->f_fd);"
;
private static native int openread(byte[] name)
"return open(&l_name->component[0], O_RDONLY, 0);"
;
private static native int openwrite(byte[] name)
"return open(&l_name->component[0], O_WRONLY|O_CREAT, 0666);"
;
private static native int openreadwrite(byte[] name)
"return open(&l_name->component[0], O_RDWR|O_CREAT, 0666);"
;
}
|