1. /*
  2. * @(#)OutputStreamHook.java 1.11 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * Licensed Materials - Property of IBM
  9. * RMI-IIOP v1.0
  10. * Copyright IBM Corp. 1998 1999 All Rights Reserved
  11. *
  12. * US Government Users Restricted Rights - Use, duplication or
  13. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  14. */
  15. package com.sun.corba.se.internal.io;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectOutput;
  20. import java.util.Hashtable;
  21. public abstract class OutputStreamHook extends ObjectOutputStream
  22. {
  23. private HookPutFields putFields = null;
  24. /**
  25. * Since ObjectOutputStream.PutField methods specify no exceptions,
  26. * we are not checking for null parameters on put methods.
  27. */
  28. private class HookPutFields extends ObjectOutputStream.PutField
  29. {
  30. private Hashtable fields = new Hashtable();
  31. /**
  32. * Put the value of the named boolean field into the persistent field.
  33. */
  34. public void put(String name, boolean value){
  35. fields.put(name, new Boolean(value));
  36. }
  37. /**
  38. * Put the value of the named char field into the persistent fields.
  39. */
  40. public void put(String name, char value){
  41. fields.put(name, new Character(value));
  42. }
  43. /**
  44. * Put the value of the named byte field into the persistent fields.
  45. */
  46. public void put(String name, byte value){
  47. fields.put(name, new Byte(value));
  48. }
  49. /**
  50. * Put the value of the named short field into the persistent fields.
  51. */
  52. public void put(String name, short value){
  53. fields.put(name, new Short(value));
  54. }
  55. /**
  56. * Put the value of the named int field into the persistent fields.
  57. */
  58. public void put(String name, int value){
  59. fields.put(name, new Integer(value));
  60. }
  61. /**
  62. * Put the value of the named long field into the persistent fields.
  63. */
  64. public void put(String name, long value){
  65. fields.put(name, new Long(value));
  66. }
  67. /**
  68. * Put the value of the named float field into the persistent fields.
  69. *
  70. */
  71. public void put(String name, float value){
  72. fields.put(name, new Float(value));
  73. }
  74. /**
  75. * Put the value of the named double field into the persistent field.
  76. */
  77. public void put(String name, double value){
  78. fields.put(name, new Double(value));
  79. }
  80. /**
  81. * Put the value of the named Object field into the persistent field.
  82. */
  83. public void put(String name, Object value){
  84. fields.put(name, value);
  85. }
  86. /**
  87. * Write the data and fields to the specified ObjectOutput stream.
  88. */
  89. public void write(ObjectOutput out) throws IOException {
  90. OutputStreamHook hook = (OutputStreamHook)out;
  91. ObjectStreamField[] osfields = hook.getFieldsNoCopy();
  92. // Write the fields to the stream in the order
  93. // provided by the ObjectStreamClass. (They should
  94. // be sorted appropriately already.)
  95. for (int i = 0; i < osfields.length; i++) {
  96. Object value = fields.get(osfields[i].getName());
  97. hook.writeField(osfields[i], value);
  98. }
  99. }
  100. }
  101. abstract void writeField(ObjectStreamField field, Object value) throws IOException;
  102. public OutputStreamHook()
  103. throws java.io.IOException {
  104. super();
  105. }
  106. public void defaultWriteObject() throws IOException {
  107. defaultWriteObjectDelegate();
  108. }
  109. public abstract void defaultWriteObjectDelegate();
  110. public ObjectOutputStream.PutField putFields()
  111. throws IOException {
  112. putFields = new HookPutFields();
  113. return putFields;
  114. }
  115. abstract ObjectStreamField[] getFieldsNoCopy();
  116. public void writeFields()
  117. throws IOException {
  118. putFields.write(this);
  119. }
  120. }