【问题标题】:What are these two extra bytes in an ObjectOutputStream file?ObjectOutputStream 文件中的这两个额外字节是什么?
【发布时间】:2014-05-30 01:17:44
【问题描述】:

我在处理之前提出的question 时遇到了这个问题。

这可能特定于ObjectInputStream,而不是一般的二进制读取,因此标题可能具有误导性。

基本上问题是这样的:作者将字符串的哈希映射序列化为双精度值。哈希图中每个条目的作者custom serialization format 非常简单

int n        // length of string key as a 4-byte integer
byte[n] key  // a string of length n
double value // the value associated with the key

现在由于某种原因,在序列化过程中,字符串之一2010-00-008.html 被序列化了两个额外的字节,如下所示。

因此,不是写入 16 个字节,而是写入了 18 个字节。这肯定会导致问题,因为它仍然说字符串是 16 字节长。

但是,出于某种原因,您可以将哈希映射写出来并完美地读回!似乎给定一个 18 字节的字符串,您可以读取 16 个字节并仍然读取整个内容。

测试代码

这是代码。它基本上是另一个问题中的代码,除了我制作它以便您应该能够更改路径并运行它。运行它后,您将获得一系列写入语句,然后是一系列读取语句。检查文件,您应该注意到字符串中的额外字节,但程序没有崩溃。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
public class Test {

    // customize the path as needed
    public static String path = "C:\\temp\\sample.dat";

    HashMap<String, Double> map = new HashMap<String, Double>();

    public Test() {
        map.put("2010-00-027.html",21732.994621513037); map.put("2010-00-020.html",3466.5169348296736); map.put("2010-00-051.html",12528.648992702407); map.put("2010-00-062.html",3354.8950010256385);
        map.put("2010-00-024.html",10295.095511718278); map.put("2010-00-052.html",5381.513344679818);  map.put("2010-00-007.html",16466.33813960735);  map.put("2010-00-017.html",9484.969198176652);
        map.put("2010-00-054.html",15423.873112634772); map.put("2010-00-022.html",8123.842752870753);  map.put("2010-00-033.html",21238.496665104063); map.put("2010-00-028.html",7578.792651786424);
        map.put("2010-00-048.html",3566.4118233046393); map.put("2010-00-040.html",2681.0799941861724); map.put("2010-00-049.html",14308.090890746222); map.put("2010-00-058.html",5911.342406606804);
        map.put("2010-00-045.html",2284.118716145881);  map.put("2010-00-031.html",2859.565771680721);  map.put("2010-00-046.html",4555.187022907964);  map.put("2010-00-036.html",8479.709295569426);
        map.put("2010-00-061.html",846.8292195815125);  map.put("2010-00-023.html",14108.644025417952); map.put("2010-00-041.html",22686.232732684934); map.put("2010-00-025.html",9513.539663409734);
        map.put("2010-00-012.html",459.6427911376829);  map.put("2010-00-005.html",0.0);    map.put("2010-00-013.html",2646.403220496738);  map.put("2010-00-065.html",5808.86423609936);
        map.put("2010-00-056.html",12154.250518054876); map.put("2010-00-008.html",10811.15198506469);  map.put("2010-00-042.html",9271.006516004005);  map.put("2010-00-000.html",4387.4162586468965);
        map.put("2010-00-059.html",4456.211623469774);  map.put("2010-00-055.html",3534.7511584735325); map.put("2010-00-057.html",8745.640098512009);  map.put("2010-00-032.html",4993.295735075575);
        map.put("2010-00-021.html",3852.5805998017922); map.put("2010-00-043.html",4108.020033536286);  map.put("2010-00-053.html",2.2446400279239946); map.put("2010-00-030.html",17853.541210836203);
    }

    public void write() { 
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
            oos.writeInt(map.size()); // write size of the map
            for (Map.Entry<String, Double> entry : map.entrySet()) { // iterate entries
                System.out.println("writing ("+ entry.getKey() +","+ entry.getValue() +")");
                byte[] bytes = entry.getKey().getBytes();
                oos.writeInt(bytes.length); // length of key string
                oos.write(bytes); // key string bytes
                oos.writeDouble(entry.getValue()); // value
            }
            oos.close();
        } catch (Exception e) {

        }
    }

    public void read() {
        try {
            FileInputStream f = new FileInputStream(path);
            ObjectInputStream ois = new ObjectInputStream(f);
            int size = ois.readInt(); // read size of the map
            HashMap<String, Double> newMap = new HashMap<>(size);
            for (int i = 0; i < size; i++) { // iterate entries
                int length = ois.readInt(); // length of key string
                byte[] bytes = new byte[length];
                ois.readFully(bytes, 0, length);
                //ois.read(bytes);
                String key = new String(bytes);
                double value = ois.readDouble(); // value
                newMap.put(key, value);
                System.out.println("read ("+ key +","+ value +")");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }        
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.write();
        t.read();
    }
}

【问题讨论】:

  • 我对这个问题投了反对票,因为我已经纠正了几个不准确和遗漏的地方,这使得它无法以原始形式回答。幸运的是,我一直在关注引发这个问题的原始问题。
  • 如果您只想将字符串直接写入文件,请考虑使用DataOutputStream 而不是ObjectOutputStream(以及DataInputStream 而不是ObjectInputStream)。它会完全按照你告诉它写的东西写,没有任何聪明之处。

标签: java string serialization objectinputstream


【解决方案1】:

您需要阅读Protocol chapter of the Object Serialization Specification。除了实际数据之外,流还充满了类型和块标记。这是其中之一,在正确读取流时被ObjectInputStream过滤掉。

EDIT额外的字节是77 64,这意味着TC_BLOCK_DATA的大小为0x64.

【讨论】:

  • 现在阅读。觉得很奇怪很有趣。
  • @MxyL 当您不再光顾这部主要作品时,您可能会认为您总是可以先阅读它,而不是猜测。
猜你喜欢
  • 2018-07-20
  • 2012-07-22
  • 2021-12-07
  • 2014-04-18
  • 2020-07-07
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
  • 2014-05-03
相关资源
最近更新 更多