From a4b4f36c5b6f51ae7cfb57113c96b5bc20de5144 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Thu, 30 Aug 2007 17:31:32 -0600 Subject: [PATCH] various classpath updates to help SWT build --- classpath/java/io/ByteArrayOutputStream.java | 94 ++++++++++++++++++++ classpath/java/io/FileOutputStream.java | 6 +- classpath/java/io/InputStream.java | 19 ++++ classpath/java/lang/Boolean.java | 4 + classpath/java/lang/Class.java | 13 +++ classpath/java/lang/Double.java | 4 + classpath/java/lang/Integer.java | 4 + classpath/java/lang/Math.java | 36 +++++++- classpath/java/lang/Object.java | 4 +- classpath/java/lang/String.java | 60 ++++++++++++- classpath/java/lang/StringBuffer.java | 18 ++++ classpath/java/lang/StringBuilder.java | 78 ++++++++++++++-- classpath/java/util/ArrayList.java | 18 ++++ classpath/java/util/Vector.java | 16 ++++ 14 files changed, 358 insertions(+), 16 deletions(-) create mode 100644 classpath/java/io/ByteArrayOutputStream.java diff --git a/classpath/java/io/ByteArrayOutputStream.java b/classpath/java/io/ByteArrayOutputStream.java new file mode 100644 index 0000000000..4ac99e263b --- /dev/null +++ b/classpath/java/io/ByteArrayOutputStream.java @@ -0,0 +1,94 @@ +package java.io; + +public class ByteArrayOutputStream extends OutputStream { + private static final int BufferSize = 32; + + private Cell chain; + private int length; + private byte[] buffer; + private int position; + + public ByteArrayOutputStream(int capacity) { } + + public ByteArrayOutputStream() { + this(0); + } + + public void write(int c) { + if (buffer == null) { + buffer = new byte[BufferSize]; + } else if (position >= buffer.length) { + flushBuffer(); + buffer = new byte[BufferSize]; + } + + buffer[position++] = (byte) (c & 0xFF); + ++ length; + } + + private byte[] copy(byte[] b, int offset, int length) { + byte[] array = new byte[length]; + System.arraycopy(b, offset, array, 0, length); + return array; + } + + public void write(byte[] b, int offset, int length) { + if (b == null) { + throw new NullPointerException(); + } + + if (offset < 0 || offset + length > b.length) { + throw new ArrayIndexOutOfBoundsException(); + } + + if (length == 0) return; + + if (buffer != null && length <= buffer.length - position) { + System.arraycopy(b, offset, buffer, position, length); + position += length; + } else { + flushBuffer(); + chain = new Cell(copy(b, offset, length), 0, length, chain); + } + + this.length += length; + } + + private void flushBuffer() { + if (position > 0) { + byte[] b = buffer; + int p = position; + buffer = null; + position = 0; + chain = new Cell(b, 0, p, chain); + length -= p; + } + } + + public byte[] toByteArray() { + flushBuffer(); + + byte[] array = new byte[length]; + int index = length; + for (Cell c = chain; c != null; c = c.next) { + int start = index - c.length; + System.arraycopy(c.array, c.offset, array, start, c.length); + index = start; + } + return array; + } + + private static class Cell { + public byte[] array; + public int offset; + public int length; + public Cell next; + + public Cell(byte[] array, int offset, int length, Cell next) { + this.array = array; + this.offset = offset; + this.length = length; + this.next = next; + } + } +} diff --git a/classpath/java/io/FileOutputStream.java b/classpath/java/io/FileOutputStream.java index 8437d3691c..fd9f577f14 100644 --- a/classpath/java/io/FileOutputStream.java +++ b/classpath/java/io/FileOutputStream.java @@ -21,12 +21,12 @@ public class FileOutputStream extends OutputStream { private static native int open(String path) throws IOException; - public static native void write(int fd, int c) throws IOException; + private static native void write(int fd, int c) throws IOException; - public static native void write(int fd, byte[] b, int offset, int length) + private static native void write(int fd, byte[] b, int offset, int length) throws IOException; - public static native void close(int fd) throws IOException; + private static native void close(int fd) throws IOException; public void write(int c) throws IOException { write(fd, c); diff --git a/classpath/java/io/InputStream.java b/classpath/java/io/InputStream.java index 441586622c..c485a38f42 100644 --- a/classpath/java/io/InputStream.java +++ b/classpath/java/io/InputStream.java @@ -23,5 +23,24 @@ public abstract class InputStream { return length; } + public long skip(long count) throws IOException { + final long Max = 8 * 1024; + int size = (int) (count < Max ? count : Max); + byte[] buffer = new byte[size]; + long remaining = count; + int c; + while ((c = read(buffer, 0, (int) (size < remaining ? size : remaining))) + >= 0 + && remaining > 0) + { + remaining -= c; + } + return count - remaining; + } + + public int available() throws IOException { + return 0; + } + public void close() throws IOException { } } diff --git a/classpath/java/lang/Boolean.java b/classpath/java/lang/Boolean.java index 5d68dcf8ed..c74c306269 100644 --- a/classpath/java/lang/Boolean.java +++ b/classpath/java/lang/Boolean.java @@ -16,6 +16,10 @@ public final class Boolean { return (value ? Boolean.TRUE : Boolean.FALSE); } + public static Boolean valueOf(String s) { + return ("true".equals(s) ? Boolean.TRUE : Boolean.FALSE); + } + public boolean equals(Object o) { return o instanceof Boolean && ((Boolean) o).value == value; } diff --git a/classpath/java/lang/Class.java b/classpath/java/lang/Class.java index 0ae4019b0c..a040853222 100644 --- a/classpath/java/lang/Class.java +++ b/classpath/java/lang/Class.java @@ -4,6 +4,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.lang.reflect.InvocationTargetException; public final class Class { private static final int PrimitiveFlag = 1 << 4; @@ -37,6 +38,18 @@ public final class Class { return staticTable; } + public T newInstance() + throws IllegalAccessException, InstantiationException + { + try { + return (T) getConstructor().newInstance(); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + } + public static Class forName(String name) throws ClassNotFoundException { return forName (name, true, Method.getCaller().getDeclaringClass().getClassLoader()); diff --git a/classpath/java/lang/Double.java b/classpath/java/lang/Double.java index 99d9b2640f..8cab976e1b 100644 --- a/classpath/java/lang/Double.java +++ b/classpath/java/lang/Double.java @@ -3,6 +3,10 @@ package java.lang; public final class Double extends Number { public static final Class TYPE = Class.forCanonicalName("D"); + public static final double NEGATIVE_INFINITY = -1.0 / 0.0; + public static final double POSITIVE_INFINITY = 1.0 / 0.0; + public static final double NaN = 0.0 / 0.0; + private final double value; public Double(String value) { diff --git a/classpath/java/lang/Integer.java b/classpath/java/lang/Integer.java index 8c2f7e030d..ad51bdb2c0 100644 --- a/classpath/java/lang/Integer.java +++ b/classpath/java/lang/Integer.java @@ -36,6 +36,10 @@ public final class Integer extends Number { return toString(v, 10); } + public static String toHexString(int v) { + return toString(v, 16); + } + public byte byteValue() { return (byte) value; } diff --git a/classpath/java/lang/Math.java b/classpath/java/lang/Math.java index 6d622b3125..51a74cdb32 100644 --- a/classpath/java/lang/Math.java +++ b/classpath/java/lang/Math.java @@ -3,6 +3,38 @@ package java.lang; public final class Math { private Math() { } + public static double max(double a, double b) { + return (a < b ? b : a); + } + + public static double min(double a, double b) { + return (a > b ? b : a); + } + + public static float max(float a, float b) { + return (a < b ? b : a); + } + + public static float min(float a, float b) { + return (a > b ? b : a); + } + + public static long max(long a, long b) { + return (a < b ? b : a); + } + + public static long min(long a, long b) { + return (a > b ? b : a); + } + + public static int max(int a, int b) { + return (a < b ? b : a); + } + + public static int min(int a, int b) { + return (a > b ? b : a); + } + public static int abs(int v) { return (v < 0 ? -v : v); } @@ -20,11 +52,11 @@ public final class Math { } public static long round(double v) { - return (long) (v + 0.5); + return (long) Math.floor(v + 0.5); } public static int round(float v) { - return (int) (v + 0.5); + return (int) Math.floor(v + 0.5); } public static native double floor(double v); diff --git a/classpath/java/lang/Object.java b/classpath/java/lang/Object.java index 236350aa91..f98afa5bfe 100644 --- a/classpath/java/lang/Object.java +++ b/classpath/java/lang/Object.java @@ -27,9 +27,9 @@ public class Object { public native String toString(); - public final void wait() { + public final void wait() throws InterruptedException { wait(0); } - public native final void wait(long timeout); + public native final void wait(long timeout) throws InterruptedException; } diff --git a/classpath/java/lang/String.java b/classpath/java/lang/String.java index 5a8e0962c2..db4d669a20 100644 --- a/classpath/java/lang/String.java +++ b/classpath/java/lang/String.java @@ -121,8 +121,54 @@ public final class String implements Comparable { } } - public int indexOf(int c) { + public String trim() { + int start = -1; for (int i = 0; i < length; ++i) { + char c = charAt(i); + if (start == -1 && ! Character.isWhitespace(c)) { + start = i; + break; + } + } + + int end = -1; + for (int i = length - 1; i >= 0; --i) { + char c = charAt(i); + if (end == -1 && ! Character.isWhitespace(c)) { + end = i + 1; + break; + } + } + + if (start >= end) { + return ""; + } else { + return substring(start, end); + } + } + + public String toLowerCase() { + char[] b = new char[length]; + for (int i = 0; i < length; ++i) { + b[i] = Character.toLowerCase(charAt(i)); + } + return new String(b, 0, length, false); + } + + public String toUpperCase() { + char[] b = new char[length]; + for (int i = 0; i < length; ++i) { + b[i] = Character.toUpperCase(charAt(i)); + } + return new String(b, 0, length, false); + } + + public int indexOf(int c) { + return indexOf(c, 0); + } + + public int indexOf(int c, int start) { + for (int i = start; i < length; ++i) { if (charAt(i) == c) { return i; } @@ -142,9 +188,13 @@ public final class String implements Comparable { } public int indexOf(String s) { - if (s.length == 0) return 0; + return indexOf(s, 0); + } - for (int i = 0; i < length - s.length + 1; ++i) { + public int indexOf(String s, int start) { + if (s.length == 0) return start; + + for (int i = start; i < length - s.length + 1; ++i) { int j = 0; for (; j < s.length; ++j) { if (charAt(i + j) != s.charAt(j)) { @@ -282,6 +332,10 @@ public final class String implements Comparable { public native String intern(); + public static String valueOf(Object s) { + return s.toString(); + } + public static String valueOf(boolean v) { return Boolean.toString(v); } diff --git a/classpath/java/lang/StringBuffer.java b/classpath/java/lang/StringBuffer.java index 497e2d1843..ac90f68fc7 100644 --- a/classpath/java/lang/StringBuffer.java +++ b/classpath/java/lang/StringBuffer.java @@ -3,6 +3,10 @@ package java.lang; public class StringBuffer { private final StringBuilder sb; + public StringBuffer(String s) { + sb = new StringBuilder(s); + } + public StringBuffer(int capacity) { sb = new StringBuilder(capacity); } @@ -36,11 +40,25 @@ public class StringBuffer { return this; } + public synchronized StringBuffer append(char[] b, int offset, int length) { + sb.append(b, offset, length); + return this; + } + + public synchronized StringBuffer insert(int i, String s) { + sb.insert(i, s); + return this; + } + public synchronized StringBuffer deleteCharAt(int i) { sb.deleteCharAt(i); return this; } + public synchronized char charAt(int i) { + return sb.charAt(i); + } + public synchronized int length() { return sb.length(); } diff --git a/classpath/java/lang/StringBuilder.java b/classpath/java/lang/StringBuilder.java index 26287796c7..a951cef50d 100644 --- a/classpath/java/lang/StringBuilder.java +++ b/classpath/java/lang/StringBuilder.java @@ -1,11 +1,17 @@ package java.lang; public class StringBuilder { + private static final int BufferSize = 32; + private Cell chain; private int length; private char[] buffer; private int position; + public StringBuilder(String s) { + append(s); + } + public StringBuilder(int capacity) { } public StringBuilder() { @@ -28,8 +34,13 @@ public class StringBuilder { return append("null"); } else { if (s.length() > 0) { - flush(); - chain = new Cell(s, chain); + if (buffer != null && s.length() <= buffer.length - position) { + s.getChars(0, s.length(), buffer, position); + position += s.length(); + } else { + flush(); + chain = new Cell(s, chain); + } length += s.length(); } return this; @@ -46,10 +57,10 @@ public class StringBuilder { public StringBuilder append(char v) { if (buffer == null) { - buffer = new char[32]; + buffer = new char[BufferSize]; } else if (position >= buffer.length) { flush(); - buffer = new char[32]; + buffer = new char[BufferSize]; } buffer[position++] = v; @@ -70,6 +81,60 @@ public class StringBuilder { return append(String.valueOf(v)); } + public char charAt(int i) { + if (i < 0 || i >= length) { + throw new IndexOutOfBoundsException(); + } + + flush(); + + 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) { + return c.value.charAt(i - start); + } + } + + throw new RuntimeException(); + } + + public StringBuilder insert(int i, String s) { + if (i < 0 || i >= length) { + throw new IndexOutOfBoundsException(); + } + + if (i == length) { + append(s); + } else { + flush(); + + int index = length; + -- length; + for (Cell c = chain; c != null; c = c.next) { + int start = index - c.value.length(); + index = start; + + if (i >= start) { + if (i == start) { + c.next = new Cell(s, c.next); + } else { + String v = c.value; + c.value = v.substring(i - start, v.length()); + c.next = new Cell(s, new Cell(v.substring(0, i - start), c.next)); + } + break; + } + } + } + + return this; + } + public StringBuilder deleteCharAt(int i) { if (i < 0 || i >= length) { throw new IndexOutOfBoundsException(); @@ -96,8 +161,9 @@ public class StringBuilder { } 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); + String v = c.value; + c.value = v.substring(i - start + 1, v.length()); + c.next = new Cell(v.substring(0, i - start), c.next); } break; } diff --git a/classpath/java/util/ArrayList.java b/classpath/java/util/ArrayList.java index f440f06794..ed48fd2328 100644 --- a/classpath/java/util/ArrayList.java +++ b/classpath/java/util/ArrayList.java @@ -67,6 +67,24 @@ public class ArrayList implements List { return true; } + public int indexOf(T element) { + for (int i = 0; i < size; ++i) { + if (equal(element, array[i])) { + return i; + } + } + return -1; + } + + public int lastIndexOf(T element) { + for (int i = size; i >= 0; --i) { + if (equal(element, array[i])) { + return i; + } + } + return -1; + } + public T get(int index) { if (index >= 0 && index < size) { return (T) array[index]; diff --git a/classpath/java/util/Vector.java b/classpath/java/util/Vector.java index 14cc5057f6..1350ce3926 100644 --- a/classpath/java/util/Vector.java +++ b/classpath/java/util/Vector.java @@ -39,14 +39,30 @@ public class Vector implements List { return list.remove(index); } + public T removeElementAt(int index) { + return remove(index); + } + public synchronized boolean remove(T element) { return list.remove(element); } + public boolean removeElement(T element) { + return remove(element); + } + public synchronized void clear() { list.clear(); } + public synchronized int indexOf(T element) { + return list.indexOf(element); + } + + public synchronized int lastIndexOf(T element) { + return list.lastIndexOf(element); + } + public synchronized void copyInto(Object[] array) { for (int i = 0; i < size(); ++i) { array[i] = list.get(i);