169 |
{ |
{ |
170 |
if (useSubclassMethod) |
if (useSubclassMethod) |
171 |
{ |
{ |
172 |
writeObjectOverride (obj); |
writeObjectOverride (obj); |
173 |
return; |
return; |
174 |
} |
} |
175 |
|
|
176 |
boolean was_serializing = isSerializing; |
boolean was_serializing = isSerializing; |
|
|
|
177 |
boolean old_mode = setBlockDataMode (false); |
boolean old_mode = setBlockDataMode (false); |
|
|
|
178 |
try |
try |
179 |
{ |
{ |
180 |
isSerializing = true; |
isSerializing = true; |
181 |
boolean replaceDone = false; |
boolean replaceDone = false; |
182 |
|
Object replacedObject = null; |
183 |
Object replacedObject = null; |
|
184 |
|
while (true) |
185 |
while (true) |
{ |
186 |
{ |
if (obj == null) |
187 |
if (obj == null) |
{ |
188 |
{ |
realOutput.writeByte (TC_NULL); |
189 |
realOutput.writeByte (TC_NULL); |
break; |
190 |
break; |
} |
191 |
} |
|
192 |
|
Integer handle = findHandle (obj); |
193 |
Integer handle = findHandle (obj); |
if (handle != null) |
194 |
if (handle != null) |
{ |
195 |
{ |
realOutput.writeByte (TC_REFERENCE); |
196 |
realOutput.writeByte (TC_REFERENCE); |
realOutput.writeInt (handle.intValue ()); |
197 |
realOutput.writeInt (handle.intValue ()); |
break; |
198 |
break; |
} |
199 |
} |
|
200 |
|
if (obj instanceof Class) |
201 |
if (obj instanceof Class) |
{ |
202 |
{ |
Class cl = (Class)obj; |
203 |
Class cl = (Class)obj; |
ObjectStreamClass osc = ObjectStreamClass.lookupForClassObject (cl); |
204 |
ObjectStreamClass osc = ObjectStreamClass.lookupForClassObject (cl); |
assignNewHandle (obj); |
205 |
assignNewHandle (obj); |
realOutput.writeByte (TC_CLASS); |
206 |
realOutput.writeByte (TC_CLASS); |
if (!osc.isProxyClass) |
207 |
if (!osc.isProxyClass) |
{ |
208 |
{ |
writeObject (osc); |
209 |
writeObject (osc); |
} |
210 |
} |
else |
211 |
else |
{ |
212 |
{ |
realOutput.writeByte (TC_PROXYCLASSDESC); |
213 |
realOutput.writeByte (TC_PROXYCLASSDESC); |
Class[] intfs = cl.getInterfaces(); |
214 |
Class[] intfs = cl.getInterfaces(); |
realOutput.writeInt(intfs.length); |
215 |
realOutput.writeInt(intfs.length); |
for (int i = 0; i < intfs.length; i++) |
216 |
for (int i = 0; i < intfs.length; i++) |
realOutput.writeUTF(intfs[i].getName()); |
|
realOutput.writeUTF(intfs[i].getName()); |
|
217 |
|
|
218 |
boolean oldmode = setBlockDataMode (true); |
boolean oldmode = setBlockDataMode (true); |
219 |
annotateProxyClass(cl); |
annotateProxyClass(cl); |
220 |
setBlockDataMode (oldmode); |
setBlockDataMode (oldmode); |
221 |
realOutput.writeByte(TC_ENDBLOCKDATA); |
realOutput.writeByte(TC_ENDBLOCKDATA); |
222 |
|
|
223 |
writeObject (osc.getSuper()); |
writeObject (osc.getSuper()); |
224 |
} |
} |
225 |
break; |
break; |
226 |
} |
} |
227 |
|
|
228 |
|
if (obj instanceof ObjectStreamClass) |
229 |
if (obj instanceof ObjectStreamClass) |
{ |
230 |
{ |
ObjectStreamClass osc = (ObjectStreamClass)obj; |
231 |
ObjectStreamClass osc = (ObjectStreamClass)obj; |
realOutput.writeByte (TC_CLASSDESC); |
232 |
realOutput.writeByte (TC_CLASSDESC); |
realOutput.writeUTF (osc.getName ()); |
233 |
realOutput.writeUTF (osc.getName ()); |
realOutput.writeLong (osc.getSerialVersionUID ()); |
234 |
realOutput.writeLong (osc.getSerialVersionUID ()); |
assignNewHandle (obj); |
235 |
assignNewHandle (obj); |
|
236 |
|
int flags = osc.getFlags (); |
237 |
int flags = osc.getFlags (); |
|
238 |
|
if (protocolVersion == PROTOCOL_VERSION_2 |
239 |
if (protocolVersion == PROTOCOL_VERSION_2 |
&& osc.isExternalizable ()) |
240 |
&& osc.isExternalizable ()) |
flags |= SC_BLOCK_DATA; |
241 |
flags |= SC_BLOCK_DATA; |
|
242 |
|
realOutput.writeByte (flags); |
243 |
realOutput.writeByte (flags); |
|
244 |
|
ObjectStreamField[] fields = osc.fields; |
245 |
ObjectStreamField[] fields = osc.fields; |
realOutput.writeShort (fields.length); |
246 |
realOutput.writeShort (fields.length); |
|
247 |
|
ObjectStreamField field; |
248 |
ObjectStreamField field; |
for (int i=0; i < fields.length; i++) |
249 |
for (int i=0; i < fields.length; i++) |
{ |
250 |
{ |
field = fields[i]; |
251 |
field = fields[i]; |
realOutput.writeByte (field.getTypeCode ()); |
252 |
realOutput.writeByte (field.getTypeCode ()); |
realOutput.writeUTF (field.getName ()); |
253 |
realOutput.writeUTF (field.getName ()); |
|
254 |
|
if (! field.isPrimitive ()) |
255 |
if (! field.isPrimitive ()) |
writeObject (field.getTypeString ()); |
256 |
writeObject (field.getTypeString ()); |
} |
257 |
} |
|
258 |
|
boolean oldmode = setBlockDataMode (true); |
259 |
boolean oldmode = setBlockDataMode (true); |
annotateClass (osc.forClass ()); |
260 |
annotateClass (osc.forClass ()); |
setBlockDataMode (oldmode); |
261 |
setBlockDataMode (oldmode); |
realOutput.writeByte (TC_ENDBLOCKDATA); |
262 |
realOutput.writeByte (TC_ENDBLOCKDATA); |
|
263 |
|
if (osc.isSerializable ()) |
264 |
if (osc.isSerializable ()) |
writeObject (osc.getSuper ()); |
265 |
writeObject (osc.getSuper ()); |
else |
266 |
else |
writeObject (null); |
267 |
writeObject (null); |
break; |
268 |
break; |
} |
269 |
} |
|
270 |
|
|
271 |
|
if ((replacementEnabled || obj instanceof Serializable) |
272 |
if ((replacementEnabled || obj instanceof Serializable) |
&& ! replaceDone) |
273 |
&& ! replaceDone) |
{ |
274 |
{ |
replacedObject = obj; |
275 |
replacedObject = obj; |
|
276 |
|
if (obj instanceof Serializable) |
277 |
if (obj instanceof Serializable) |
{ |
278 |
{ |
Method m = null; |
279 |
Method m = null; |
try |
280 |
try |
{ |
281 |
{ |
Class classArgs[] = {}; |
282 |
Class classArgs[] = {}; |
m = obj.getClass ().getDeclaredMethod ("writeReplace", |
283 |
m = obj.getClass ().getDeclaredMethod ("writeReplace", |
classArgs); |
284 |
classArgs); |
// m can't be null by definition since an exception would |
285 |
// m can't be null by definition since an exception would |
// have been thrown so a check for null is not needed. |
286 |
// have been thrown so a check for null is not needed. |
obj = m.invoke (obj, new Object[] {}); |
287 |
obj = m.invoke (obj, new Object[] {}); |
} |
288 |
} |
catch (NoSuchMethodException ignore) |
289 |
catch (NoSuchMethodException ignore) |
{ |
290 |
{ |
} |
291 |
} |
catch (IllegalAccessException ignore) |
292 |
catch (IllegalAccessException ignore) |
{ |
293 |
{ |
} |
294 |
} |
catch (InvocationTargetException ignore) |
295 |
catch (InvocationTargetException ignore) |
{ |
296 |
{ |
} |
297 |
} |
} |
298 |
} |
|
299 |
|
if (replacementEnabled) |
300 |
if (replacementEnabled) |
obj = replaceObject (obj); |
301 |
obj = replaceObject (obj); |
|
302 |
|
replaceDone = true; |
303 |
replaceDone = true; |
continue; |
304 |
continue; |
} |
305 |
} |
|
306 |
|
if (obj instanceof String) |
307 |
if (obj instanceof String) |
{ |
308 |
{ |
realOutput.writeByte (TC_STRING); |
309 |
realOutput.writeByte (TC_STRING); |
assignNewHandle (obj); |
310 |
assignNewHandle (obj); |
realOutput.writeUTF ((String)obj); |
311 |
realOutput.writeUTF ((String)obj); |
break; |
312 |
break; |
} |
313 |
} |
|
314 |
|
Class clazz = obj.getClass (); |
315 |
Class clazz = obj.getClass (); |
ObjectStreamClass osc = ObjectStreamClass.lookupForClassObject (clazz); |
316 |
ObjectStreamClass osc = ObjectStreamClass.lookupForClassObject (clazz); |
if (osc == null) |
317 |
if (osc == null) |
throw new NotSerializableException (clazz.getName ()); |
318 |
throw new NotSerializableException (clazz.getName ()); |
|
319 |
|
if (clazz.isArray ()) |
320 |
if (clazz.isArray ()) |
{ |
321 |
{ |
realOutput.writeByte (TC_ARRAY); |
322 |
realOutput.writeByte (TC_ARRAY); |
writeObject (osc); |
323 |
writeObject (osc); |
assignNewHandle (obj); |
324 |
assignNewHandle (obj); |
writeArraySizeAndElements (obj, clazz.getComponentType ()); |
325 |
writeArraySizeAndElements (obj, clazz.getComponentType ()); |
break; |
326 |
break; |
} |
327 |
} |
|
328 |
|
realOutput.writeByte (TC_OBJECT); |
329 |
realOutput.writeByte (TC_OBJECT); |
writeObject (osc); |
330 |
writeObject (osc); |
|
331 |
|
if (replaceDone) |
332 |
if (replaceDone) |
assignNewHandle (replacedObject); |
333 |
assignNewHandle (replacedObject); |
else |
334 |
else |
assignNewHandle (obj); |
335 |
assignNewHandle (obj); |
|
336 |
|
if (obj instanceof Externalizable) |
337 |
if (obj instanceof Externalizable) |
{ |
338 |
{ |
if (protocolVersion == PROTOCOL_VERSION_2) |
339 |
if (protocolVersion == PROTOCOL_VERSION_2) |
setBlockDataMode (true); |
340 |
setBlockDataMode (true); |
|
341 |
|
((Externalizable)obj).writeExternal (this); |
342 |
((Externalizable)obj).writeExternal (this); |
|
343 |
|
if (protocolVersion == PROTOCOL_VERSION_2) |
344 |
if (protocolVersion == PROTOCOL_VERSION_2) |
{ |
345 |
{ |
setBlockDataMode (false); |
346 |
setBlockDataMode (false); |
realOutput.writeByte (TC_ENDBLOCKDATA); |
347 |
realOutput.writeByte (TC_ENDBLOCKDATA); |
} |
348 |
} |
|
349 |
|
break; |
350 |
break; |
} |
351 |
} |
|
352 |
|
if (obj instanceof Serializable) |
353 |
if (obj instanceof Serializable) |
{ |
354 |
{ |
currentObject = obj; |
355 |
currentObject = obj; |
ObjectStreamClass[] hierarchy = |
356 |
ObjectStreamClass[] hierarchy = |
ObjectStreamClass.getObjectStreamClasses (clazz); |
357 |
ObjectStreamClass.getObjectStreamClasses (clazz); |
|
358 |
|
boolean has_write; |
359 |
boolean has_write; |
for (int i=0; i < hierarchy.length; i++) |
360 |
for (int i=0; i < hierarchy.length; i++) |
{ |
361 |
{ |
currentObjectStreamClass = hierarchy[i]; |
362 |
currentObjectStreamClass = hierarchy[i]; |
|
363 |
|
fieldsAlreadyWritten = false; |
364 |
fieldsAlreadyWritten = false; |
has_write = currentObjectStreamClass.hasWriteMethod (); |
365 |
has_write = currentObjectStreamClass.hasWriteMethod (); |
|
366 |
|
writeFields (obj, currentObjectStreamClass.fields, |
367 |
writeFields (obj, currentObjectStreamClass.fields, |
has_write); |
368 |
has_write); |
|
369 |
|
// if (has_write) |
370 |
// if (has_write) |
// { |
371 |
// { |
// drain (); |
372 |
// drain (); |
// realOutput.writeByte (TC_ENDBLOCKDATA); |
373 |
// realOutput.writeByte (TC_ENDBLOCKDATA); |
// } |
374 |
// } |
} |
375 |
} |
|
376 |
|
currentObject = null; |
377 |
currentObject = null; |
currentObjectStreamClass = null; |
378 |
currentObjectStreamClass = null; |
currentPutField = null; |
379 |
currentPutField = null; |
break; |
380 |
break; |
} |
|
} |
|
381 |
|
|
382 |
throw new NotSerializableException (clazz.getName ()); |
throw new NotSerializableException (clazz.getName ()); |
383 |
} // end pseudo-loop |
} // end pseudo-loop |
384 |
} |
} |
385 |
catch (IOException e) |
catch (IOException e) |
386 |
{ |
{ |
387 |
realOutput.writeByte (TC_EXCEPTION); |
realOutput.writeByte (TC_EXCEPTION); |
388 |
reset (true); |
reset (true); |
389 |
|
|
390 |
setBlockDataMode (false); // ?? |
setBlockDataMode (false); // ?? |
391 |
try |
try |
392 |
{ |
{ |
393 |
writeObject (e); |
writeObject (e); |
394 |
} |
} |
395 |
catch (IOException ioe) |
catch (IOException ioe) |
396 |
{ |
{ |
397 |
throw new StreamCorruptedException ("Exception " + ioe + " thrown while exception was being written to stream."); |
throw new StreamCorruptedException ("Exception " + ioe + " thrown while exception was being written to stream."); |
398 |
} |
} |
399 |
|
|
400 |
reset (true); |
reset (true); |
401 |
} |
} |
402 |
finally |
finally |
403 |
{ |
{ |
404 |
isSerializing = was_serializing; |
isSerializing = was_serializing; |
405 |
|
|
406 |
setBlockDataMode (old_mode); |
setBlockDataMode (old_mode); |
407 |
} |
} |
408 |
} |
} |
409 |
|
|
541 |
@see java.io.ObjectInputStream#resolveClass (java.io.ObjectStreamClass) |
@see java.io.ObjectInputStream#resolveClass (java.io.ObjectStreamClass) |
542 |
*/ |
*/ |
543 |
protected void annotateClass (Class cl) throws IOException |
protected void annotateClass (Class cl) throws IOException |
544 |
{} |
{ |
545 |
|
} |
546 |
|
|
547 |
protected void annotateProxyClass(Class cl) throws IOException |
protected void annotateProxyClass(Class cl) throws IOException |
548 |
{} |
{ |
549 |
|
} |
550 |
|
|
551 |
/** |
/** |
552 |
Allows subclasses to replace objects that are written to the |
Allows subclasses to replace objects that are written to the |
645 |
throw new NotActiveException ("Subclass of ObjectOutputStream must implement writeObjectOverride"); |
throw new NotActiveException ("Subclass of ObjectOutputStream must implement writeObjectOverride"); |
646 |
} |
} |
647 |
|
|
648 |
|
|
649 |
/** |
/** |
650 |
@see java.io.DataOutputStream#write (int) |
@see java.io.DataOutputStream#write (int) |
651 |
*/ |
*/ |
987 |
= currentObjectStreamClass.getField (name); |
= currentObjectStreamClass.getField (name); |
988 |
if (field == null) |
if (field == null) |
989 |
throw new IllegalArgumentException (); |
throw new IllegalArgumentException (); |
|
|
|
990 |
if (value != null && |
if (value != null && |
991 |
! field.getType ().isAssignableFrom (value.getClass ())) |
! field.getType ().isAssignableFrom (value.getClass ())) |
992 |
throw new IllegalArgumentException (); |
throw new IllegalArgumentException (); |
1207 |
|
|
1208 |
drain(); |
drain(); |
1209 |
boolean oldmode = writeDataAsBlocks; |
boolean oldmode = writeDataAsBlocks; |
|
|
|
1210 |
writeDataAsBlocks = on; |
writeDataAsBlocks = on; |
1211 |
|
|
1212 |
if (on) |
if (on) |
1261 |
} |
} |
1262 |
} |
} |
1263 |
} |
} |
|
|
|