mirror of
https://github.com/corda/corda.git
synced 2025-01-19 11:16:54 +00:00
Merge pull request #95 from dscho/compatible-serialization
Java-compatible (de)serialization of TreeMap, ArrayList and Number
This commit is contained in:
commit
4cf3d9de88
@ -10,7 +10,9 @@
|
||||
|
||||
package java.lang;
|
||||
|
||||
public abstract class Number {
|
||||
import java.io.Serializable;
|
||||
|
||||
public abstract class Number implements Serializable {
|
||||
public abstract byte byteValue();
|
||||
public abstract short shortValue();
|
||||
public abstract int intValue();
|
||||
|
@ -10,6 +10,10 @@
|
||||
|
||||
package java.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class ArrayList<T> extends AbstractList<T> implements java.io.Serializable {
|
||||
private static final int MinimumCapacity = 16;
|
||||
|
||||
@ -183,4 +187,23 @@ public class ArrayList<T> extends AbstractList<T> implements java.io.Serializabl
|
||||
public String toString() {
|
||||
return Collections.toString(this);
|
||||
}
|
||||
|
||||
private void writeObject(ObjectOutputStream out) throws IOException {
|
||||
out.defaultWriteObject();
|
||||
out.writeInt(array.length);
|
||||
for (T o : this) {
|
||||
out.writeObject(o);
|
||||
}
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream in)
|
||||
throws ClassNotFoundException, IOException
|
||||
{
|
||||
in.defaultReadObject();
|
||||
int capacity = in.readInt();
|
||||
grow(capacity);
|
||||
for (int i = 0; i < size; i++) {
|
||||
array[i] = in.readObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,25 +10,37 @@
|
||||
|
||||
package java.util;
|
||||
|
||||
public class TreeMap<K,V> implements Map<K,V> {
|
||||
private TreeSet<MyEntry<K,V>> set;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public TreeMap(final Comparator<K> comparator) {
|
||||
set = new TreeSet(new Comparator<MyEntry<K,V>>() {
|
||||
public int compare(MyEntry<K,V> a, MyEntry<K,V> b) {
|
||||
return comparator.compare(a.key, b.key);
|
||||
}
|
||||
});
|
||||
public class TreeMap<K,V> implements Map<K,V> {
|
||||
private final Comparator<K> comparator;
|
||||
private transient TreeSet<MyEntry<K,V>> set;
|
||||
|
||||
public TreeMap(Comparator<K> comparator) {
|
||||
this.comparator = comparator;
|
||||
initializeSet();
|
||||
}
|
||||
|
||||
public TreeMap() {
|
||||
this(new Comparator<K>() {
|
||||
private void initializeSet() {
|
||||
final Comparator<K> comparator = this.comparator != null ?
|
||||
this.comparator : new Comparator<K>() {
|
||||
public int compare(K a, K b) {
|
||||
return ((Comparable) a).compareTo(b);
|
||||
}
|
||||
};
|
||||
set = new TreeSet(new Comparator<MyEntry<K,V>>() {
|
||||
public int compare(MyEntry<K,V> a, MyEntry<K,V> b) {
|
||||
return comparator.compare(a.key, b.key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public TreeMap() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return Collections.toString(this);
|
||||
}
|
||||
@ -224,4 +236,26 @@ public class TreeMap<K,V> implements Map<K,V> {
|
||||
return new Collections.ValueIterator(set.iterator());
|
||||
}
|
||||
}
|
||||
|
||||
public final static long serialVersionUID = 0x0cc1f63e2d256ae6l;
|
||||
|
||||
private void writeObject(ObjectOutputStream out) throws IOException {
|
||||
out.defaultWriteObject();
|
||||
out.writeInt(size());
|
||||
for (Entry<K, V> entry : entrySet()) {
|
||||
out.writeObject(entry.getKey());
|
||||
out.writeObject(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream in) throws IOException {
|
||||
in.defaultReadObject();
|
||||
initializeSet();
|
||||
int size = in.readInt();
|
||||
for (int i = 0; i < size; i++) try {
|
||||
put((K) in.readObject(), (V) in.readObject());
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class Serialize implements Serializable {
|
||||
public static final long serialVersionUID = 1l;
|
||||
@ -23,15 +24,44 @@ public class Serialize implements Serializable {
|
||||
expect(a.equals(b));
|
||||
}
|
||||
|
||||
private static String pad(long number, int length) {
|
||||
return pad(Long.toHexString(number), length, '0');
|
||||
}
|
||||
|
||||
private static String pad(String s, int length, char padChar) {
|
||||
length -= s.length();
|
||||
if (length <= 0) {
|
||||
return s;
|
||||
}
|
||||
StringBuilder builder = new StringBuilder();
|
||||
while (length-- > 0) {
|
||||
builder.append(padChar);
|
||||
}
|
||||
return builder.append(s).toString();
|
||||
}
|
||||
|
||||
protected static void hexdump(byte[] a) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
System.err.print(pad(0, 8) + " ");
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if ((i & 0xf) == 0) {
|
||||
System.err.println();
|
||||
}
|
||||
String hex = Integer.toHexString(a[i] & 0xff);
|
||||
System.err.print(" " + (hex.length() == 1 ? "0" : "") + hex);
|
||||
builder.append(a[i] < 0x20 || a[i] > 0x7f ? '.' : (char)a[i]);
|
||||
if ((i & 0xf) == 0x7) {
|
||||
System.err.print(" ");
|
||||
} else if ((i & 0xf) == 0xf) {
|
||||
System.err.println(" |" + builder + "|");
|
||||
builder.setLength(0);
|
||||
System.err.print(pad(i + 1, 8) + " ");
|
||||
}
|
||||
}
|
||||
System.err.println();
|
||||
for (int i = a.length & 0xf; i < 0x10; i++) {
|
||||
System.err.print(" ");
|
||||
if ((i & 0xf) == 0x7) {
|
||||
System.err.print(" ");
|
||||
}
|
||||
}
|
||||
System.err.println(" |" + builder + "|");
|
||||
}
|
||||
|
||||
private static void expectEqual(byte[] a, int[] b) {
|
||||
@ -112,5 +142,51 @@ public class Serialize implements Serializable {
|
||||
Serialize unserialized = (Serialize) in2.readObject();
|
||||
expectEqual(0x12345678, unserialized.dummy);
|
||||
in2.close();
|
||||
|
||||
out.reset();
|
||||
out2 = new ObjectOutputStream(out);
|
||||
TreeMap map = new TreeMap();
|
||||
map.put("key", "value");
|
||||
out2.writeObject(map);
|
||||
out2.close();
|
||||
array = out.toByteArray();
|
||||
expectEqual(array, new int[] {
|
||||
// magic
|
||||
0xac, 0xed,
|
||||
// version
|
||||
0x00, 0x05,
|
||||
// object
|
||||
0x73,
|
||||
// class desc "java.util.TreeMap"
|
||||
0x72, 0, 17, 'j', 'a', 'v', 'a', '.', 'u', 't', 'i', 'l', '.',
|
||||
'T', 'r', 'e', 'e', 'M', 'a', 'p',
|
||||
// serial version UID: 0x0cc1f64e2d266ae6
|
||||
0x0c, 0xc1, 0xf6, 0x3e, 0x2d, 0x25, 0x6a, 0xe6,
|
||||
// flags: SC_SERIALIZABLE | SC_WRITE_METHOD
|
||||
0x03,
|
||||
// 1 field: comparator
|
||||
0, 1, 'L', 0, 10, 'c', 'o', 'm', 'p', 'a', 'r', 'a', 't', 'o', 'r',
|
||||
0x74, 0, 22, 'L', 'j', 'a', 'v', 'a', '/', 'u', 't', 'i', 'l', '/',
|
||||
'C', 'o', 'm', 'p', 'a', 'r', 'a', 't', 'o', 'r', ';',
|
||||
// class annotation
|
||||
0x78,
|
||||
// super class desc
|
||||
0x70,
|
||||
// classdata[]: NULL
|
||||
0x70,
|
||||
// custom TreeMap data writte by TreeMap#writeObject
|
||||
0x77, 4, 0x00 , 0x00, 0x00, 0x01, // (int)1 (== map.size())
|
||||
0x74, 0, 3, 'k', 'e', 'y', // "key"
|
||||
0x74, 0, 5, 'v', 'a', 'l', 'u', 'e', // "value"
|
||||
// end block data
|
||||
0x78
|
||||
});
|
||||
map.put("Hello", "ween");
|
||||
in = new ByteArrayInputStream(array);
|
||||
in2 = new ObjectInputStream(in);
|
||||
map = (TreeMap)in2.readObject();
|
||||
in2.close();
|
||||
expectEqual(1, map.size());
|
||||
expectEqual("value", (String)map.get("key"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user