a static jni method takes the jclass for that method as its second argument; simplify pad() and divide(), and rename divide() to ceiling(); sketch FileInputStream.cpp and FileOutputStream.cpp

This commit is contained in:
Joel Dice
2007-07-25 18:48:28 -06:00
parent 38a982c7dd
commit 9ab88ef619
14 changed files with 233 additions and 49 deletions

View File

@ -5,7 +5,7 @@ public class FileDescriptor {
public static final FileDescriptor out = new FileDescriptor(1);
public static final FileDescriptor err = new FileDescriptor(2);
private final int value;
final int value;
private FileDescriptor(int value) {
this.value = value;

View File

@ -0,0 +1,79 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "jni.h"
#undef JNIEXPORT
#define JNIEXPORT __attribute__ ((visibility("default")))
#ifdef WIN32
# include <io.h>
# define CLOSE _close
# define READ _read
#else
# include <unistd.h>
# define CLOSE close
# define READ read
#endif
namespace {
int
doRead(JNIEnv* e, jint fd, jbyte* data, jint length)
{
int r = READ(fd, data, length);
if (r > 0) {
return r;
} else if (r == 0) {
return -1;
} else {
e->ThrowNew(e->FindClass("java/lang/IOException"), strerror(errno));
return 0;
}
}
} // namespace
extern "C" JNIEXPORT jint JNICALL
Java_java_io_FileInputStream_read__I(JNIEnv* e, jclass, jint fd)
{
jbyte data;
int r = doRead(e, fd, &data, 1);
if (r <= 0) {
return -1;
} else {
return data;
}
}
extern "C" JNIEXPORT jint JNICALL
Java_java_io_FileInputStream_read__I_3BII
(JNIEnv* e, jclass, jint fd, jbyteArray b, jint offset, jint length)
{
jbyte* data = static_cast<jbyte*>(malloc(length));
if (data == 0) {
e->ThrowNew(e->FindClass("java/lang/OutOfMemoryError"), 0);
return 0;
}
int r = doRead(e, fd, data, length);
e->SetByteArrayRegion(b, offset, length, data);
free(data);
return r;
}
extern "C" JNIEXPORT void JNICALL
Java_java_io_FileInputStream_close(JNIEnv* e, jclass, jint fd)
{
int r = CLOSE(fd);
if (r == -1) {
e->ThrowNew(e->FindClass("java/lang/IOException"), strerror(errno));
}
}

View File

@ -1,15 +1,28 @@
package java.io;
public class FileInputStream extends InputStream {
private final FileDescriptor fd;
private final int fd;
public FileInputStream(FileDescriptor fd) {
this.fd = fd;
this.fd = fd.value;
}
public native int read() throws IOException;
private static native int read(int fd) throws IOException;
public native int read(byte[] b, int offset, int length) throws IOException;
private static native int read(int fd, byte[] b, int offset, int length)
throws IOException;
public native void close() throws IOException;
public static native void close(int fd) throws IOException;
public int read() throws IOException {
return read(fd);
}
public int read(byte[] b, int offset, int length) throws IOException {
return read(fd, b, offset, length);
}
public void close() throws IOException {
close(fd);
}
}

View File

@ -0,0 +1,69 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "jni.h"
#undef JNIEXPORT
#define JNIEXPORT __attribute__ ((visibility("default")))
#ifdef WIN32
# include <io.h>
# define CLOSE _close
# define WRITE _write
#else
# include <unistd.h>
# define CLOSE close
# define WRITE write
#endif
namespace {
void
doWrite(JNIEnv* e, jint fd, const jbyte* data, jint length)
{
int r = WRITE(fd, data, length);
if (r != length) {
e->ThrowNew(e->FindClass("java/lang/IOException"), strerror(errno));
}
}
} // namespace
extern "C" JNIEXPORT void JNICALL
Java_java_io_FileOutputStream_write__II(JNIEnv* e, jclass, jint fd, jint c)
{
jbyte data = c;
doWrite(e, fd, &data, 1);
}
extern "C" JNIEXPORT void JNICALL
Java_java_io_FileOutputStream_write__I_3BII
(JNIEnv* e, jclass, jint fd, jbyteArray b, jint offset, jint length)
{
jbyte* data = static_cast<jbyte*>(malloc(length));
if (data == 0) {
e->ThrowNew(e->FindClass("java/lang/OutOfMemoryError"), 0);
return;
}
e->GetByteArrayRegion(b, offset, length, data);
if (not e->ExceptionCheck()) {
doWrite(e, fd, data, length);
}
free(data);
}
extern "C" JNIEXPORT void JNICALL
Java_java_io_FileOutputStream_close(JNIEnv* e, jclass, jint fd)
{
int r = CLOSE(fd);
if (r == -1) {
e->ThrowNew(e->FindClass("java/lang/IOException"), strerror(errno));
}
}

View File

@ -1,16 +1,28 @@
package java.io;
public class FileOutputStream extends OutputStream {
private final FileDescriptor fd;
private final int fd;
public FileOutputStream(FileDescriptor fd) {
this.fd = fd;
this.fd = fd.value;
}
public native void write(int c) throws IOException;
public static native void write(int fd, int c) throws IOException;
public native void write(byte[] b, int offset, int length)
public static native void write(int fd, byte[] b, int offset, int length)
throws IOException;
public native void close() throws IOException;
public static native void close(int fd) throws IOException;
public void write(int c) throws IOException {
write(fd, c);
}
public void write(byte[] b, int offset, int length) throws IOException {
write(fd, b, offset, length);
}
public void close() throws IOException {
close(fd);
}
}

View File

@ -1,4 +1,3 @@
#include "stdio.h"
#include "string.h"
#include "jni.h"
@ -6,7 +5,7 @@
#define JNIEXPORT __attribute__ ((visibility("default")))
extern "C" JNIEXPORT jstring JNICALL
Java_java_lang_System_getProperty(JNIEnv* e, jstring key)
Java_java_lang_System_getProperty(JNIEnv* e, jclass, jstring key)
{
jstring value = 0;