????writeObject??readObject????
??????ArrayList?ж??????????????? writeObject??readObject??
?????????????????:
?????????л??????У?????????л??????ж?????writeObject ?? readObject ???????????????????????????? writeObject ?? readObject ??????????????????????л???????л???
??????????????????????????????? ObjectOutputStream ?? defaultWriteObject ??????? ObjectInputStream ?? defaultReadObject ??????
????????????? writeObject ?? readObject ??????????????????????л???????????????????л???????ж????????л????????
??????????????????????????????
????code 5
????private void readObject(java.io.ObjectInputStream s)
????throws java.io.IOException?? ClassNotFoundException {
????elementData = EMPTY_ELEMENTDATA;
????// Read in size?? and any hidden stuff
????s.defaultReadObject();
????// Read in capacity
????s.readInt(); // ignored
????if (size > 0) {
????// be like clone()?? allocate array based upon size not capacity
????ensureCapacityInternal(size);
????Object[] a = elementData;
????// Read in all elements in the proper order.
????for (int i=0; i<size; i++) {
????a[i] = s.readObject();
????}
????}
????}
????code 6
????private void writeObject(java.io.ObjectOutputStream s)
????throws java.io.IOException{
????// Write out element count?? and any hidden stuff
????int expectedModCount = modCount;
????s.defaultWriteObject();
????// Write out size as capacity for behavioural compatibility with clone()
????s.writeInt(size);
????// Write out all elements in the proper order.
????for (int i=0; i<size; i++) {
????s.writeObject(elementData[i]);
????}
????if (modCount != expectedModCount) {
????throw new ConcurrentModificationException();
????}
????}
??????????ArrayList?????????????????л????
????why transient
????ArrayList????????????飬???????????????????趨?????????????????????????????100?????????????????????????л?99??null??????????????л?????????????null?????????л???ArrayList??????????????transient??
????why writeObject and readObject
?????????????????????????????????????鱻???л??????????洢???????ArrayList???transient??????elementData?? ?????????????????????л??????л?????????е????????????????????????????дwriteObject ?? readObject?????????????е?????????????
????writeObject??????elementData?????е???????????浽???????ObjectOutputStream???С?
????readObject??????????????ObjectInputStream???ж??????????渳???elementData?????С?
???????????????????????????????????
????????????????л???????л?????
???????????????????л???????????writeObject ?? readObject????????????????????
???????ArrayList??д??writeObject ?? readObject ??????????????????????????????????e???
????????????????а???writeObject ?? readObject ??????????????????????????????????
????ObjectOutputStream
??????code 4?У?????????????????????л????????ObjectOutputStream??ObjectInputputStream??????????????????????????????????ArrayList?е?writeObject ?? readObject ????????????α?????????
?????????????????????ObjectOutputStream??writeObject????????
????writeObject ---> writeObject0 --->writeOrdinaryObject--->writeSerialData--->invokeWriteObject
???????????invokeWriteObject??
void invokeWriteObject(Object obj?? ObjectOutputStream out)
throws IOException?? UnsupportedOperationException
{
if (writeObjectMethod != null) {
try {
writeObjectMethod.invoke(obj?? new Object[]{ out });
} catch (InvocationTargetException ex) {
Throwable th = ex.getTargetException();
if (th instanceof IOException) {
throw (IOException) th;
} else {
throwMiscException(th);
}
} catch (IllegalAccessException ex) {
// should not occur?? as access checks have been suppressed
throw new InternalError(ex);
}
} else {
throw new UnsupportedOperationException();
}
}
????????writeObjectMethod.invoke(obj?? new Object[]{ out });????????????????????writeObjectMethod?????????????????????writeObjectMethod???
????class-defined writeObject method?? or null if none
????????????????У????????????????ArrayList?ж????writeObject?????????????????????????
???????????????????????????????????
?????????????а???writeObject ?? readObject ????????????????????????????????
???????????ObjectOutputStream??writeObject??????ObjectInputStream??readObject??????????????????????á?
????????????????????????ArrayList?????л?????????????????????????????????????
????Serializable?????????????????????????????????y????????????????л??????л??????
????Serializable??????壺
????public interface Serializable {
????}
???????????????code 1?е???Serializable??????????????code 2???????java.io.NotSerializableException??
???????????????????????????????ObjectOutputStream??writeObject????????
????writeObject ---> writeObject0 --->writeOrdinaryObject--->writeSerialData--->invokeWriteObject
????writeObject0?????????????δ???
if (obj instanceof String) {
writeString((String) obj?? unshared);
} else if (cl.isArray()) {
writeArray(obj?? desc?? unshared);
} else if (obj instanceof Enum) {
writeEnum((Enum<?>) obj?? desc?? unshared);
} else if (obj instanceof Serializable) {
writeOrdinaryObject(obj?? desc?? unshared);
} else {
if (extendedDebugInfo) {
throw new NotSerializableException(
cl.getName() + " " + debugInfoStack.toString());
} else {
throw new NotSerializableException(cl.getName());
}
}
????????????л???????????ж???????л??????????Enum??Array??Serializable????????????????????NotSerializableException??
???????
????1???????????????л?????????Serializable???????????NotSerializableException??????????????????л??????????л????????м?飬??????л????????????Enum??Array??Serializable???????е??κ?????
????2????????????????ù??????????????????????л???????С?
????3????????????writeObject ?? readObject ???????????????????л?????