Java 日看一类(39)之IO包中的ObjectStreamException和OptionalDataException异常类

ObjectStreamException异常类继承自IOException



该类的类头注释如下:

* Superclass of all exceptions specific to Object Stream classes.

大意如下:

描述所有对象流类异常的顶级类



该类含有如下的成员变量:

序列化ID

private static final long serialVersionUID = 7260898174833392607L;


该类含有如下的成员方法:

构造方法(传入出错的类名)

protected ObjectStreamException(String classname) {
    super(classname);
}

构造方法(默认)

protected ObjectStreamException() {
    super();
}



该类作为对象流异常的顶级类,一般情况下被使用到的是其子类,如下面要提到的OptionalDataException



OptionalDataException继承自ObjectStreamException


该类的类头注释如下:

/**
 * Exception indicating the failure of an object read operation due to
 * unread primitive data, or the end of data belonging to a serialized
 * object in the stream.  This exception may be thrown in two cases:
 *
 * <ul>
 *   <li>An attempt was made to read an object when the next element in the
 *       stream is primitive data.  In this case, the OptionalDataException's
 *       length field is set to the number of bytes of primitive data
 *       immediately readable from the stream, and the eof field is set to
 *       false.
 *
 *   <li>An attempt was made to read past the end of data consumable by a
 *       class-defined readObject or readExternal method.  In this case, the
 *       OptionalDataException's eof field is set to true, and the length field
 *       is set to 0.
 * </ul>
 *
 * @author  unascribed
 * @since   JDK1.1
 */

大意如下:

该类表示读取对象操作失败,原因是无法读取流中对象的基本数据或者是序列化对象的末尾数据,该异常会因两种原因被抛出:

企图读取的对象下一部分是基础数据,在这种情况下,OptionalDataException的长度字段将会被设置为立刻可从流中读出的基础数据字节数,文件结束字段为false

企图使用类声明的readObject或者readExternal方法来读取数据末尾后的部分。在这种情况下,文件结束字段会被设置为true,并且长度字段设置为0



该类含有如下的成员变量:

序列化ID

private static final long serialVersionUID = -8011121865681257820L;

可立刻被读取出的byte长度

public int length;

文件结束标志符

public boolean eof;



该类含有如下的成员方法:

构造函数(第一种情况

OptionalDataException(int len) {
    eof = false;
    length = len;
}

构造函数(第二种情况

OptionalDataException(boolean end) {
    length = 0;
    eof = end;
}



该类的使用方式在注释中已经提到,在这里不再赘述(该类的注释写的有点模糊,说是数据末尾,又说是数据末尾后的非法空间,等到看到了该类的具体抛出位置再来补充和更新)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable. To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime. During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream. When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object. Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures: private void writeObject(java.io.ObjectOutputStream out) throws IOException private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException; private void readObjectNoData() throws ObjectStreamException;
07-23

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值