mirror of
https://github.com/corda/corda.git
synced 2024-12-28 16:58:55 +00:00
quick sketch of java/io/*
This commit is contained in:
parent
f56fda9af6
commit
97aaa419b4
68
classpath/java/io/BufferedInputStream.java
Normal file
68
classpath/java/io/BufferedInputStream.java
Normal 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();
|
||||
}
|
||||
}
|
51
classpath/java/io/BufferedOutputStream.java
Normal file
51
classpath/java/io/BufferedOutputStream.java
Normal 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();
|
||||
}
|
||||
}
|
17
classpath/java/io/FileDescriptor.java
Normal file
17
classpath/java/io/FileDescriptor.java
Normal 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);
|
||||
}
|
||||
}
|
15
classpath/java/io/FileInputStream.java
Normal file
15
classpath/java/io/FileInputStream.java
Normal 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;
|
||||
}
|
16
classpath/java/io/FileOutputStream.java
Normal file
16
classpath/java/io/FileOutputStream.java
Normal 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;
|
||||
}
|
19
classpath/java/io/IOException.java
Normal file
19
classpath/java/io/IOException.java
Normal 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);
|
||||
}
|
||||
}
|
27
classpath/java/io/InputStream.java
Normal file
27
classpath/java/io/InputStream.java
Normal 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 { }
|
||||
}
|
19
classpath/java/io/OutputStream.java
Normal file
19
classpath/java/io/OutputStream.java
Normal 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 { }
|
||||
}
|
51
classpath/java/io/PrintStream.java
Normal file
51
classpath/java/io/PrintStream.java
Normal 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();
|
||||
}
|
||||
}
|
@ -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)
|
||||
{
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
2
makefile
2
makefile
@ -16,7 +16,7 @@ src = src
|
||||
classpath = classpath
|
||||
test = test
|
||||
|
||||
input = $(cls)/Threads.class
|
||||
input = $(cls)/Hello.class
|
||||
|
||||
cxx = g++
|
||||
cc = gcc
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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!");
|
||||
|
Loading…
Reference in New Issue
Block a user