⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectoutputstream.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	  throws IllegalArgumentException	{	  if (TypeSignature.getEncodingOfClass(field.getType()).charAt(0)	      != type)	    throw new IllegalArgumentException();	}      };    // end PutFieldImpl    return currentPutField;  }  public void writeFields() throws IOException  {    if (currentPutField == null)      throw new NotActiveException("writeFields can only be called after putFields has been called");    markFieldsWritten();    currentPutField.write(this);  }  // write out the block-data buffer, picking the correct header  // depending on the size of the buffer  private void writeBlockDataHeader(int size) throws IOException  {    if (size < 256)      {	realOutput.writeByte(TC_BLOCKDATA);	realOutput.write(size);      }    else      {	realOutput.writeByte(TC_BLOCKDATALONG);	realOutput.writeInt(size);      }  }  // lookup the handle for OBJ, return null if OBJ doesn't have a  // handle yet  private Integer findHandle(Object obj)  {    return (Integer)OIDLookupTable.get(new ObjectIdentityWrapper(obj));  }  // assigns the next availible handle to OBJ  private int assignNewHandle(Object obj)  {    OIDLookupTable.put(new ObjectIdentityWrapper(obj),		       new Integer(nextOID));    return nextOID++;  }  // resets mapping from objects to handles  private void clearHandles()  {    nextOID = baseWireHandle;    OIDLookupTable.clear();  }  // write out array size followed by each element of the array  private void writeArraySizeAndElements(Object array, Class clazz)    throws IOException  {    int length = Array.getLength(array);    if (clazz.isPrimitive())      {	if (clazz == Boolean.TYPE)	  {	    boolean[] cast_array = (boolean[])array;	    realOutput.writeInt (length);	    for (int i = 0; i < length; i++)	      realOutput.writeBoolean(cast_array[i]);	    return;	  }	if (clazz == Byte.TYPE)	  {	    byte[] cast_array = (byte[])array;	    realOutput.writeInt(length);	    realOutput.write(cast_array, 0, length);	    return;	  }	if (clazz == Character.TYPE)	  {	    char[] cast_array = (char[])array;	    realOutput.writeInt(length);	    for (int i = 0; i < length; i++)	      realOutput.writeChar(cast_array[i]);	    return;	  }	if (clazz == Double.TYPE)	  {	    double[] cast_array = (double[])array;	    realOutput.writeInt(length);	    for (int i = 0; i < length; i++)	      realOutput.writeDouble(cast_array[i]);	    return;	  }	if (clazz == Float.TYPE)	  {	    float[] cast_array = (float[])array;	    realOutput.writeInt(length);	    for (int i = 0; i < length; i++)	      realOutput.writeFloat(cast_array[i]);	    return;	  }	if (clazz == Integer.TYPE)	  {	    int[] cast_array = (int[])array;	    realOutput.writeInt(length);	    for (int i = 0; i < length; i++)	      realOutput.writeInt(cast_array[i]);	    return;	  }	if (clazz == Long.TYPE)	  {	    long[] cast_array = (long[])array;	    realOutput.writeInt (length);	    for (int i = 0; i < length; i++)	      realOutput.writeLong(cast_array[i]);	    return;	  }	if (clazz == Short.TYPE)	  {	    short[] cast_array = (short[])array;	    realOutput.writeInt (length);	    for (int i = 0; i < length; i++)	      realOutput.writeShort(cast_array[i]);	    return;	  }      }    else      {	Object[] cast_array = (Object[])array;	realOutput.writeInt(length);	for (int i = 0; i < length; i++)	  writeObject(cast_array[i]);      }  }  // writes out FIELDS of OBJECT for the specified ObjectStreamClass.  // FIELDS are already in canonical order.  private void writeFields(Object obj, ObjectStreamClass osc)    throws IOException  {    ObjectStreamField[] fields = osc.fields;    boolean oldmode = setBlockDataMode(false);    String field_name;    Class type;    for (int i = 0; i < fields.length; i++)      {	field_name = fields[i].getName();	type = fields[i].getType();	if (dump)	  dumpElementln ("WRITE FIELD: " + field_name + " type=" + type);	if (type == Boolean.TYPE)	  realOutput.writeBoolean(getBooleanField(obj, osc.forClass(), field_name));	else if (type == Byte.TYPE)	  realOutput.writeByte(getByteField(obj, osc.forClass(), field_name));	else if (type == Character.TYPE)	  realOutput.writeChar(getCharField(obj, osc.forClass(), field_name));	else if (type == Double.TYPE)	  realOutput.writeDouble(getDoubleField(obj, osc.forClass(), field_name));	else if (type == Float.TYPE)	  realOutput.writeFloat(getFloatField(obj, osc.forClass(), field_name));	else if (type == Integer.TYPE)	  realOutput.writeInt(getIntField(obj, osc.forClass(), field_name));	else if (type == Long.TYPE)	  realOutput.writeLong(getLongField(obj, osc.forClass(), field_name));	else if (type == Short.TYPE)	  realOutput.writeShort(getShortField(obj, osc.forClass(), field_name));	else	  writeObject(getObjectField(obj, osc.forClass(), field_name,				     fields[i].getTypeString ()));      }    setBlockDataMode(oldmode);  }  // Toggles writing primitive data to block-data buffer.  // Package-private to avoid a trampoline constructor.  boolean setBlockDataMode(boolean on) throws IOException  {    if (on == writeDataAsBlocks)      return on;    drain();    boolean oldmode = writeDataAsBlocks;    writeDataAsBlocks = on;    if (on)      dataOutput = blockDataOutput;    else      dataOutput = realOutput;    return oldmode;  }  private void callWriteMethod(Object obj, ObjectStreamClass osc)    throws IOException  {    currentPutField = null;    try      {        Object args[] = {this};        osc.writeObjectMethod.invoke(obj, args);      }    catch (InvocationTargetException x)      {        /* Rethrow if possible. */	Throwable exception = x.getTargetException();	if (exception instanceof RuntimeException)	  throw (RuntimeException) exception;	if (exception instanceof IOException)	  throw (IOException) exception;	IOException ioe	  = new IOException("Exception thrown from writeObject() on " +			    osc.forClass().getName() + ": " +                            exception.getClass().getName());	ioe.initCause(exception);	throw ioe;      }    catch (Exception x)      {	IOException ioe	  = new IOException("Failure invoking writeObject() on " +			    osc.forClass().getName() + ": " +			    x.getClass().getName());	ioe.initCause(x);	throw ioe;      }  }  private boolean getBooleanField(Object obj, Class klass, String field_name)    throws IOException  {    try      {	Field f = getField(klass, field_name);	boolean b = f.getBoolean(obj);	return b;      }    catch (IllegalArgumentException _)      {	throw new InvalidClassException	  ("invalid requested type for field " + field_name + " in class " + klass.getName());      }    catch (IOException e)      {	throw e;      }    catch (Exception _)      {	throw new IOException("Unexpected exception " + _);      }  }  private byte getByteField (Object obj, Class klass, String field_name)    throws IOException  {    try      {	Field f = getField (klass, field_name);	byte b = f.getByte (obj);	return b;      }    catch (IllegalArgumentException _)      {	throw new InvalidClassException	  ("invalid requested type for field " + field_name + " in class " + klass.getName());      }    catch (IOException e)      {	throw e;      }    catch (Exception _)      {	throw new IOException("Unexpected exception " + _);      }      }  private char getCharField (Object obj, Class klass, String field_name)    throws IOException  {    try      {	Field f = getField (klass, field_name);	char b = f.getChar (obj);	return b;      }    catch (IllegalArgumentException _)      {	throw new InvalidClassException	  ("invalid requested type for field " + field_name + " in class " + klass.getName());      }    catch (IOException e)      {	throw e;      }    catch (Exception _)      {	throw new IOException("Unexpected exception " + _);      }      }  private double getDoubleField (Object obj, Class klass, String field_name)    throws IOException  {    try      {	Field f = getField (klass, field_name);	double b = f.getDouble (obj);	return b;      }    catch (IllegalArgumentException _)      {	throw new InvalidClassException	  ("invalid requested type for field " + field_name + " in class " + klass.getName());      }    catch (IOException e)      {	throw e;      }    catch (Exception _)      {	throw new IOException("Unexpected exception " + _);      }      }  private float getFloatField (Object obj, Class klass, String field_name)    throws IOException  {    try      {	Field f = getField (klass, field_name);	float b = f.getFloat (obj);	return b;      }    catch (IllegalArgumentException _)      {	throw new InvalidClassException	  ("invalid requested type for field " + field_name + " in class " + klass.getName());      }    catch (IOException e)      {	throw e;      }    catch (Exception _)      {	throw new IOException("Unexpected exception " + _);      }  }  private int getIntField (Object obj, Class klass, String field_name)    throws IOException  {    try      {	Field f = getField (klass, field_name);	int b = f.getInt (obj);	return b;      }    catch (IllegalArgumentException _)      {	throw new InvalidClassException	  ("invalid requested type for field " + field_name + " in class " + klass.getName());      }    catch (IOException e)      {	throw e;      }    catch (Exception _)      {	throw new IOException("Unexpected exception " + _);      }  }  private long getLongField (Object obj, Class klass, String field_name)    throws IOException  {    try      {	Field f = getField (klass, field_name);	long b = f.getLong (obj);	return b;      }    catch (IllegalArgumentException _)      {	throw new InvalidClassException	  ("invalid requested type for field " + field_name + " in class " + klass.getName());      }    catch (IOException e)      {	throw e;      }    catch (Exception _)      {	throw new IOException("Unexpected exception " + _);      }      }  private short getShortField (Object obj, Class klass, String field_name)    throws IOException  {    try      {	Field f = getField (klass, field_name);	short b = f.getShort (obj);	return b;      }    catch (IllegalArgumentException _)      {	throw new InvalidClassException	  ("invalid requested type for field " + field_name + " in class " + klass.getName());      }    catch (IOException e)      {       throw e;      }    catch (Exception _)      {	throw new IOException("Unexpected exception " + _);      }  }  private Object getObjectField (Object obj, Class klass, String field_name,				 String type_code) throws IOException  {    try      {	Field f = getField (klass, field_name);	ObjectStreamField of = new ObjectStreamField(f.getName(), f.getType());	/* if of is primitive something went wrong	 * in the check for primitive classes in writeFields.	 */	if (of.isPrimitive())	  throw new InvalidClassException	    ("invalid type code for " + field_name + " in class " + klass.getName() + " : object stream field is primitive");	if (!of.getTypeString().equals(type_code))	    throw new InvalidClassException		("invalid type code for " + field_name + " in class " + klass.getName() + " : object stream field " + of + " has type string " + of.getTypeString() + " instead of " + type_code);	Object o = f.get (obj);	// FIXME: We should check the type_code here	return o;      }    catch (IOException e)      {	throw e;      }    catch (Exception e)      {	throw new IOException ();      }      }  private Field getField (Class klass, String name)    throws java.io.InvalidClassException  {    try      {	final Field f = klass.getDeclaredField(name);	setAccessible.setMember(f);	AccessController.doPrivileged(setAccessible);	return f;      }    catch (java.lang.NoSuchFieldException e)      {	throw new InvalidClassException	  ("no field called " + name + " in class " + klass.getName());      }  }  private void dumpElementln (String msg)  {    for (int i = 0; i < depth; i++)      System.out.print (" ");    System.out.print (Thread.currentThread() + ": ");    System.out.println(msg);  }  // this value comes from 1.2 spec, but is used in 1.1 as well  private static final int BUFFER_SIZE = 1024;  private static int defaultProtocolVersion = PROTOCOL_VERSION_2;  private DataOutputStream dataOutput;  private boolean writeDataAsBlocks;  private DataOutputStream realOutput;  private DataOutputStream blockDataOutput;  private byte[] blockData;  private int blockDataCount;  private Object currentObject;  // Package-private to avoid a trampoline.  ObjectStreamClass currentObjectStreamClass;  private PutField currentPutField;  private boolean fieldsAlreadyWritten;  private boolean replacementEnabled;  private boolean isSerializing;  private int nextOID;  private Hashtable OIDLookupTable;  private int protocolVersion;  private boolean useSubclassMethod;  private SetAccessibleAction setAccessible = new SetAccessibleAction();  // The nesting depth for debugging output  private int depth = 0;  // Set if we're generating debugging dumps  private boolean dump = false;  private static final boolean DEBUG = false;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -