objectinputstream.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,655 行 · 第 1/4 页
JAVA
1,655 行
for (int i = 0; i < cast_array.length; i++)
cast_array[i] = this.realInputStream.readInt();
return;
}
if (clazz == Long.TYPE) {
long[] cast_array = (long[]) array;
for (int i = 0; i < cast_array.length; i++)
cast_array[i] = this.realInputStream.readLong();
return;
}
if (clazz == Short.TYPE) {
short[] cast_array = (short[]) array;
for (int i = 0; i < cast_array.length; i++)
cast_array[i] = this.realInputStream.readShort();
return;
}
} else {
Object[] cast_array = (Object[]) array;
for (int i = 0; i < cast_array.length; i++)
cast_array[i] = readObject();
}
}
private void readFields(
Object obj,
ObjectStreamField[] stream_fields,
boolean call_read_method,
ObjectStreamClass stream_osc)
throws ClassNotFoundException, IOException {
// DEBUGln ("In readFields");
if (call_read_method) {
// DEBUGln (" call_read_method is true");
fieldsAlreadyRead = false;
boolean oldmode = setBlockDataMode(true);
callReadMethod(obj, stream_osc.forClass());
setBlockDataMode(oldmode);
return;
}
ObjectStreamField[] real_fields =
ObjectStreamClass.lookup(stream_osc.forClass()).fields;
boolean default_initialize, set_value;
String field_name = null;
Class type = null;
ObjectStreamField stream_field = null;
ObjectStreamField real_field = null;
int stream_idx = 0;
int real_idx = 0;
while (stream_idx < stream_fields.length
&& real_idx < real_fields.length) {
default_initialize = false;
set_value = true;
if (stream_idx == stream_fields.length)
default_initialize = true;
else {
stream_field = stream_fields[stream_idx];
type = stream_field.getType();
}
if (real_idx == real_fields.length)
set_value = false;
else {
real_field = real_fields[real_idx];
type = real_field.getType();
field_name = real_field.getName();
}
if (set_value && !default_initialize) {
int comp_val = real_field.compareTo(stream_field);
if (comp_val < 0) {
default_initialize = true;
real_idx++;
} else if (comp_val > 0) {
set_value = false;
stream_idx++;
} else {
real_idx++;
stream_idx++;
}
}
try {
if (type == Boolean.TYPE) {
boolean value =
default_initialize
? false
: this.realInputStream.readBoolean();
if (!default_initialize && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
setBooleanField(obj, field_name, value);
} else if (type == Byte.TYPE) {
byte value =
default_initialize
? 0
: this.realInputStream.readByte();
if (!default_initialize && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
setByteField(obj, field_name, value);
} else if (type == Character.TYPE) {
char value =
default_initialize
? (char) 0
: this.realInputStream.readChar();
if (!default_initialize && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
setCharField(obj, field_name, value);
} else if (type == Double.TYPE) {
double value =
default_initialize
? 0
: this.realInputStream.readDouble();
if (!default_initialize && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
setDoubleField(obj, field_name, value);
} else if (type == Float.TYPE) {
float value =
default_initialize
? 0
: this.realInputStream.readFloat();
if (!default_initialize && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
setFloatField(obj, field_name, value);
} else if (type == Integer.TYPE) {
int value =
default_initialize ? 0 : this.realInputStream.readInt();
if (!default_initialize && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
setIntField(obj, field_name, value);
} else if (type == Long.TYPE) {
long value =
default_initialize
? 0
: this.realInputStream.readLong();
if (!default_initialize && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
setLongField(obj, field_name, value);
} else if (type == Short.TYPE) {
short value =
default_initialize
? (short) 0
: this.realInputStream.readShort();
if (!default_initialize && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
setShortField(obj, field_name, value);
} else {
Object value = default_initialize ? null : readObject();
if (set_value)
setObjectField(
obj,
field_name,
real_field.getTypeString(),
value);
}
} catch (NoSuchFieldError e) {
dumpElementln("XXXX " + field_name + " does not exist.");
}
}
}
// Toggles writing primitive data to block-data buffer.
private boolean setBlockDataMode(boolean on) {
// DEBUGln ("Setting block data mode to " + on);
boolean oldmode = this.readDataFromBlock;
this.readDataFromBlock = on;
if (on)
this.dataInputStream = this.blockDataInput;
else
this.dataInputStream = this.realInputStream;
return oldmode;
}
// returns a new instance of REAL_CLASS that has been constructed
// only to the level of CONSTRUCTOR_CLASS (a super class of REAL_CLASS)
private Object newObject(Class real_class, Class constructor_class) {
try {
Object obj = allocateObject(real_class);
callConstructor(constructor_class, obj);
return obj;
} catch (InstantiationException e) {
return null;
}
}
// runs all registered ObjectInputValidations in prioritized order
// on OBJ
private void invokeValidators() throws InvalidObjectException {
Object[] validators = new Object[this.validators.size()];
this.validators.copyInto(validators);
Arrays.sort(validators);
try {
for (int i = 0; i < validators.length; i++)
((ObjectInputValidation) validators[i]).validateObject();
} finally {
this.validators.removeAllElements();
}
}
// this native method is used to get access to the protected method
// of the same name in SecurityManger
private static ClassLoader currentClassLoader(SecurityManager sm) {
return null;
}
private static Field getField(Class klass, String name)
throws java.lang.NoSuchFieldException {
return klass.getDeclaredField(name);
}
private static Method getMethod(Class klass, String name, Class args[])
throws java.lang.NoSuchMethodException {
return klass.getDeclaredMethod(name, args);
}
private void callReadMethod(Object obj, Class klass) throws IOException {
try {
Class classArgs[] = { ObjectInputStream.class };
Method m = getMethod(klass, "readObject", classArgs);
if (m == null)
return;
Object args[] = { this };
m.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;
throw new IOException(
"Exception thrown from readObject() on "
+ klass
+ ": "
+ exception.getClass().getName());
} catch (Exception x) {
throw new IOException(
"Failure invoking readObject() on "
+ klass
+ ": "
+ x.getClass().getName());
}
}
private Object allocateObject(Class clazz)
throws InstantiationException {
return null;
}
private void callConstructor(Class clazz, Object obj) {
}
private void setBooleanField(Object obj, String field_name, boolean val) {
try {
Class klass = obj.getClass();
Field f = getField(klass, field_name);
f.setAccessible(true);
f.setBoolean(obj, val);
} catch (Exception _) {
}
}
private void setByteField(Object obj, String field_name, byte val) {
try {
Class klass = obj.getClass();
Field f = getField(klass, field_name);
f.setAccessible(true);
f.setByte(obj, val);
} catch (Exception _) {
}
}
private void setCharField(Object obj, String field_name, char val) {
try {
Class klass = obj.getClass();
Field f = getField(klass, field_name);
f.setAccessible(true);
f.setChar(obj, val);
} catch (Exception _) {
}
}
private void setDoubleField(Object obj, String field_name, double val) {
try {
Class klass = obj.getClass();
Field f = getField(klass, field_name);
f.setAccessible(true);
f.setDouble(obj, val);
} catch (Exception _) {
}
}
private void setFloatField(Object obj, String field_name, float val) {
try {
Class klass = obj.getClass();
Field f = getField(klass, field_name);
f.setAccessible(true);
f.setFloat(obj, val);
} catch (Exception _) {
}
}
private void setIntField(Object obj, String field_name, int val) {
try {
Class klass = obj.getClass();
Field f = getField(klass, field_name);
f.setAccessible(true);
f.setInt(obj, val);
} catch (Exception _) {
}
}
private void setLongField(Object obj, String field_name, long val) {
try {
Class klass = obj.getClass();
Field f = getField(klass, field_name);
f.setAccessible(true);
f.setLong(obj, val);
} catch (Exception _) {
}
}
private void setShortField(Object obj, String field_name, short val) {
try {
Class klass = obj.getClass();
Field f = getField(klass, field_name);
f.setAccessible(true);
f.setShort(obj, val);
} catch (Exception _) {
}
}
private void setObjectField(
Object obj,
String field_name,
String type_code,
Object val) {
try {
Class klass = obj.getClass();
Field f = getField(klass, field_name);
f.setAccessible(true);
// FIXME: We should check the type_code here
f.set(obj, val);
} catch (Exception _) {
}
}
private static final int BUFFER_SIZE = 1024;
private static final Class[] readObjectParams = { ObjectInputStream.class };
private DataInputStream realInputStream;
private DataInputStream dataInputStream;
private DataInputStream blockDataInput;
private int blockDataPosition;
private int blockDataBytes;
private byte[] blockData;
private boolean useSubclassMethod;
private int nextOID;
private boolean resolveEnabled;
private Hashtable objectLookupTable;
private Object currentObject;
private ObjectStreamClass currentObjectStreamClass;
private boolean readDataFromBlock;
private boolean isDeserializing;
private boolean fieldsAlreadyRead;
private Vector validators;
private static boolean dump = false;
private void dumpElement(String msg) {
}
private void dumpElementln(String msg) {
}
private void DEBUG(String msg) {
System.out.print(msg);
}
private void DEBUGln(String msg) {
System.out.println(msg);
}
}
// used to keep a prioritized list of object validators
class ValidatorAndPriority implements Comparable {
int priority;
ObjectInputValidation validator;
ValidatorAndPriority(ObjectInputValidation validator, int priority) {
this.priority = priority;
this.validator = validator;
}
public int compareTo(Object o) {
ValidatorAndPriority vap = (ValidatorAndPriority) o;
return this.priority - vap.priority;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?