diff --git a/classpath/java/io/BufferedReader.java b/classpath/java/io/BufferedReader.java new file mode 100644 index 0000000000..5bd25e9051 --- /dev/null +++ b/classpath/java/io/BufferedReader.java @@ -0,0 +1,57 @@ +package java.io; + +public class BufferedReader extends Reader { + private final Reader in; + private final char[] buffer; + private int position; + private int limit; + + public BufferedReader(Reader in, int bufferSize) { + this.in = in; + this.buffer = new char[bufferSize]; + } + + protected BufferedReader(Reader in) { + this(in, 32); + } + + private void fill() throws IOException { + position = 0; + limit = in.read(buffer); + } + + public int read(char[] 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(); + } +} diff --git a/classpath/java/io/BufferedWriter.java b/classpath/java/io/BufferedWriter.java new file mode 100644 index 0000000000..4f246216e5 --- /dev/null +++ b/classpath/java/io/BufferedWriter.java @@ -0,0 +1,43 @@ +package java.io; + +public class BufferedWriter extends Writer { + private final Writer out; + private final char[] buffer; + private int position; + + public BufferedWriter(Writer out, int size) { + this.out = out; + this.buffer = new char[size]; + } + + public BufferedWriter(Writer out) { + this(out, 4096); + } + + private void drain() throws IOException { + if (position > 0) { + out.write(buffer, 0, position); + position = 0; + } + } + + public void write(char[] 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(); + } +} diff --git a/classpath/java/io/FileReader.java b/classpath/java/io/FileReader.java new file mode 100644 index 0000000000..3771453805 --- /dev/null +++ b/classpath/java/io/FileReader.java @@ -0,0 +1,29 @@ +package java.io; + +public class FileReader extends Reader { + private final Reader in; + + public FileReader(FileInputStream in) { + this.in = new InputStreamReader(in); + } + + public FileReader(FileDescriptor fd) { + this(new FileInputStream(fd)); + } + + public FileReader(String path) throws IOException { + this(new FileInputStream(path)); + } + + public FileReader(File file) throws IOException { + this(new FileInputStream(file)); + } + + public int read(char[] b, int offset, int length) throws IOException { + return in.read(b, offset, length); + } + + public void close() throws IOException { + in.close(); + } +} diff --git a/classpath/java/io/FileWriter.java b/classpath/java/io/FileWriter.java new file mode 100644 index 0000000000..ab1978762a --- /dev/null +++ b/classpath/java/io/FileWriter.java @@ -0,0 +1,33 @@ +package java.io; + +public class FileWriter extends Writer { + private final Writer out; + + private FileWriter(FileOutputStream out) { + this.out = new OutputStreamWriter(out); + } + + public FileWriter(FileDescriptor fd) { + this(new FileOutputStream(fd)); + } + + public FileWriter(String path) throws IOException { + this(new FileOutputStream(path)); + } + + public FileWriter(File file) throws IOException { + this(new FileOutputStream(file)); + } + + public void write(char[] b, int offset, int length) throws IOException { + out.write(b, offset, length); + } + + public void flush() throws IOException { + out.flush(); + } + + public void close() throws IOException { + out.close(); + } +} diff --git a/classpath/java/io/InputStreamReader.java b/classpath/java/io/InputStreamReader.java new file mode 100644 index 0000000000..a318289179 --- /dev/null +++ b/classpath/java/io/InputStreamReader.java @@ -0,0 +1,22 @@ +package java.io; + +public class InputStreamReader extends Reader { + private final InputStream in; + + public InputStreamReader(InputStream in) { + this.in = in; + } + + public int read(char[] b, int offset, int length) throws IOException { + byte[] buffer = new byte[length]; + int c = in.read(buffer); + for (int i = 0; i < c; ++i) { + b[i + offset] = (char) buffer[i]; + } + return c; + } + + public void close() throws IOException { + in.close(); + } +} diff --git a/classpath/java/io/LineNumberReader.java b/classpath/java/io/LineNumberReader.java new file mode 100644 index 0000000000..b3876f37aa --- /dev/null +++ b/classpath/java/io/LineNumberReader.java @@ -0,0 +1,31 @@ +package java.io; + +public class LineNumberReader extends BufferedReader { + private int line; + + public LineNumberReader(Reader in, int bufferSize) { + super(in, bufferSize); + } + + protected LineNumberReader(Reader in) { + super(in); + } + + public int getLineNumber() { + return line; + } + + public void setLineNumber(int v) { + line = v; + } + + public int read(char[] b, int offset, int length) throws IOException { + int c = super.read(b, offset, length); + for (int i = 0; i < c; ++i) { + if (b[i] == '\n') { + ++ line; + } + } + return c; + } +} diff --git a/classpath/java/io/ObjectInputStream.java b/classpath/java/io/ObjectInputStream.java new file mode 100644 index 0000000000..d0adaf0ad5 --- /dev/null +++ b/classpath/java/io/ObjectInputStream.java @@ -0,0 +1,21 @@ +package java.io; + +public class ObjectInputStream extends InputStream { + private final InputStream in; + + public ObjectInputStream(InputStream in) { + this.in = in; + } + + public int read() throws IOException { + return in.read(); + } + + public int read(byte[] b, int offset, int length) throws IOException { + return in.read(b, offset, length); + } + + public void close() throws IOException { + in.close(); + } +} diff --git a/classpath/java/io/ObjectOutputStream.java b/classpath/java/io/ObjectOutputStream.java new file mode 100644 index 0000000000..9a57d10bcd --- /dev/null +++ b/classpath/java/io/ObjectOutputStream.java @@ -0,0 +1,26 @@ +package java.io; + +public class ObjectOutputStream extends OutputStream { + private final OutputStream out; + + public ObjectOutputStream(OutputStream out) { + this.out = out; + } + + public void write(int c) throws IOException { + out.write(c); + } + + public void write(byte[] b, int offset, int length) throws IOException { + out.write(b, offset, length); + } + + public void flush() throws IOException { + out.flush(); + } + + public void close() throws IOException { + out.close(); + } + +} diff --git a/classpath/java/io/ObjectStreamException.java b/classpath/java/io/ObjectStreamException.java new file mode 100644 index 0000000000..f08ebed675 --- /dev/null +++ b/classpath/java/io/ObjectStreamException.java @@ -0,0 +1,11 @@ +package java.io; + +public class ObjectStreamException extends IOException { + public ObjectStreamException(String message) { + super(message); + } + + public ObjectStreamException() { + this(null); + } +} diff --git a/classpath/java/io/OutputStreamWriter.java b/classpath/java/io/OutputStreamWriter.java new file mode 100644 index 0000000000..827206db03 --- /dev/null +++ b/classpath/java/io/OutputStreamWriter.java @@ -0,0 +1,25 @@ +package java.io; + +public class OutputStreamWriter extends Writer { + private final OutputStream out; + + public OutputStreamWriter(OutputStream out) { + this.out = out; + } + + public void write(char[] b, int offset, int length) throws IOException { + byte[] buffer = new byte[length]; + for (int i = 0; i < length; ++i) { + buffer[i] = (byte) b[i + offset]; + } + out.write(buffer); + } + + public void flush() throws IOException { + out.flush(); + } + + public void close() throws IOException { + out.close(); + } +} diff --git a/classpath/java/io/PrintWriter.java b/classpath/java/io/PrintWriter.java new file mode 100644 index 0000000000..38659d3155 --- /dev/null +++ b/classpath/java/io/PrintWriter.java @@ -0,0 +1,46 @@ +package java.io; + +public class PrintWriter extends Writer { + private static final char[] newline + = System.getProperty("line.separator").toCharArray(); + + private final Writer out; + private final boolean autoFlush; + + public PrintWriter(Writer out, boolean autoFlush) { + this.out = out; + this.autoFlush = true; + } + + public PrintWriter(Writer out) { + this(out, false); + } + + public synchronized void print(String s) { + try { + out.write(s.toCharArray()); + if (autoFlush) flush(); + } catch (IOException e) { } + } + + public synchronized void println(String s) { + try { + out.write(s.toCharArray()); + out.write(newline); + if (autoFlush) flush(); + } catch (IOException e) { } + } + + public void write(char[] 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(); + } +} diff --git a/classpath/java/io/Reader.java b/classpath/java/io/Reader.java new file mode 100644 index 0000000000..39737cffdb --- /dev/null +++ b/classpath/java/io/Reader.java @@ -0,0 +1,22 @@ +package java.io; + +public abstract class Reader { + public int read() throws IOException { + char[] buffer = new char[1]; + int c = read(buffer); + if (c <= 0) { + return -1; + } else { + return (int) buffer[0]; + } + } + + public int read(char[] buffer) throws IOException { + return read(buffer, 0, buffer.length); + } + + public abstract int read(char[] buffer, int offset, int length) + throws IOException; + + public abstract void close() throws IOException; +} diff --git a/classpath/java/io/Serializable.java b/classpath/java/io/Serializable.java new file mode 100644 index 0000000000..2dab37587f --- /dev/null +++ b/classpath/java/io/Serializable.java @@ -0,0 +1,3 @@ +package java.io; + +public interface Serializable { } diff --git a/classpath/java/io/StringReader.java b/classpath/java/io/StringReader.java new file mode 100644 index 0000000000..f2e2b82424 --- /dev/null +++ b/classpath/java/io/StringReader.java @@ -0,0 +1,23 @@ +package java.io; + +public class StringReader extends Reader { + private final String in; + private int position = 0; + + public StringReader(String in) { + this.in = in; + } + + public int read(char[] b, int offset, int length) throws IOException { + if (length > in.length() - position) { + length = in.length() - position; + if (length <= 0) { + return -1; + } + } + in.getChars(position, length, b, offset); + return length; + } + + public void close() throws IOException { } +} diff --git a/classpath/java/io/StringWriter.java b/classpath/java/io/StringWriter.java new file mode 100644 index 0000000000..0e335d7b96 --- /dev/null +++ b/classpath/java/io/StringWriter.java @@ -0,0 +1,17 @@ +package java.io; + +public class StringWriter extends Writer { + private final StringBuilder out = new StringBuilder(); + + public void write(char[] b, int offset, int length) throws IOException { + out.append(new String(b, offset, length)); + } + + public String toString() { + return out.toString(); + } + + public void flush() throws IOException { } + + public void close() throws IOException { } +} diff --git a/classpath/java/io/Writer.java b/classpath/java/io/Writer.java new file mode 100644 index 0000000000..98246ad0e1 --- /dev/null +++ b/classpath/java/io/Writer.java @@ -0,0 +1,19 @@ +package java.io; + +public abstract class Writer { + public void write(int c) throws IOException { + char[] buffer = new char[] { (char) c }; + write(buffer); + } + + public void write(char[] buffer) throws IOException { + write(buffer, 0, buffer.length); + } + + public abstract void write(char[] buffer, int offset, int length) + throws IOException; + + public abstract void flush() throws IOException; + + public abstract void close() throws IOException; +} diff --git a/classpath/java/lang/String.java b/classpath/java/lang/String.java index be3c72ab5e..f8153be92f 100644 --- a/classpath/java/lang/String.java +++ b/classpath/java/lang/String.java @@ -10,10 +10,18 @@ public final class String implements Comparable { this((Object) data, offset, length, copy); } + public String(char[] data, int offset, int length) { + this(data, offset, length, true); + } + public String(byte[] data, int offset, int length, boolean copy) { this((Object) data, offset, length, copy); } + public String(byte[] data, int offset, int length) { + this(data, offset, length, true); + } + private String(Object data, int offset, int length, boolean copy) { if (copy) { Object c; @@ -162,12 +170,6 @@ public final class String implements Comparable { } } - 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) { @@ -186,6 +188,12 @@ public final class String implements Comparable { } } + public byte[] getBytes() { + byte[] b = new byte[length]; + getBytes(0, length, b, 0); + return b; + } + public void getChars(int srcOffset, int srcLength, char[] dst, int dstOffset) { @@ -204,6 +212,12 @@ public final class String implements Comparable { } } + public char[] toCharArray() { + char[] b = new char[length]; + getChars(0, length, b, 0); + return b; + } + public char charAt(int index) { if (index < 0 || index > length) { throw new IndexOutOfBoundsException(); diff --git a/classpath/java/lang/StringBuffer.java b/classpath/java/lang/StringBuffer.java new file mode 100644 index 0000000000..080826efd0 --- /dev/null +++ b/classpath/java/lang/StringBuffer.java @@ -0,0 +1,39 @@ +package java.lang; + +public class StringBuffer { + private final StringBuilder sb = new StringBuilder(); + + public synchronized StringBuffer append(String s) { + sb.append(s); + return this; + } + + public synchronized StringBuffer append(Object o) { + sb.append(o); + return this; + } + + public synchronized StringBuffer append(int v) { + sb.append(v); + return this; + } + + public synchronized StringBuffer append(long v) { + sb.append(v); + return this; + } + + public synchronized int length() { + return sb.length(); + } + + public synchronized void getChars(int srcOffset, int srcLength, char[] dst, + int dstOffset) + { + sb.getChars(srcOffset, srcLength, dst, dstOffset); + } + + public synchronized String toString() { + return sb.toString(); + } +} diff --git a/classpath/java/lang/StringBuilder.java b/classpath/java/lang/StringBuilder.java index a734bab1a6..68fbbb71d5 100644 --- a/classpath/java/lang/StringBuilder.java +++ b/classpath/java/lang/StringBuilder.java @@ -22,13 +22,40 @@ public class StringBuilder { return append(String.valueOf(v)); } - public String toString() { - char[] array = new char[length]; + public int length() { + return length; + } + + public void getChars(int srcOffset, int srcLength, char[] dst, int dstOffset) + { + if (srcOffset + srcLength > length) { + throw new IndexOutOfBoundsException(); + } + int index = length; for (Cell c = chain; c != null; c = c.next) { - index -= c.value.length(); - c.value.getChars(0, c.value.length(), array, index); - } + int start = index - c.value.length(); + int end = index; + index = start; + + if (start < srcOffset) { + start = srcOffset; + } + + if (end > srcOffset + srcLength) { + end = srcOffset + srcLength; + } + + if (start < end) { + c.value.getChars(start - index, end - start, + dst, dstOffset + (start - srcOffset)); + } + } + } + + public String toString() { + char[] array = new char[length]; + getChars(0, length, array, 0); return new String(array, 0, length, false); } diff --git a/classpath/java/lang/Throwable.java b/classpath/java/lang/Throwable.java index 53ce5eac62..60b7595b85 100644 --- a/classpath/java/lang/Throwable.java +++ b/classpath/java/lang/Throwable.java @@ -1,5 +1,8 @@ package java.lang; +import java.io.PrintStream; +import java.io.PrintWriter; + public class Throwable { private String message; private Object trace; @@ -42,10 +45,20 @@ public class Throwable { return (StackTraceElement[]) trace; } - public void printStackTrace() { + public void printStackTrace(PrintStream out) { StringBuilder sb = new StringBuilder(); printStackTrace(sb, System.getProperty("line.separator")); - System.err.print(sb.toString()); + out.print(sb.toString()); + } + + public void printStackTrace(PrintWriter out) { + StringBuilder sb = new StringBuilder(); + printStackTrace(sb, System.getProperty("line.separator")); + out.print(sb.toString()); + } + + public void printStackTrace() { + printStackTrace(System.err); } private void printStackTrace(StringBuilder sb, String nl) { diff --git a/classpath/java/net/MalformedURLException.java b/classpath/java/net/MalformedURLException.java new file mode 100644 index 0000000000..7c10a3568a --- /dev/null +++ b/classpath/java/net/MalformedURLException.java @@ -0,0 +1,11 @@ +package java.net; + +public class MalformedURLException extends Exception { + public MalformedURLException(String message) { + super(message); + } + + public MalformedURLException() { + this(null); + } +} diff --git a/classpath/java/net/URL.java b/classpath/java/net/URL.java new file mode 100644 index 0000000000..787b239ec0 --- /dev/null +++ b/classpath/java/net/URL.java @@ -0,0 +1,5 @@ +package java.net; + +public class URL { + +}