Created
May 18, 2016 09:37
-
-
Save jodastephen/2bb70e1f1180b030d46b5a6366c0a0c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// released to the public domain, no copyright applies | |
package com.opengamma.strata.calc; | |
import java.io.ByteArrayOutputStream; | |
import java.io.Externalizable; | |
import java.io.IOException; | |
import java.io.InvalidObjectException; | |
import java.io.ObjectInput; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutput; | |
import java.io.ObjectOutputStream; | |
import java.io.ObjectStreamException; | |
import java.io.Serializable; | |
import java.util.AbstractList; | |
import java.util.Objects; | |
import java.util.RandomAccess; | |
public class SerTest { | |
public static void main(String[] args) throws Exception { | |
ImmList<String> list1 = new ImmList(); | |
ImmList<String> list2 = new ImmList("a", "bb", "ccc"); | |
ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); | |
ObjectOutputStream out1 = new ObjectOutputStream(baos1); | |
out1.writeObject(list1); | |
out1.close(); | |
baos1.close(); | |
byte[] array1 = baos1.toByteArray(); | |
System.out.println(array1.length + " " + output(array1)); | |
ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); | |
ObjectOutputStream out2 = new ObjectOutputStream(baos2); | |
out2.writeObject(list1); | |
out2.writeObject(list2); | |
out2.close(); | |
baos2.close(); | |
byte[] array2 = baos2.toByteArray(); | |
System.out.println(array2.length + " " + output(array2)); | |
} | |
static String output(byte[] bytes) { | |
StringBuilder buf = new StringBuilder(); | |
for (byte b : bytes) { | |
if (b >= 32 && b < 127) { | |
buf.append((char) b); | |
} else { | |
String hex = Integer.toHexString(b); | |
buf.append("[" + (hex.startsWith("ffffff") ? hex.substring(6) : hex) + "]"); | |
} | |
} | |
return buf.toString(); | |
} | |
} | |
final class ImmList<E> extends AbstractList<E> implements RandomAccess, Serializable { | |
private final E[] elements; | |
@SafeVarargs | |
ImmList(E... input) { | |
@SuppressWarnings("unchecked") | |
E[] tmp = (E[]) new Object[input.length]; // implicit nullcheck of input | |
for (int i = 0; i < input.length; i++) { | |
tmp[i] = Objects.requireNonNull(input[i]); | |
} | |
this.elements = tmp; | |
} | |
@Override | |
public int size() { | |
return elements.length; | |
} | |
@Override | |
public E get(int index) { | |
return elements[index]; | |
} | |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { | |
throw new InvalidObjectException("not serial proxy"); | |
} | |
private Object writeReplace() { | |
return new Ser(Ser.IMM_LIST, elements); | |
} | |
} | |
//------------------------------------------------------------------------- | |
final class CollSer implements Serializable { | |
private static final long serialVersionUID = 6309168927139932177L; | |
static final int IMM_LIST = 1; | |
int flags; | |
Object[] array; | |
CollSer(int f, Object... a) { | |
flags = f; | |
array = a; | |
} | |
private Object readResolve() throws ObjectStreamException { | |
try { | |
if (array == null) { | |
throw new InvalidObjectException("null array"); | |
} | |
switch (flags & 0xff) { | |
case IMM_LIST: | |
return new ImmList(array); | |
default: | |
throw new InvalidObjectException(String.format("invalid flags 0x%x", flags)); | |
} | |
} catch (NullPointerException | IllegalArgumentException ex) { | |
InvalidObjectException ioe = new InvalidObjectException("invalid object"); | |
ioe.initCause(ex); | |
throw ioe; | |
} | |
} | |
} | |
//------------------------------------------------------------------------- | |
final class Ser implements Externalizable { | |
private static final long serialVersionUID = 6309168927139932177L; | |
static final byte IMM_LIST = 1; | |
byte type; | |
Object[] array; | |
Ser(byte f, Object[] a) { | |
type = f; | |
array = a; | |
} | |
@Override | |
public void writeExternal(ObjectOutput out) throws IOException { | |
out.writeByte(type); | |
switch (type) { | |
case IMM_LIST: { | |
out.writeInt(array.length); | |
for (Object object : array) { | |
out.writeObject(object); | |
} | |
} | |
break; | |
default: | |
throw new InvalidObjectException(String.format("invalid flags 0x%x", type)); | |
} | |
} | |
@Override | |
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { | |
byte type = in.readByte(); | |
switch (type) { | |
case IMM_LIST: { | |
int size = in.readInt(); | |
Object[] array = new Object[size]; | |
for (int i = 0; i < size; i++) { | |
array[i] = in.readObject(); | |
} | |
} | |
break; | |
default: | |
throw new InvalidObjectException(String.format("invalid flags 0x%x", type)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment