From b59d234b1661d504258ef3780c7337278ac62e32 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 11 Oct 2007 09:59:22 -0600 Subject: [PATCH 1/5] Math.floor() is used by SWT. Defers to the libm (or GCC builtin) for floor --- classpath/java-lang.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/classpath/java-lang.cpp b/classpath/java-lang.cpp index 6f680f12e7..87d28d5c21 100644 --- a/classpath/java-lang.cpp +++ b/classpath/java-lang.cpp @@ -70,8 +70,15 @@ Java_java_lang_System_doMapLibraryName(JNIEnv* e, jclass, jstring name) return r; } +#include +extern "C" JNIEXPORT jdouble JNICALL +Java_java_lang_Math_floor(JNIEnv*, jclass, jdouble val) +{ + return floor(val); +} + extern "C" JNIEXPORT jint JNICALL -Java_java_lang_Double_fillBufferWithDouble(JNIEnv *e, jclass, jdouble val, +Java_java_lang_Double_fillBufferWithDouble(JNIEnv* e, jclass, jdouble val, jbyteArray buffer, jint bufferSize) { jboolean isCopy; jbyte* buf = e->GetByteArrayElements(buffer, &isCopy); From e831a41e903d9c82bda13f9386ba0c0958b70cc0 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 11 Oct 2007 09:59:52 -0600 Subject: [PATCH 2/5] Implemented ByteBuffer.clear() --- classpath/java/nio/ByteBuffer.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/classpath/java/nio/ByteBuffer.java b/classpath/java/nio/ByteBuffer.java index 08ba722bd5..a492637111 100644 --- a/classpath/java/nio/ByteBuffer.java +++ b/classpath/java/nio/ByteBuffer.java @@ -27,6 +27,12 @@ public class ByteBuffer { return array; } + public ByteBuffer clear() { + position = 0; + limit = capacity; + return this; + } + public ByteBuffer slice() { ByteBuffer buf = new ByteBuffer(array); buf.arrayOffset = arrayOffset + position; From db2b7e8fa712c3f44ec0a27f4c8b88332dc36def Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 11 Oct 2007 10:00:35 -0600 Subject: [PATCH 3/5] Semantics of getChars was incorrect. It should specify a begin and end index, not a bunch of lengths --- classpath/java/lang/String.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/classpath/java/lang/String.java b/classpath/java/lang/String.java index 94c7339673..7eb33fa104 100644 --- a/classpath/java/lang/String.java +++ b/classpath/java/lang/String.java @@ -301,13 +301,13 @@ public final class String implements Comparable { return getBytes(); } - public void getChars(int srcOffset, int srcLength, + public void getChars(int srcOffset, int srcEnd, char[] dst, int dstOffset) { - if (srcOffset < 0 || srcOffset + srcLength > length) { + if (srcOffset < 0 || srcEnd > length) { throw new IndexOutOfBoundsException(); } - + int srcLength = srcEnd-srcOffset; if (data instanceof char[]) { char[] src = (char[]) data; System.arraycopy(src, offset + srcOffset, dst, dstOffset, srcLength); From 75c51bb5eca14399f62082c5d9d596b67ab55b25 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 11 Oct 2007 15:39:21 -0600 Subject: [PATCH 4/5] Added user.home system property --- classpath/java-lang.cpp | 14 ++++++++++++-- classpath/java/lang/System.java | 3 +++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/classpath/java-lang.cpp b/classpath/java-lang.cpp index 87d28d5c21..dac022c5d2 100644 --- a/classpath/java-lang.cpp +++ b/classpath/java-lang.cpp @@ -1,3 +1,5 @@ +#include "math.h" +#include "stdlib.h" #include "sys/time.h" #include "time.h" #include "time.h" @@ -22,7 +24,8 @@ Java_java_lang_System_getProperty(JNIEnv* e, jclass, jint code) LineSeparator = 100, FileSeparator = 101, OsName = 102, - JavaIoTmpdir = 103 + JavaIoTmpdir = 103, + UserHome = 104 }; switch (code) { @@ -38,6 +41,14 @@ Java_java_lang_System_getProperty(JNIEnv* e, jclass, jint code) case JavaIoTmpdir: return e->NewStringUTF("/tmp"); + case UserHome: + return e->NewStringUTF("/home/scharff"); +#ifdef WIN32 + LPWSTR home = _wgetenv(L"USERPROFILE"); + return JvNewString(reinterpret_cast(home), lstrlenW(home)); +#else + return e->NewStringUTF(getenv("HOME")); +#endif default: throwNew(e, "java/lang/RuntimeException", 0); return 0; @@ -70,7 +81,6 @@ Java_java_lang_System_doMapLibraryName(JNIEnv* e, jclass, jstring name) return r; } -#include extern "C" JNIEXPORT jdouble JNICALL Java_java_lang_Math_floor(JNIEnv*, jclass, jdouble val) { diff --git a/classpath/java/lang/System.java b/classpath/java/lang/System.java index 1817392ee0..185524b0e7 100644 --- a/classpath/java/lang/System.java +++ b/classpath/java/lang/System.java @@ -15,6 +15,7 @@ public abstract class System { private static final int FileSeparator = 101; private static final int OsName = 102; private static final int JavaIoTmpdir = 103; + private static final int UserHome = 104; private static Property properties; @@ -50,6 +51,8 @@ public abstract class System { code = LineSeparator; } else if (name.equals("file.separator")) { code = FileSeparator; + } else if (name.equals("user.home")) { + code = UserHome; } else if (name.equals("os.name")) { code = OsName; } From 00cfa587bc5bf494e7792383ae58093877726a79 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 11 Oct 2007 15:41:23 -0600 Subject: [PATCH 5/5] Various bug fixes and optimizations --- classpath/java/nio/ByteBuffer.java | 46 ++++++++++++++++--- .../java/nio/channels/SocketChannel.java | 1 + classpath/java/util/LinkedList.java | 4 +- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/classpath/java/nio/ByteBuffer.java b/classpath/java/nio/ByteBuffer.java index a492637111..ba3560482e 100644 --- a/classpath/java/nio/ByteBuffer.java +++ b/classpath/java/nio/ByteBuffer.java @@ -6,17 +6,19 @@ public class ByteBuffer { private int capacity; private int position; private int limit; + private final boolean readOnly; public static ByteBuffer allocate(int capacity) { - return new ByteBuffer(new byte[capacity]); + return new ByteBuffer(new byte[capacity], false); } public static ByteBuffer wrap(byte[] array) { - return new ByteBuffer(array); + return new ByteBuffer(array, false); } - private ByteBuffer(byte[] array) { + private ByteBuffer(byte[] array, boolean readOnly) { this.array = array; + this.readOnly = readOnly; arrayOffset = 0; capacity = array.length; limit = capacity; @@ -34,10 +36,10 @@ public class ByteBuffer { } public ByteBuffer slice() { - ByteBuffer buf = new ByteBuffer(array); + ByteBuffer buf = new ByteBuffer(array, true); buf.arrayOffset = arrayOffset + position; buf.position = 0; - buf.capacity = capacity - position; + buf.capacity = remaining(); buf.limit = buf.capacity; return buf; } @@ -83,13 +85,15 @@ public class ByteBuffer { } public ByteBuffer put(byte val) { + checkPut(1); array[arrayOffset+(position++)] = val; return this; } public ByteBuffer put(ByteBuffer src) { + checkPut(src.remaining()); put(src.array, src.arrayOffset + src.position, src.remaining()); - position += src.remaining(); + src.position += src.remaining(); return this; } @@ -98,12 +102,14 @@ public class ByteBuffer { } public ByteBuffer put(byte[] arr, int offset, int len) { + checkPut(len); System.arraycopy(arr, offset, array, arrayOffset+position, len); position += len; return this; } public ByteBuffer putInt(int position, int val) { + checkPut(position, 4); array[arrayOffset+position] = (byte)((val >> 24) & 0xff); array[arrayOffset+position+1] = (byte)((val >> 16) & 0xff); array[arrayOffset+position+2] = (byte)((val >> 8) & 0xff); @@ -112,18 +118,21 @@ public class ByteBuffer { } public ByteBuffer putInt(int val) { + checkPut(4); putInt(position, val); position += 4; return this; } public ByteBuffer putShort(short val) { + checkPut(2); put((byte)((val >> 8) & 0xff)); put((byte)(val & 0xff)); return this; } public ByteBuffer putLong(long val) { + checkPut(8); putInt((int)(val >> 32)); putInt((int)val); return this; @@ -135,10 +144,12 @@ public class ByteBuffer { } public byte get() { + checkGet(1); return array[arrayOffset+(position++)]; } public byte get(int position) { + checkGet(position, 1); return array[arrayOffset+position]; } @@ -149,6 +160,7 @@ public class ByteBuffer { } public int getInt() { + checkGet(4); int i = get() << 24; i |= (get() & 0xff) << 16; i |= (get() & 0xff) << 8; @@ -157,14 +169,36 @@ public class ByteBuffer { } public short getShort() { + checkGet(2); short s = (short)(get() << 8); s |= get() & 0xff; return s; } public long getLong() { + checkGet(8); long l = getInt() << 32; l |= getInt() & 0xffffffff; return l; } + + private void checkPut(int amount) { + if (readOnly) throw new ReadOnlyBufferException(); + if (amount > limit-position) throw new IndexOutOfBoundsException(); + } + + private void checkPut(int position, int amount) { + if (readOnly) throw new ReadOnlyBufferException(); + if (position < 0 || position+amount > limit) + throw new IndexOutOfBoundsException(); + } + + private void checkGet(int amount) { + if (amount > limit-position) throw new IndexOutOfBoundsException(); + } + + private void checkGet(int position, int amount) { + if (position < 0 || position+amount > limit) + throw new IndexOutOfBoundsException(); + } } diff --git a/classpath/java/nio/channels/SocketChannel.java b/classpath/java/nio/channels/SocketChannel.java index 6c4738385a..1dd41db4f3 100644 --- a/classpath/java/nio/channels/SocketChannel.java +++ b/classpath/java/nio/channels/SocketChannel.java @@ -57,6 +57,7 @@ public class SocketChannel extends SelectableChannel if (! connected) { natThrowWriteError(socket); } + if (b.remaining() == 0) return 0; int w = natWrite(socket, b.array(), b.arrayOffset() + b.position(), b.remaining()); if (w > 0) { b.position(b.position() + w); diff --git a/classpath/java/util/LinkedList.java b/classpath/java/util/LinkedList.java index bdb9b7d589..48d27ff97a 100644 --- a/classpath/java/util/LinkedList.java +++ b/classpath/java/util/LinkedList.java @@ -33,6 +33,7 @@ public class LinkedList implements List { front = rear = c; } else { c.next = front; + front.prev = c; front = c; } } @@ -44,6 +45,7 @@ public class LinkedList implements List { front = rear = c; } else { c.prev = rear; + rear.next = c; rear = c; } } @@ -103,7 +105,7 @@ public class LinkedList implements List { } public boolean add(T element) { - addFirst(element); + addLast(element); return true; }