private Object readObject0(boolean unshared)throws IOException { booleanoldMode= bin.getBlockDataMode(); if (oldMode) { intremain= bin.currentBlockRemaining(); if (remain > 0) { thrownewOptionalDataException(remain); } elseif (defaultDataEnd) { /* * Fix for 4360508: stream is currently at the end of a field * value block written via default serialization; since there * is no terminating TC_ENDBLOCKDATA tag, simulate * end-of-custom-data behavior explicitly. */ thrownewOptionalDataException(true); } bin.setBlockDataMode(false); }
depth++; try { switch (tc) { case TC_NULL: return readNull();
case TC_REFERENCE: return readHandle(unshared);
case TC_CLASS: return readClass(unshared);
case TC_CLASSDESC: case TC_PROXYCLASSDESC: return readClassDesc(unshared);
case TC_STRING: case TC_LONGSTRING: return checkResolve(readString(unshared));
case TC_ARRAY: return checkResolve(readArray(unshared));
case TC_ENUM: return checkResolve(readEnum(unshared));
case TC_OBJECT: return checkResolve(readOrdinaryObject(unshared));
case TC_EXCEPTION: IOExceptionex= readFatalException(); thrownewWriteAbortedException("writing aborted", ex);
case TC_BLOCKDATA: case TC_BLOCKDATALONG: if (oldMode) { bin.setBlockDataMode(true); bin.peek(); // force header read thrownewOptionalDataException( bin.currentBlockRemaining()); } else { thrownewStreamCorruptedException( "unexpected block data"); }
case TC_ENDBLOCKDATA: if (oldMode) { thrownewOptionalDataException(true); } else { thrownewStreamCorruptedException( "unexpected end of block data"); }
privatevoidreadSerialData(Object obj, ObjectStreamClass desc) throws IOException { ObjectStreamClass.ClassDataSlot[] slots = desc.getClassDataLayout(); for (inti=0; i < slots.length; i++) { ObjectStreamClassslotDesc= slots[i].desc;
if (slots[i].hasData) { if (obj == null || handles.lookupException(passHandle) != null) { defaultReadFields(null, slotDesc); // skip field values } elseif (slotDesc.hasReadObjectMethod()) {//判断是否有重写的readobject SerialCallbackContextoldContext= curContext; if (oldContext != null) oldContext.check(); try { curContext = newSerialCallbackContext(obj, slotDesc);
bin.setBlockDataMode(true); slotDesc.invokeReadObject(obj, this);//调用重写的readObject } catch (ClassNotFoundException ex) { /* * In most cases, the handle table has already * propagated a CNFException to passHandle at this * point; this mark call is included to address cases * where the custom readObject method has cons'ed and * thrown a new CNFException of its own. */ handles.markException(passHandle, ex); } finally { curContext.setUsed(); if (oldContext!= null) oldContext.check(); curContext = oldContext; }
/* * defaultDataEnd may have been set indirectly by custom * readObject() method when calling defaultReadObject() or * readFields(); clear it to restore normal read behavior. */ defaultDataEnd = false; } else { defaultReadFields(obj, slotDesc);//进行数据填充 }