I have a HashMap
I'd like to persist and have fast access to. I use Kryo to serialize the object
Kryo kryo = new Kryo();
MapSerializer serializer = new MapSerializer();
kryo.register(Location.class);
kryo.register(HashMap.class, serializer);
Output output = new Output(new FileOutputStream("src/main/resources/locations50K.kryo"));
kryo.writeObject(output, locationMap);
output.close();
I can successfully deserialize with
Input input = new Input(new FileInputStream("src/main/resources/locations50K.kryo"));
Map<String, Location> locationMap;
locationMap = kryo.readObject(input, HashMap.class);
input.close();
log.info(locationMap.size());
the log.info shows that I have 231,045 entries in my map.
Now, I would like to access my .kryo file after I compiled a *-jar-with-dependencies.jar (I'm using Maven). So, rather than a FileInputStream
that reads from src/main/resources/
, I use MyClass.class.getResourceAsStream
InputStream isr = MyClass.class.getResourceAsStream("/locations50K.kryo");
if(isr == null)
log.error("null input");
Input input = new Input(isr);
locationMap = kryo.readObject(input, HashMap.class);
input.close();
log.info(locationMap.size());
the log.error never shows and the log.info says I have 0 entries in my map. Why? isr
isn't null so it is reading something, Kryo just can't seem to deserialize it and doesn't provide any error.