fix various ObjectOutputStream/ObjectInputStream bugs

This commit is contained in:
Anonymous 2011-07-01 08:43:43 -06:00 committed by Joel Dice
parent 9700b7def1
commit 794a45cb79
11 changed files with 63 additions and 26 deletions

View File

@ -98,6 +98,10 @@ public class ByteArrayOutputStream extends OutputStream {
return array;
}
public String toString(String encoding) throws UnsupportedEncodingException {
return new String(toByteArray(), encoding);
}
private static class Cell {
public byte[] array;
public int offset;

View File

@ -10,7 +10,7 @@
package java.io;
public class File {
public class File implements Serializable {
private static final String FileSeparator
= System.getProperty("file.separator");

View File

@ -11,26 +11,30 @@
package java.io;
public class FilterOutputStream extends OutputStream {
private OutputStream os;
protected OutputStream out;
public FilterOutputStream(OutputStream out) {
this.out = out;
}
public void close() throws IOException {
os.close();
out.close();
}
public void flush() throws IOException {
os.flush();
out.flush();
}
public void write(byte[] b) throws IOException {
os.write(b);
out.write(b);
}
public void write(byte[] b, int off, int len) throws IOException {
os.write(b, off, len);
out.write(b, off, len);
}
public void write(int b) throws IOException {
os.write(b);
out.write(b);
}
}

View File

@ -10,6 +10,8 @@
package java.io;
import avian.VMClass;
import java.util.HashMap;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
@ -110,7 +112,7 @@ public class ObjectInputStream extends InputStream {
StringBuilder sb = new StringBuilder();
int c;
while ((c = r.read()) != -1 && ! Character.isWhitespace((char) c)) {
while ((c = r.read()) != -1 && ! Character.isWhitespace((char) c) && c != ')') {
sb.append((char) c);
}
if (c != -1) {
@ -149,7 +151,6 @@ public class ObjectInputStream extends InputStream {
throws IOException, ClassNotFoundException
{
skipSpace();
switch (r.read()) {
case 'a':
return deserializeArray(map);
@ -203,7 +204,7 @@ public class ObjectInputStream extends InputStream {
return o;
}
private static native Object makeInstance(Class c);
private static native Object makeInstance(VMClass c);
private Object deserializeObject(HashMap<Integer, Object> map)
throws IOException, ClassNotFoundException
@ -211,11 +212,11 @@ public class ObjectInputStream extends InputStream {
read('(');
int id = (int) readLongToken();
Class c = Class.forName(readStringToken());
Object o = makeInstance(c);
Object o = makeInstance(c.vmClass);
map.put(id, o);
for (Field f: c.getFields()) {
for (Field f: c.getAllFields()) {
int modifiers = f.getModifiers();
if ((modifiers & (Modifier.TRANSIENT | Modifier.STATIC)) == 0) {
try {

View File

@ -39,7 +39,7 @@ public class ObjectOutputStream extends OutputStream {
}
public void writeObject(Object o) throws IOException {
writeObject(o, new IdentityHashMap(), 0);
writeObject(o, new IdentityHashMap(), new int[] {0});
}
public void writeBoolean(boolean v) {
@ -87,7 +87,7 @@ public class ObjectOutputStream extends OutputStream {
}
private void writeObject(Object o, IdentityHashMap<Object, Integer> map,
int nextId)
int[] nextId)
throws IOException
{
if (o == null) {
@ -95,7 +95,7 @@ public class ObjectOutputStream extends OutputStream {
} else {
Integer id = map.get(o);
if (id == null) {
map.put(o, nextId);
map.put(o, nextId[0]);
Class c = o.getClass();
if (c.isArray()) {
@ -113,7 +113,7 @@ public class ObjectOutputStream extends OutputStream {
}
private void serializeArray(Object o, IdentityHashMap<Object, Integer> map,
int nextId)
int[] nextId)
throws IOException
{
Class c = o.getClass();
@ -121,7 +121,7 @@ public class ObjectOutputStream extends OutputStream {
int length = Array.getLength(o);
out.print("a(");
out.print(nextId++);
out.print(nextId[0]++);
out.print(" ");
out.print(c.getName());
out.print(" ");
@ -155,17 +155,17 @@ public class ObjectOutputStream extends OutputStream {
}
private void serializeObject(Object o, IdentityHashMap<Object, Integer> map,
int nextId)
int[] nextId)
throws IOException
{
Class c = o.getClass();
out.print("l(");
out.print(nextId++);
out.print(nextId[0]++);
out.print(" ");
out.print(c.getName());
for (Field f: c.getFields()) {
for (Field f: c.getAllFields()) {
int modifiers = f.getModifiers();
if ((modifiers & (Modifier.TRANSIENT | Modifier.STATIC)) == 0) {
out.print(" ");

View File

@ -29,6 +29,7 @@ import java.lang.annotation.Annotation;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.security.ProtectionDomain;
@ -424,6 +425,25 @@ public final class Class <T> implements Type, AnnotatedElement {
return array;
}
private static void getAllFields(VMClass vmClass, ArrayList<Field> fields) {
if (vmClass.super_ != null) {
getAllFields(vmClass.super_, fields);
}
if (vmClass.fieldTable != null) {
Classes.link(vmClass);
for (int i = 0; i < vmClass.fieldTable.length; ++i) {
fields.add(new Field(vmClass.fieldTable[i]));
}
}
}
public Field[] getAllFields() {
ArrayList<Field> fields = new ArrayList<Field>();
getAllFields(vmClass, fields);
return fields.toArray(new Field[fields.size()]);
}
private int countMethods(boolean publicOnly) {
int count = 0;
if (vmClass.methodTable != null) {
@ -515,7 +535,7 @@ public final class Class <T> implements Type, AnnotatedElement {
}
public Class getSuperclass() {
return SystemClassLoader.getClass(vmClass.super_);
return (vmClass.super_ == null ? null : SystemClassLoader.getClass(vmClass.super_));
}
public boolean isArray() {

View File

@ -205,7 +205,7 @@ public class Field<T> extends AccessibleObject {
} else {
throw new IllegalArgumentException
("needed " + getType() + ", got "
+ Class.getName(Classes.vmClass(target)) +
+ value.getClass().getName() +
" when setting " + Class.getName(vmField.class_) + "." + getName());
}
break;

View File

@ -11,5 +11,5 @@
package java.lang.reflect;
public interface InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] arguments);
public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable;
}

View File

@ -20,4 +20,8 @@ public class ProtectionDomain {
this.codeSource = codeSource;
this.permissions = permissions;
}
public CodeSource getCodeSource() {
return codeSource;
}
}

View File

@ -10,7 +10,7 @@
package java.util;
public class ArrayList<T> extends AbstractList<T> {
public class ArrayList<T> extends AbstractList<T> implements java.io.Serializable {
private static final int MinimumCapacity = 16;
private Object[] array;

View File

@ -10,7 +10,7 @@
package java.util;
public class Vector<T> extends AbstractList<T> {
public class Vector<T> extends AbstractList<T> implements java.io.Serializable {
private final ArrayList<T> list;
public Vector(int capacity) {
@ -81,6 +81,10 @@ public class Vector<T> extends AbstractList<T> {
remove(index);
}
public synchronized void removeAllElements() {
list.clear();
}
public synchronized boolean remove(Object element) {
return list.remove(element);
}