我正在构建一个处理Binary De / Serialization的类。方法open()
会收到InputStream
和OutputStream
。这些是由另一个open()
方法创建的,该方法接收路径作为参数。 InputStream
实际上是ByteArrayInputStream
。
我已经做了一些测试来证明InputStream
到达带有内容的open()
方法 - 实际上是。但是当我尝试使用它设置ObjectInputStream
时,它不起作用。没有抛出异常,但是当我尝试从中读取字节时,它总是给我-1
。
BinaryStrategy类
public class BinaryStrategy implements SerializableStrategy{
public BinaryStrategy(){
try{
open("products.ser");
}catch(IOException ioe){
}
}
@Override
public void open(InputStream input, OutputStream output) throws IOException {
try{
this.ois = new ObjectInputStream(input);
}catch(Exception ioe){
System.out.println(ioe);
}
this.oos = new ObjectOutputStream(output);
}
@Override
public void writeObject(fpt.com.Product obj) throws IOException {
oos.writeObject(obj);
oos.flush();
}
@Override
public Product readObject() throws IOException {
Product read = new Product();
try{
read.readExternal(ois);
}catch(IOException | ClassNotFoundException exc){
System.out.println(exc);
}
return read;
}
}
interface SerializableStrategy(只是默认方法)
default void open(Path path) throws IOException {
if (path != null) {
ByteArrayInputStream in = null;
if (Files.exists(path)) {
byte[] data = Files.readAllBytes(path);
in = new ByteArrayInputStream(data);
}
OutputStream out = Files.newOutputStream(path);
open(in, out);
}
产品类
public class Product implements java.io.Externalizable {
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeLong(getId());
out.writeObject(getName());
out.writeObject(getPrice());
out.writeObject(getQuantity());
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.setId((Long)in.readLong());
this.setName((String) in.readObject());
this.setPrice((Double) in.readObject());
this.setQuantity((Integer) in.readObject());
}
我必须对其进行个性化,因为属性为SimpleProperty
s
在public void open(InputStream input, OutputStream output)
,我尝试按照以下步骤进行测试:
public void open(InputStream input, OutputStream output) throws IOException {
try{
System.out.println(input.available() + " " + input.read() + " " + input.read());
//is gives me: 181 172 237
//181 is the exact size of the file I have, so i think that the Output is ok
//172 237 - just some chars that are in the file
//I know that for now on it is going to give me an excepetion because
// of the position of the index that is reading. I did it just to test
this.ois = new ObjectInputStream(input);
}catch(Exception ioe){
System.out.println(ioe);
}
this.oos = new ObjectOutputStream(output);
}
然后另一个测试:
public void open(InputStream input, OutputStream output) throws IOException {
try{
this.ois = new ObjectInputStream(input);
System.out.println(ois.available() + " " + ois.read());
//here is where I am receiving -1 and 0 available bytes!
//so something is going wrong right here.
//i tried to just go on and try to read the object,
//but I got a EOFException, in other words, -1.
}catch(Exception ioe){
System.out.println(ioe);
}
this.oos = new ObjectOutputStream(output);
}
答案 0 :(得分:2)
请检查path
表示的文件是否写入了java对象。
从ObjectInputStream API doc https://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html
ObjectInputStream对先前使用ObjectOutputStream编写的基元数据和对象进行反序列化。
ObjectInputStream用于恢复先前序列化的对象。
如果您正在执行this.ois.readObject()
,并且获得-1
,则该文件可能不包含其中的对象。
更新:readObject
返回一个对象,而不是int
。如果您使用read
中的ois
方法,并且获得-1
,则该文件为空。
此外,您的文件可能包含 -1 作为其内容;)
答案 1 :(得分:0)
问题是我正在以错误的方式阅读ObjectInputStream
。它就像:
read.readExternal(ois);
但正确的方法是:
read = (Product)ois.readObject();
由于我这样做的例外情况,我认为问题在于使用ObjectInputStream
时构建ByteArrayInputStream
。
真是个大错! :d
感谢所有试图提供帮助的人。
答案 2 :(得分:0)
ObjectInputStream
,在内部使用BlockDataInputStream
执行其读取操作。当您调用read
时,这会读取一个数据块,而不仅仅是我们所期望的字节。它只读取一个字节,如果它是"块"
输出不是我所期望的。
但是,如果你看一下ObjectInputStream.read()
的代码,那就有道理了。
因此,在您的情况下,仅使用readObject
来恢复您的对象是有意义的。状态。
再次见到你的代码......
class SimpleJava {
public static void open(InputStream input, OutputStream output) throws IOException {
try {
ObjectInputStream ois = new ObjectInputStream(input);
System.out.println(ois.available());// 0
System.out.println(ois.available() + " " + ois.read() + " " + ois.read());// 0 -1 -1
// Reads the object even if the available returned 0
// and ois.read() returned -1
System.out.println("object:" + ois.readObject());// object:abcd
}
catch (Exception ioe) {
ioe.printStackTrace();
}
}
static void open(Path path) throws IOException {
if (path != null) {
ByteArrayInputStream in = null;
if (Files.exists(path)) {
byte[] data = Files.readAllBytes(path);
in = new ByteArrayInputStream(data);
}
OutputStream out = Files.newOutputStream(path);
open(in, out);
}
}
public static void main(String[] args) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("/home/pradhan/temp.object")));
oos.writeObject("abcd");//writes a string object for us to read later
oos.close();
//
open(FileSystems.getDefault().getPath("/home/user/temp.object"));
}
}
继承人的产出......
0
0 -1 -1
object:abcd