quick sketch of java/io/*

This commit is contained in:
Joel Dice 2007-07-24 18:34:45 -06:00
parent f56fda9af6
commit 97aaa419b4
15 changed files with 327 additions and 26 deletions

View File

@ -0,0 +1,68 @@
package java.io;
public class BufferedInputStream extends InputStream {
private final InputStream in;
private final byte[] buffer;
private int position;
private int limit;
public BufferedInputStream(InputStream in, int size) {
this.in = in;
this.buffer = new byte[size];
}
public BufferedInputStream(InputStream in) {
this(in, 32);
}
private void fill() throws IOException {
position = 0;
limit = in.read(buffer);
}
public int read() throws IOException {
if (position >= limit) {
fill();
if (limit == -1) {
return -1;
}
}
return buffer[position++];
}
public int read(byte[] b, int offset, int length) throws IOException {
int count = 0;
if (position < limit) {
int remaining = limit - position;
if (remaining > length) {
remaining = length;
}
System.arraycopy(buffer, position, b, offset, remaining);
count += remaining;
position += remaining;
offset += remaining;
length -= remaining;
}
if (length > 0) {
int c = in.read(b, offset, length);
if (c == -1) {
if (count == 0) {
count = -1;
}
} else {
count += c;
}
}
return count;
}
public void close() throws IOException {
in.close();
}
}

View File

@ -0,0 +1,51 @@
package java.io;
public class BufferedOutputStream extends OutputStream {
private final OutputStream out;
private final byte[] buffer;
private int position;
public BufferedOutputStream(OutputStream out, int size) {
this.out = out;
this.buffer = new byte[size];
}
public BufferedOutputStream(OutputStream out) {
this(out, 4096);
}
private void drain() throws IOException {
if (position > 0) {
out.write(buffer, 0, position);
position = 0;
}
}
public void write(int c) throws IOException {
if (position >= buffer.length) {
drain();
}
buffer[position++] = (byte) (c & 0xFF);
}
public void write(byte[] b, int offset, int length) throws IOException {
if (length > buffer.length - position) {
drain();
out.write(b, offset, length);
} else {
System.arraycopy(b, offset, buffer, position, length);
position += length;
}
}
public void flush() throws IOException {
drain();
out.flush();
}
public void close() throws IOException {
flush();
out.close();
}
}

View File

@ -0,0 +1,17 @@
package java.io;
public 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 final int value;
private FileDescriptor(int value) {
this.value = value;
}
public FileDescriptor() {
this(-1);
}
}

View File

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

View File

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

View File

@ -0,0 +1,19 @@
package java.io;
public class IOException extends Exception {
public IOException(String message, Throwable cause) {
super(message, cause);
}
public IOException(String message) {
this(message, null);
}
public IOException(Throwable cause) {
this(null, cause);
}
public IOException() {
this(null, null);
}
}

View File

@ -0,0 +1,27 @@
package java.io;
public abstract class InputStream {
public abstract int read() throws IOException;
public int read(byte[] buffer) throws IOException {
return read(buffer, 0, buffer.length);
}
public int read(byte[] buffer, int offset, int length) throws IOException {
for (int i = 0; i < length; ++i) {
int c = read();
if (c == -1) {
if (i == 0) {
return -1;
} else {
return i;
}
} else {
buffer[offset + i] = (byte) (c & 0xFF);
}
}
return length;
}
public void close() throws IOException { }
}

View File

@ -0,0 +1,19 @@
package java.io;
public abstract class OutputStream {
public abstract void write(int c) throws IOException;
public void write(byte[] buffer) throws IOException {
write(buffer, 0, buffer.length);
}
public void write(byte[] buffer, int offset, int length) throws IOException {
for (int i = 0; i < length; ++i) {
write(buffer[offset + i]);
}
}
public void flush() throws IOException { }
public void close() throws IOException { }
}

View File

@ -0,0 +1,51 @@
package java.io;
public class PrintStream extends OutputStream {
private static final byte[] newline
= System.getProperty("line.separator").getBytes();
private final OutputStream out;
private final boolean autoFlush;
public PrintStream(OutputStream out, boolean autoFlush) {
this.out = out;
this.autoFlush = true;
}
public PrintStream(OutputStream out) {
this(out, false);
}
public synchronized void print(String s) {
try {
out.write(s.getBytes());
if (autoFlush) flush();
} catch (IOException e) { }
}
public synchronized void println(String s) {
try {
out.write(s.getBytes());
out.write(newline);
if (autoFlush) flush();
} catch (IOException e) { }
}
public void write(int c) throws IOException {
out.write(c);
if (autoFlush && c == '\n') flush();
}
public void write(byte[] buffer, int offset, int length) throws IOException {
out.write(buffer, offset, length);
if (autoFlush) flush();
}
public void flush() throws IOException {
out.flush();
}
public void close() throws IOException {
out.close();
}
}

View File

@ -130,6 +130,30 @@ public final class String implements Comparable<String> {
}
}
public byte[] getBytes() {
byte[] b = new byte[length];
getBytes(0, length, b, 0);
return b;
}
public void getBytes(int srcOffset, int srcLength,
byte[] dst, int dstOffset)
{
if (srcOffset + srcLength > length) {
throw new IndexOutOfBoundsException();
}
if (data instanceof char[]) {
char[] src = (char[]) data;
for (int i = 0; i < srcLength; ++i) {
dst[i + dstOffset] = (byte) src[i + offset + srcOffset];
}
} else {
byte[] src = (byte[]) data;
System.arraycopy(src, offset + srcOffset, dst, dstOffset, srcLength);
}
}
public void getChars(int srcOffset, int srcLength,
char[] dst, int dstOffset)
{

View File

@ -5,17 +5,6 @@
#undef JNIEXPORT
#define JNIEXPORT __attribute__ ((visibility("default")))
extern "C" JNIEXPORT void JNICALL
Java_java_lang_System_00024Output_print(JNIEnv* e, jobject, jstring s)
{
jboolean isCopy;
const char* chars = e->GetStringUTFChars(s, &isCopy);
if (chars) {
printf("%s", chars);
}
e->ReleaseStringUTFChars(s, chars);
}
extern "C" JNIEXPORT jstring JNICALL
Java_java_lang_System_getProperty(JNIEnv* e, jstring key)
{

View File

@ -1,13 +1,27 @@
package java.lang;
public abstract class System {
public static final Output out = new Output();
public static final Output err = out;
import java.io.PrintStream;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileDescriptor;
public abstract class System {
static {
loadLibrary("natives");
}
public static final PrintStream out = new PrintStream
(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)), true);
public static final PrintStream err = new PrintStream
(new BufferedOutputStream(new FileOutputStream(FileDescriptor.err)), true);
public static final InputStream in
= new BufferedInputStream(new FileInputStream(FileDescriptor.in));
public static native void arraycopy(Object src, int srcOffset, Object dst,
int dstOffset, int length);
@ -26,13 +40,4 @@ public abstract class System {
public static void exit(int code) {
Runtime.getRuntime().exit(code);
}
public static class Output {
public synchronized native void print(String s);
public synchronized void println(String s) {
print(s);
print(getProperty("line.separator"));
}
}
}

View File

@ -16,7 +16,7 @@ src = src
classpath = classpath
test = test
input = $(cls)/Threads.class
input = $(cls)/Hello.class
cxx = g++
cc = gcc

View File

@ -22,7 +22,7 @@
namespace vm {
const bool Verbose = false;
const bool DebugRun = false;
const bool DebugRun = true;
const bool DebugStack = false;
const bool DebugMonitors = false;

View File

@ -5,7 +5,7 @@ public class Reflection {
public static void main(String[] args) throws Exception {
Class system = Class.forName("java.lang.System");
Field out = system.getDeclaredField("out");
Class output = Class.forName("java.lang.System$Output");
Class output = Class.forName("java.io.PrintStream");
Method println = output.getDeclaredMethod("println", String.class);
println.invoke(out.get(null), "Hello, World!");