-3

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 String writeMaterialMap(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

Sage M
  • 119
  • 1
  • 2
  • 9
  • The empty map you are seeing in the output is the one you pointlessly created yourself, not the result of `readObject()`, which threw an exception *instead* of returning a map, and you therefore didn't get as far as the typecast either. – user207421 Dec 05 '17 at 22:36
  • Yes!! I know that!! :P that's my problem. – Sage M Dec 06 '17 at 00:02
  • Your new code is also pointless. You don't need the `new HashMap<...>()` part at all. Just the `readObject()` and the typecast. You seem to be just guessing. – user207421 Dec 06 '17 at 02:58
  • I'm completely aware I can just inline the variable... That doesn't solve the problem – Sage M Dec 06 '17 at 12:55

1 Answers1

0

EOFE while casting a ObjectInputStream.readObject() to hashmap

No. The suggestion is absurd. Typecasts don't throw EOFExceptions. readXXX() methods do that. It happens in readObject(). Check the stack trace. Nothing to do with casting whatsoever.

Currently, I can get the hashmap

No you can't. You get an EOFException, not a HashMap. Again the suggestion is absurd.

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

There is nothing here that suggests any such thing. You created a pointless empty map, tried to overwrite it with the result of readObject(), got an EOFException reading the object stream, and printed your own empty map.

The only problem here is the pointless while(true) loop. If you're going to read a finite input in an infinite loop it is impossible not to reach end of file.

Remove it.

If the exception remains, your hex string is incomplete.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I added it with the intention of it being pointless, it has the exact same result when I remove it. – Sage M Dec 04 '17 at 03:30
  • A thoroughly incomprehensible strategy, but in that case your hex string is incomplete. – user207421 Dec 04 '17 at 03:41
  • Like I said, the output is exactly what was encoded. When I try to put it in a hashmap, it screws up. Would that have to do with anything? – Sage M Dec 04 '17 at 13:12
  • Nowhere did you say any such thing actually, but if the hex is 'exactly what was encoded', the encoding was incomplete, and like *I* said, the problem arises in `readObject()`, not when casting to `HashMap`. Show how you derived the hex, in your question, and please also provide the complete stack trace. – user207421 Dec 04 '17 at 14:49
  • provided write method – Sage M Dec 05 '17 at 16:40
  • Your stack trace proves my point completely. How was the hex string transported from `writeMaterialMap()` to `readMaterialMap()`? – user207421 Dec 05 '17 at 22:34
  • I entered it into SQL. - how come it errors at the variable initialization but not the system.out.print? Genuine question :p – Sage M Dec 06 '17 at 00:01
  • I've answered that, about five times. **The hex string is incomplete.** And I don't know what kind of error you are expecting from `System.out.println()`, or why. You *still* haven't *shown* how you passed the hex around. Telling us how you *think* it works is futile, as clearly it doesn't. – user207421 Dec 06 '17 at 03:06
  • I shouldn't have to show you how I passed it around. I literally just put it into SQL, retrieved it from SQL. I've said this. – Sage M Dec 06 '17 at 12:56
  • The evidence clearly shows that you did it wrong. You don't have to show it to anybody, but you don't have to get get your question answered or your problem solved either. It's up to you. – user207421 Dec 07 '17 at 04:54
  • Hey man I'm not here to argue with you. But I really need someone who can offer me a solution, not someone to point out problems. – Sage M Dec 08 '17 at 12:38