objectinputstream.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,655 行 · 第 1/4 页
JAVA
1,655 行
!= TC_ENDBLOCKDATA)
throw new IOException("No end of block data seen for class with readObject (ObjectInputStream) method.");
dumpElementln("yes");
} catch (EOFException e) {
dumpElementln("no, got EOFException");
} catch (IOException e) {
dumpElementln("no, got IOException");
}
}
}
this.currentObject = null;
this.currentObjectStreamClass = null;
ret_val = processResolution(obj, handle);
break;
}
case TC_RESET :
dumpElementln("RESET");
clearHandles();
ret_val = readObject();
break;
case TC_EXCEPTION :
{
dumpElement("EXCEPTION=");
Exception e = (Exception) readObject();
dumpElementln(e.toString());
clearHandles();
//try{
throw new WriteAbortedException(
"Exception thrown during writing of stream",
e);
//}catch(IOException _){
// ret_val = null;
//}
//break;
}
default :
throw new IOException(
"Unknown marker on stream: " + marker);
}
} finally {
setBlockDataMode(old_mode);
this.isDeserializing = was_deserializing;
if (!was_deserializing) {
//setBlockDataMode (true);
if (validators.size() > 0)
invokeValidators();
}
}
return ret_val;
}
/**
Reads the current objects non-transient, non-static fields from
the current class from the underlying output stream.
This method is intended to be called from within a object's
<code>private void readObject (ObjectInputStream)</code>
method.
@exception ClassNotFoundException The class that an object being
read in belongs to cannot be found.
@exception NotActiveException This method was called from a
context other than from the current object's and current class's
<code>private void readObject (ObjectInputStream)</code>
method.
@exception IOException Exception from underlying
<code>OutputStream</code>.
*/
public void defaultReadObject()
throws ClassNotFoundException, IOException, NotActiveException {
if (this.currentObject == null
|| this.currentObjectStreamClass == null)
throw new NotActiveException("defaultReadObject called by non-active class and/or object");
if (fieldsAlreadyRead)
throw new NotActiveException("defaultReadObject called but fields already read from stream (by defaultReadObject or readFields)");
boolean oldmode = setBlockDataMode(false);
readFields(
this.currentObject,
this.currentObjectStreamClass.fields,
false,
this.currentObjectStreamClass);
setBlockDataMode(oldmode);
fieldsAlreadyRead = true;
}
/**
Registers a <code>ObjectInputValidation</code> to be carried out
on the object graph currently being deserialized before it is
returned to the original caller of <code>readObject ()</code>.
The order of validation for multiple
<code>ObjectInputValidation</code>s can be controled using
<code>priority</code>. Validators with higher priorities are
called first.
@see java.io.ObjectInputValidation
@exception InvalidObjectException <code>validator</code> is
<code>null</code>
@exception NotActiveException an attempt was made to add a
validator outside of the <code>readObject</code> method of the
object currently being deserialized
*/
public void registerValidation(
ObjectInputValidation validator,
int priority)
throws InvalidObjectException, NotActiveException {
if (this.currentObject == null
|| this.currentObjectStreamClass == null)
throw new NotActiveException("registerValidation called by non-active class and/or object");
if (validator == null)
throw new InvalidObjectException("attempt to add a null ObjectInputValidation object");
this.validators.addElement(
new ValidatorAndPriority(validator, priority));
}
/**
Called when a class is being deserialized. This is a hook to
allow subclasses to read in information written by the
<code>annotateClass (Class)</code> method of an
<code>ObjectOutputStream</code>.
This implementation looks up the active call stack for a
<code>ClassLoader</code>; if a <code>ClassLoader</code> is found,
it is used to load the class associated with <code>osc</code>,
otherwise, the default system <code>ClassLoader</code> is used.
@exception IOException Exception from underlying
<code>OutputStream</code>.
@see java.io.ObjectOutputStream#annotateClass (java.lang.Class)
*/
protected Class resolveClass(ObjectStreamClass osc)
throws ClassNotFoundException, IOException {
// DEBUGln ("Resolving " + osc);
SecurityManager sm = System.getSecurityManager();
if (sm == null)
sm = new SecurityManager() {
};
ClassLoader cl = currentClassLoader(sm);
if (cl == null) {
// DEBUGln ("No class loader found");
return Class.forName(osc.getName());
} else {
// DEBUGln ("Using " + cl);
return cl.loadClass(osc.getName());
}
}
/**
Allows subclasses to resolve objects that are read from the
stream with other objects to be returned in their place. This
method is called the first time each object is encountered.
This method must be enabled before it will be called in the
serialization process.
@exception IOException Exception from underlying
<code>OutputStream</code>.
@see enableResolveObject (boolean)
*/
protected Object resolveObject(Object obj) throws IOException {
return obj;
}
protected Class resolveProxyClass(String[] intfs)
throws IOException, ClassNotFoundException {
SecurityManager sm = System.getSecurityManager();
if (sm == null)
sm = new SecurityManager() {
};
ClassLoader cl = currentClassLoader(sm);
Class[] clss = new Class[intfs.length];
if (cl == null) {
for (int i = 0; i < intfs.length; i++)
clss[i] = Class.forName(intfs[i]);
cl = ClassLoader.getSystemClassLoader();
} else
for (int i = 0; i < intfs.length; i++)
clss[i] = cl.loadClass(intfs[i]);
try {
return Proxy.getProxyClass(cl, clss);
} catch (IllegalArgumentException e) {
throw new ClassNotFoundException(null, e);
}
}
/**
If <code>enable</code> is <code>true</code> and this object is
trusted, then <code>resolveObject (Object)</code> will be called
in subsequent calls to <code>readObject (Object)</code>.
Otherwise, <code>resolveObject (Object)</code> will not be called.
@exception SecurityException This class is not trusted.
*/
protected boolean enableResolveObject(boolean enable)
throws SecurityException {
if (enable) {
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPermission(
new SerializablePermission("enableSubtitution"));
}
boolean old_val = this.resolveEnabled;
this.resolveEnabled = enable;
return old_val;
}
/**
Reads stream magic and stream version information from the
underlying stream.
@exception IOException Exception from underlying stream.
@exception StreamCorruptedException An invalid stream magic
number or stream version was read from the stream.
*/
protected void readStreamHeader()
throws IOException, StreamCorruptedException {
dumpElement("STREAM MAGIC ");
if (this.realInputStream.readShort() != STREAM_MAGIC)
throw new StreamCorruptedException("Invalid stream magic number");
dumpElementln("STREAM VERSION ");
if (this.realInputStream.readShort() != STREAM_VERSION)
throw new StreamCorruptedException("Invalid stream version number");
}
public int read() throws IOException {
if (this.readDataFromBlock) {
if (this.blockDataPosition >= this.blockDataBytes)
readNextBlock();
return (this.blockData[this.blockDataPosition++] & 0xff);
} else
return this.realInputStream.read();
}
public int read(byte[] data, int offset, int length) throws IOException {
if (this.readDataFromBlock) {
if (this.blockDataPosition + length > this.blockDataBytes) {
int remain = this.blockDataBytes - this.blockDataPosition;
if (remain != 0) {
System.arraycopy(
this.blockData,
this.blockDataPosition,
data,
offset,
remain);
offset += remain;
length -= remain;
}
readNextBlock();
}
System.arraycopy(
this.blockData,
this.blockDataPosition,
data,
offset,
length);
this.blockDataPosition += length;
return length;
} else
return this.realInputStream.read(data, offset, length);
}
public int available() throws IOException {
if (this.readDataFromBlock) {
if (this.blockDataPosition >= this.blockDataBytes)
readNextBlock();
return this.blockDataBytes - this.blockDataPosition;
} else
return this.realInputStream.available();
}
public void close() throws IOException {
this.realInputStream.close();
}
public boolean readBoolean() throws IOException {
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
boolean value = this.dataInputStream.readBoolean();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public byte readByte() throws IOException {
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
byte value = this.dataInputStream.readByte();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public int readUnsignedByte() throws IOException {
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
int value = this.dataInputStream.readUnsignedByte();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public short readShort() throws IOException {
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
short value = this.dataInputStream.readShort();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public int readUnsignedShort() throws IOException {
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
int value = this.dataInputStream.readUnsignedShort();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public char readChar() throws IOException {
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
char value = this.dataInputStream.readChar();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public int readInt() throws IOException {
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 4)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
int value = this.dataInputStream.readInt();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public long readLong() throws IOException {
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 8)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
long value = this.dataInputStream.readLong();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public float readFloat() throws IOException {
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 4)
switchmode = false;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?