more classpath progress

This commit is contained in:
Joel Dice 2007-07-28 19:29:01 -06:00
parent c96a4a5b39
commit a9e10d1c7f
22 changed files with 550 additions and 13 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

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

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -0,0 +1,3 @@
package java.io;
public interface Serializable { }

View File

@ -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 { }
}

View File

@ -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 { }
}

View File

@ -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;
}

View File

@ -10,10 +10,18 @@ public final class String implements Comparable<String> {
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<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)
{
@ -186,6 +188,12 @@ public final class String implements Comparable<String> {
}
}
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<String> {
}
}
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();

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -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) {

View File

@ -0,0 +1,11 @@
package java.net;
public class MalformedURLException extends Exception {
public MalformedURLException(String message) {
super(message);
}
public MalformedURLException() {
this(null);
}
}

View File

@ -0,0 +1,5 @@
package java.net;
public class URL {
}