1. /*
  2. * @(#)InputStreamHook.java 1.15 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.NotActiveException;
  18. import java.io.InputStream;
  19. import java.io.ObjectInputStream;
  20. import java.util.*;
  21. import java.lang.reflect.Array;
  22. import java.lang.reflect.Constructor;
  23. public abstract class InputStreamHook extends ObjectInputStream
  24. {
  25. private class HookGetFields extends ObjectInputStream.GetField {
  26. private Map fields = null;
  27. HookGetFields(Map fields){
  28. this.fields = fields;
  29. }
  30. /**
  31. * Get the ObjectStreamClass that describes the fields in the stream.
  32. *
  33. * REVISIT! This doesn't work since we have our own ObjectStreamClass.
  34. */
  35. public java.io.ObjectStreamClass getObjectStreamClass() {
  36. return null;
  37. }
  38. /**
  39. * Return true if the named field is defaulted and has no value
  40. * in this stream.
  41. */
  42. public boolean defaulted(String name)
  43. throws IOException, IllegalArgumentException {
  44. return (!fields.containsKey(name));
  45. }
  46. /**
  47. * Get the value of the named boolean field from the persistent field.
  48. */
  49. public boolean get(String name, boolean defvalue)
  50. throws IOException, IllegalArgumentException {
  51. if (defaulted(name))
  52. return defvalue;
  53. else return ((Boolean)fields.get(name)).booleanValue();
  54. }
  55. /**
  56. * Get the value of the named char field from the persistent fields.
  57. */
  58. public char get(String name, char defvalue)
  59. throws IOException, IllegalArgumentException {
  60. if (defaulted(name))
  61. return defvalue;
  62. else return ((Character)fields.get(name)).charValue();
  63. }
  64. /**
  65. * Get the value of the named byte field from the persistent fields.
  66. */
  67. public byte get(String name, byte defvalue)
  68. throws IOException, IllegalArgumentException {
  69. if (defaulted(name))
  70. return defvalue;
  71. else return ((Byte)fields.get(name)).byteValue();
  72. }
  73. /**
  74. * Get the value of the named short field from the persistent fields.
  75. */
  76. public short get(String name, short defvalue)
  77. throws IOException, IllegalArgumentException {
  78. if (defaulted(name))
  79. return defvalue;
  80. else return ((Short)fields.get(name)).shortValue();
  81. }
  82. /**
  83. * Get the value of the named int field from the persistent fields.
  84. */
  85. public int get(String name, int defvalue)
  86. throws IOException, IllegalArgumentException {
  87. if (defaulted(name))
  88. return defvalue;
  89. else return ((Integer)fields.get(name)).intValue();
  90. }
  91. /**
  92. * Get the value of the named long field from the persistent fields.
  93. */
  94. public long get(String name, long defvalue)
  95. throws IOException, IllegalArgumentException {
  96. if (defaulted(name))
  97. return defvalue;
  98. else return ((Long)fields.get(name)).longValue();
  99. }
  100. /**
  101. * Get the value of the named float field from the persistent fields.
  102. */
  103. public float get(String name, float defvalue)
  104. throws IOException, IllegalArgumentException {
  105. if (defaulted(name))
  106. return defvalue;
  107. else return ((Float)fields.get(name)).floatValue();
  108. }
  109. /**
  110. * Get the value of the named double field from the persistent field.
  111. */
  112. public double get(String name, double defvalue)
  113. throws IOException, IllegalArgumentException {
  114. if (defaulted(name))
  115. return defvalue;
  116. else return ((Double)fields.get(name)).doubleValue();
  117. }
  118. /**
  119. * Get the value of the named Object field from the persistent field.
  120. */
  121. public Object get(String name, Object defvalue)
  122. throws IOException, IllegalArgumentException {
  123. if (defaulted(name))
  124. return defvalue;
  125. else return fields.get(name);
  126. }
  127. public String toString(){
  128. return fields.toString();
  129. }
  130. }
  131. public InputStreamHook()
  132. throws IOException {
  133. super();
  134. }
  135. public void defaultReadObject()
  136. throws IOException, ClassNotFoundException, NotActiveException
  137. {
  138. defaultReadObjectDelegate();
  139. }
  140. public abstract void defaultReadObjectDelegate();
  141. abstract void readFields(java.util.Map fieldToValueMap)
  142. throws java.io.InvalidClassException, java.io.StreamCorruptedException,
  143. ClassNotFoundException, java.io.IOException;
  144. public ObjectInputStream.GetField readFields()
  145. throws IOException, ClassNotFoundException, NotActiveException {
  146. HashMap fieldValueMap = new HashMap();
  147. readFields(fieldValueMap);
  148. return new HookGetFields(fieldValueMap);
  149. }
  150. }