This commit is contained in:
Joel Dice 2007-08-14 19:14:55 -06:00
parent 71c7013808
commit f22dda0df1
11 changed files with 129 additions and 18 deletions

View File

@ -1,6 +1,10 @@
package java.io; package java.io;
public class File { public class File {
static {
System.loadLibrary("natives");
}
private final String path; private final String path;
public File(String path) { public File(String path) {

View File

@ -1,6 +1,10 @@
package java.io; package java.io;
public class FileInputStream extends InputStream { public class FileInputStream extends InputStream {
static {
System.loadLibrary("natives");
}
private int fd; private int fd;
public FileInputStream(FileDescriptor fd) { public FileInputStream(FileDescriptor fd) {

View File

@ -1,6 +1,10 @@
package java.io; package java.io;
public class FileOutputStream extends OutputStream { public class FileOutputStream extends OutputStream {
static {
System.loadLibrary("natives");
}
private int fd; private int fd;
public FileOutputStream(FileDescriptor fd) { public FileOutputStream(FileDescriptor fd) {

View File

@ -1,7 +1,15 @@
package java.lang; package java.lang;
public class Object { public class Object {
protected native Object clone() throws CloneNotSupportedException; protected Object clone() throws CloneNotSupportedException {
if (this instanceof Cloneable) {
return clone(this);
} else {
throw new CloneNotSupportedException();
}
}
private static native Object clone(Object o);
public boolean equals(Object o) { public boolean equals(Object o) {
return this == o; return this == o;

View File

@ -31,6 +31,18 @@ public final class String implements Comparable<String> {
} }
private String(Object data, int offset, int length, boolean copy) { private String(Object data, int offset, int length, boolean copy) {
int l;
if (data instanceof char[]) {
l = ((char[]) data).length;
} else {
l = ((byte[]) data).length;
}
if (offset < 0 || offset + length > l) {
throw new IndexOutOfBoundsException
(offset + " < 0 or " + offset + " + " + length + " > " + l);
}
if (copy) { if (copy) {
Object c; Object c;
if (data instanceof char[]) { if (data instanceof char[]) {
@ -59,7 +71,9 @@ public final class String implements Comparable<String> {
public int hashCode() { public int hashCode() {
if (hash == 0) { if (hash == 0) {
for (int i = 0; i < length; ++i) hash = (hash * 31) + charAt(i); int h = 0;
for (int i = 0; i < length; ++i) h = (h * 31) + charAt(i);
hash = h;
} }
return hash; return hash;
} }

View File

@ -21,6 +21,11 @@ public class StringBuffer {
return this; return this;
} }
public synchronized StringBuffer append(char v) {
sb.append(v);
return this;
}
public synchronized StringBuffer append(int v) { public synchronized StringBuffer append(int v) {
sb.append(v); sb.append(v);
return this; return this;

View File

@ -3,6 +3,8 @@ package java.lang;
public class StringBuilder { public class StringBuilder {
private Cell chain; private Cell chain;
private int length; private int length;
private char[] buffer;
private int position;
public StringBuilder(int capacity) { } public StringBuilder(int capacity) { }
@ -10,12 +12,28 @@ public class StringBuilder {
this(0); this(0);
} }
public StringBuilder append(String s) { private void flush() {
if (s.length() > 0) { if (position > 0) {
chain = new Cell(s, chain); char[] b = buffer;
length += s.length(); int p = position;
buffer = null;
position = 0;
append(new String(b, 0, p, false));
length -= p;
}
}
public StringBuilder append(String s) {
if (s == null) {
return append("null");
} else {
if (s.length() > 0) {
flush();
chain = new Cell(s, chain);
length += s.length();
}
return this;
} }
return this;
} }
public StringBuilder append(char[] b, int offset, int length) { public StringBuilder append(char[] b, int offset, int length) {
@ -26,6 +44,20 @@ public class StringBuilder {
return append(o == null ? "null" : o.toString()); return append(o == null ? "null" : o.toString());
} }
public StringBuilder append(char v) {
if (buffer == null) {
buffer = new char[32];
} else if (position >= buffer.length) {
flush();
buffer = new char[32];
}
buffer[position++] = v;
++ length;
return this;
}
public StringBuilder append(int v) { public StringBuilder append(int v) {
return append(String.valueOf(v)); return append(String.valueOf(v));
} }
@ -39,6 +71,8 @@ public class StringBuilder {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
flush();
int index = length; int index = length;
-- length; -- length;
Cell p = null; Cell p = null;
@ -83,6 +117,8 @@ public class StringBuilder {
return; return;
} }
flush();
int index = length; int index = length;
length = v; length = v;
for (Cell c = chain; c != null; c = c.next) { for (Cell c = chain; c != null; c = c.next) {
@ -106,6 +142,8 @@ public class StringBuilder {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
flush();
int index = length; int index = length;
for (Cell c = chain; c != null; c = c.next) { for (Cell c = chain; c != null; c = c.next) {
int start = index - c.value.length(); int start = index - c.value.length();

View File

@ -5,7 +5,7 @@ public class HashMap<K, V> implements Map<K, V> {
private Cell[] array; private Cell[] array;
private final Helper helper; private final Helper helper;
HashMap(int capacity, Helper<K, V> helper) { public HashMap(int capacity, Helper<K, V> helper) {
if (capacity > 0) { if (capacity > 0) {
array = new Cell[nextPowerOfTwo(capacity)]; array = new Cell[nextPowerOfTwo(capacity)];
} }
@ -20,6 +20,22 @@ public class HashMap<K, V> implements Map<K, V> {
this(0); this(0);
} }
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{");
for (Iterator<Entry<K, V>> it = iterator(); it.hasNext();) {
Entry<K, V> e = it.next();
sb.append(e.getKey())
.append("=")
.append(e.getValue());
if (it.hasNext()) {
sb.append(",");
}
}
sb.append("}");
return sb.toString();
}
private static int nextPowerOfTwo(int n) { private static int nextPowerOfTwo(int n) {
int r = 1; int r = 1;
while (r < n) r <<= 1; while (r < n) r <<= 1;
@ -85,8 +101,7 @@ public class HashMap<K, V> implements Map<K, V> {
array[index] = cell; array[index] = cell;
} }
// primarily for use by WeakHashMap: public void remove(Cell<K, V> cell) {
void remove(Cell<K, V> cell) {
int index = cell.hashCode() & (array.length - 1); int index = cell.hashCode() & (array.length - 1);
Cell<K, V> p = null; Cell<K, V> p = null;
for (Cell<K, V> c = array[index]; c != null; c = c.next()) { for (Cell<K, V> c = array[index]; c != null; c = c.next()) {
@ -366,7 +381,7 @@ public class HashMap<K, V> implements Map<K, V> {
} }
} }
} }
return false; return nextCell != null;
} }
public void remove() { public void remove() {

View File

@ -10,7 +10,7 @@ namespace {
object object
doInvoke(Thread* t, object this_, object instance, object arguments) doInvoke(Thread* t, object this_, object instance, object arguments)
{ {
object v = pushReference(t, run2(t, this_, instance, arguments)); object v = run2(t, this_, instance, arguments);
if (t->exception) { if (t->exception) {
t->exception = makeInvocationTargetException(t, t->exception); t->exception = makeInvocationTargetException(t, t->exception);
} }
@ -64,6 +64,16 @@ Object_hashCode(Thread* t, jobject this_)
return objectHash(t, *this_); return objectHash(t, *this_);
} }
jobject
Object_clone(Thread* t, jclass, jobject o)
{
object clone = make(t, objectClass(t, *o));
memcpy(static_cast<void**>(clone) + 1,
static_cast<void**>(*o) + 1,
(baseSize(t, *o, objectClass(t, *o)) - 1) * BytesPerWord);
return pushReference(t, clone);
}
jclass jclass
ClassLoader_defineClass(Thread* t, jclass, jbyteArray b, jint offset, ClassLoader_defineClass(Thread* t, jclass, jbyteArray b, jint offset,
jint length) jint length)
@ -816,6 +826,8 @@ populateBuiltinMap(Thread* t, object map)
reinterpret_cast<void*>(::Object_wait) }, reinterpret_cast<void*>(::Object_wait) },
{ "Java_java_lang_Object_hashCode", { "Java_java_lang_Object_hashCode",
reinterpret_cast<void*>(::Object_hashCode) }, reinterpret_cast<void*>(::Object_hashCode) },
{ "Java_java_lang_Object_clone",
reinterpret_cast<void*>(::Object_clone) },
{ "Java_java_lang_reflect_Array_get", { "Java_java_lang_reflect_Array_get",
reinterpret_cast<void*>(::Array_get) }, reinterpret_cast<void*>(::Array_get) },

View File

@ -1829,7 +1829,9 @@ inline uint32_t
hash(const int8_t* s, unsigned length) hash(const int8_t* s, unsigned length)
{ {
uint32_t h = 0; uint32_t h = 0;
for (unsigned i = 0; i < length; ++i) h = (h * 31) + s[i]; for (unsigned i = 0; i < length; ++i) {
h = (h * 31) + static_cast<unsigned>(s[i]);
}
return h; return h;
} }
@ -1837,7 +1839,9 @@ inline uint32_t
hash(const uint16_t* s, unsigned length) hash(const uint16_t* s, unsigned length)
{ {
uint32_t h = 0; uint32_t h = 0;
for (unsigned i = 0; i < length; ++i) h = (h * 31) + s[i]; for (unsigned i = 0; i < length; ++i) {
h = (h * 31) + s[i];
}
return h; return h;
} }
@ -1934,9 +1938,11 @@ stringHash(Thread* t, object s)
if (objectClass(t, data) if (objectClass(t, data)
== arrayBody(t, t->vm->types, Machine::ByteArrayType)) == arrayBody(t, t->vm->types, Machine::ByteArrayType))
{ {
stringHashCode(t, s) = byteArrayHash(t, data); stringHashCode(t, s) = hash
(&byteArrayBody(t, data, stringOffset(t, s)), stringLength(t, s));
} else { } else {
stringHashCode(t, s) = charArrayHash(t, data); stringHashCode(t, s) = hash
(&charArrayBody(t, data, stringOffset(t, s)), stringLength(t, s));
} }
} }
return stringHashCode(t, s); return stringHashCode(t, s);

View File

@ -1570,12 +1570,13 @@ run(Thread* t)
object class_ = resolveClass(t, codePool(t, code), index - 1); object class_ = resolveClass(t, codePool(t, code), index - 1);
if (UNLIKELY(exception)) goto throw_; if (UNLIKELY(exception)) goto throw_;
if (instanceOf(t, class_, peekObject(t, sp - 1))) { if (instanceOf(t, class_, popObject(t))) {
pushInt(t, 1); pushInt(t, 1);
} else { } else {
pushInt(t, 0); pushInt(t, 0);
} }
} else { } else {
popObject(t);
pushInt(t, 0); pushInt(t, 0);
} }
} goto loop; } goto loop;