简体   繁体   English

RandomFileAccess-java.io.StreamCorruptedException

[英]RandomFileAccess - java.io.StreamCorruptedException

I am trying to read 'n' bytes (serialized object) from file starting from position 'i' using. 我正在尝试从使用位置“ i”开始的文件中读取“ n”个字节(序列化对象)。 Here is my code snippet below, 这是我的以下代码段,

public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchAlgorithmException {
      String s = "XYZ";
      RandomAccessFile f = new RandomAccessFile("/home/Test.txt", "rw");
      f.write(s.getBytes());
      FingerPrint finger = new FingerPrint("ABCDEFG", "ABCD.com");
      Serializer ser = new Serializer();
      byte[] key = ser.serialize(finger);//Serializing the object
      f.seek(3);
      f.write(key);
      byte[] new1 = new byte[(int)f.length()-3];
      int i=0;
      for(i=3;f.read()!=-1;i++){
        f.seek(i);
        new1[i]=f.readByte();
      }
      FingerPrint finger2 = (FingerPrint) ser.deserialize(new1);//deserializing it
      System.out.println("After reading:"+finger2.getURL());
}

I am getting following exception, 我正在追随异常,

 Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 00000000
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:807)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:302)

Here is my serializer class, 这是我的序列化器类,

class Serializer {

public static byte[] serialize(Object obj) throws IOException {
    try(ByteArrayOutputStream b = new ByteArrayOutputStream()){
        try(ObjectOutputStream o = new ObjectOutputStream(b)){
            o.writeObject(obj);
        }
        return b.toByteArray();
    }
}

public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
    try(ByteArrayInputStream b = new ByteArrayInputStream(bytes)){
        try(ObjectInputStream o = new ObjectInputStream(b)){
            return o.readObject();
        }
    }
}

} }

If i don't write any string to the file, and only read the object ie start from 0 to eof, i am able to see the output. 如果我不向文件写入任何字符串,而仅读取对象(即从0到eof开始),则能够看到输出。 There are many such questions already asked but i want to know why it is not working when i write a object in particular location and read it back. 已经问过许多这样的问题,但是我想知道为什么当我在特定位置写入对象并将其读回时它不起作用。 May be i am doing something wrong, Please share your thoughts. 可能是我做错了,请分享您的想法。

Here is your problem: 这是您的问题:

  f.seek(3);
  f.write(key);
  byte[] new1 = new byte[(int)f.length()-3];
  int i=0;
  for(i=3;f.read()!=-1;i++){
          ^^^^^^^^

At this point the file pointer is at EOF since you just wrote the serialized bytes and have not moved the file pointer yet. 此时,文件指针位于EOF,因为您只写了序列化的字节,还没有移动文件指针。 This loop is unnecessary, you should seek(3) and then read directly into new1 as in 该循环是不必要的,您应该执行seek(3) ,然后直接将其读入new1

byte[] new1 = new byte[(int) f.length() - 3];
f.seek(3);
f.read(new1);
FingerPrint finger2 = (FingerPrint) ser.deserialize(new1);// deserializing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM