您的位置 首页 网络技术

EOFException while calling ObjectInputStream.readObject()

I need to get a hashmap from a hex string I’ve serialized. Currently, I can get the hashmap, but it creates an EOFException.

  • private static StringwriteMaterialMap(HashMap<String, Double> map) throws Exception {

    System.out.print(map);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);

    objectOutputStream.writeObject(map);
    objectOutputStream.close();

    byte[] bytes = byteArrayOutputStream.toByteArray();

    final StringBuilder stringBuilder = new StringBuilder();
    for (byte bytte : bytes) {
    stringBuilder.append(String.format("%02x", bytte));
    }

    System.out.print("Saving: " + stringBuilder.toString());

    return stringBuilder.toString();
    }

    @SuppressWarnings("unchecked")
    private static HashMap<String, Double> readMaterialMap(String hex) throws Exception {

    int length = hex.length();
    byte[] byteArray = new byte[length / 2];
    for (int i = 0; i < length; i += 2) {
    byteArray[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
    + Character.digit(hex.charAt(i + 1), 16));
    }

    ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteArray));
    System.out.print(objectInputStream.readObject());

    HashMap<String, Double> materials = new HashMap<>((Map<String, Double>) objectInputStream.readObject());

    System.out.print(materials);

    return materials;

    }



 

Currently, I get this:

{"A"=1}
{}
java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte
    at java.io.ObjectInputStream.readObject0
    at java.io.ObjectInputStream.readObject
    at me.MapHex.readMaterialMap(MineHex.java:66)

What I can see from this is: It can read the object, but the EOFE is preventing me from casting it to a HashMap. I need to cast it to a HashMap without an EOFE

猫叔总结了 适合新手操作的副业 《淘宝虚拟产品月入2万的 6个 细分类目》的电子书 仅供参考

如果你对虚拟产品比较感兴趣,可以点击:

淘宝卖什么虚拟产品赚钱(月入2万+)

花猫大叔

关于花猫大叔短视频创业 作者: 花猫大叔

热门文章