classpath progress

This commit is contained in:
Joel Dice 2007-07-28 20:15:45 -06:00
parent a9e10d1c7f
commit 51943427ad
9 changed files with 120 additions and 11 deletions

View File

@ -20,6 +20,27 @@ public class BufferedReader extends Reader {
limit = in.read(buffer);
}
public String readLine() throws IOException {
StringBuilder sb = new StringBuilder();
while (true) {
if (position >= limit) {
fill();
}
if (position >= limit) {
return sb.toString();
}
for (int i = position; i < limit; ++i) {
if (buffer[i] == '\n') {
sb.append(buffer, position, i);
position = i + 1;
return sb.toString();
}
}
}
}
public int read(char[] b, int offset, int length) throws IOException {
int count = 0;

View File

@ -19,7 +19,6 @@ public class PrintStream extends OutputStream {
public synchronized void print(String s) {
try {
out.write(s.getBytes());
if (autoFlush) flush();
} catch (IOException e) { }
}

View File

@ -19,7 +19,6 @@ public class PrintWriter extends Writer {
public synchronized void print(String s) {
try {
out.write(s.toCharArray());
if (autoFlush) flush();
} catch (IOException e) { }
}
@ -30,7 +29,7 @@ public class PrintWriter extends Writer {
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();

View File

@ -4,7 +4,7 @@ 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));
out.append(b, offset, length);
}
public String toString() {

View File

@ -10,6 +10,16 @@ public abstract class Writer {
write(buffer, 0, buffer.length);
}
public void write(String s) throws IOException {
write(s.toCharArray());
}
public void write(String s, int offset, int length) throws IOException {
char[] b = new char[length];
s.getChars(offset, length, b, 0);
write(b);
}
public abstract void write(char[] buffer, int offset, int length)
throws IOException;

View File

@ -173,7 +173,7 @@ public final class String implements Comparable<String> {
public void getBytes(int srcOffset, int srcLength,
byte[] dst, int dstOffset)
{
if (srcOffset + srcLength > length) {
if (srcOffset < 0 || srcOffset + srcLength > length) {
throw new IndexOutOfBoundsException();
}
@ -197,7 +197,7 @@ public final class String implements Comparable<String> {
public void getChars(int srcOffset, int srcLength,
char[] dst, int dstOffset)
{
if (srcOffset + srcLength > length) {
if (srcOffset < 0 || srcOffset + srcLength > length) {
throw new IndexOutOfBoundsException();
}

View File

@ -23,10 +23,19 @@ public class StringBuffer {
return this;
}
public synchronized StringBuffer deleteCharAt(int i) {
sb.deleteCharAt(i);
return this;
}
public synchronized int length() {
return sb.length();
}
public synchronized void setLength(int v) {
sb.setLength(v);
}
public synchronized void getChars(int srcOffset, int srcLength, char[] dst,
int dstOffset)
{

View File

@ -5,11 +5,17 @@ public class StringBuilder {
private int length;
public StringBuilder append(String s) {
chain = new Cell(s, chain);
length += s.length();
if (s.length() > 0) {
chain = new Cell(s, chain);
length += s.length();
}
return this;
}
public StringBuilder append(char[] b, int offset, int length) {
return append(new String(b, offset, length));
}
public StringBuilder append(Object o) {
return append(o == null ? "null" : o.toString());
}
@ -22,13 +28,75 @@ public class StringBuilder {
return append(String.valueOf(v));
}
public StringBuilder deleteCharAt(int i) {
if (i < 0 || i >= length) {
throw new IndexOutOfBoundsException();
}
int index = length;
-- length;
Cell p = null;
for (Cell c = chain; c != null; c = c.next) {
int start = index - c.value.length();
index = start;
if (i >= start) {
if (c.value.length() == 1) {
if (p == null) {
chain = c.next;
} else {
p.next = c.next;
}
} else if (i == start) {
c.value = c.value.substring(1);
} else if (i == start + c.value.length() - 1) {
c.value = c.value.substring(0, c.value.length() - 1);
} else {
c.value = c.value.substring(i + 1, c.value.length());
c.next = new Cell(c.value.substring(0, i), c.next);
}
break;
}
}
return this;
}
public int length() {
return length;
}
public void setLength(int v) {
if (v < 0) {
throw new IndexOutOfBoundsException();
}
if (v == 0) {
length = 0;
chain = null;
return;
}
int index = length;
length = v;
for (Cell c = chain; c != null; c = c.next) {
int start = index - c.value.length();
if (v > start) {
if (v < index) {
c.value = c.value.substring(0, v - start);
}
break;
}
chain = c.next;
index = start;
}
}
public void getChars(int srcOffset, int srcLength, char[] dst, int dstOffset)
{
if (srcOffset + srcLength > length) {
if (srcOffset < 0 || srcOffset + srcLength > length) {
throw new IndexOutOfBoundsException();
}
@ -60,8 +128,8 @@ public class StringBuilder {
}
private static class Cell {
public final String value;
public final Cell next;
public String value;
public Cell next;
public Cell(String value, Cell next) {
this.value = value;

View File

@ -2,6 +2,7 @@ package java.lang;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.IOException;
public class Throwable {
private String message;
@ -49,12 +50,14 @@ public class Throwable {
StringBuilder sb = new StringBuilder();
printStackTrace(sb, System.getProperty("line.separator"));
out.print(sb.toString());
try { out.flush(); } catch (IOException e) { }
}
public void printStackTrace(PrintWriter out) {
StringBuilder sb = new StringBuilder();
printStackTrace(sb, System.getProperty("line.separator"));
out.print(sb.toString());
try { out.flush(); } catch (IOException e) { }
}
public void printStackTrace() {