Skip to content
daemianmack edited this page Nov 21, 2012 · 3 revisions

Fressian can cache values that it encounters, associating those values with a short (often single byte!) bytecode sequence. Subsequent writes of the same value will write the short bytecode sequence instead. Caching is off by default, but can be activated by passing true to writeObject:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer w = new FressianWriter(baos);
Set data = bunchOfData();
w.writeObject(data, true);
System.out.println("\tSerialized size of one bunch: " + baos.size());
w.writeObject(data, true);
System.out.println("\tSerialized size of two bunches: " + baos.size());

In the example above, the second write of data will only add one byte to the array, regardless of the size of data. You can see the full example here.

Tags are automatically cached

When you extend Fressian with a custom tag handler, the description of your type is automatically cached. So you can e.g. use good namespaced names for your tags, without worrying about the overhead of serializing them again and again for every instance.

Long data streams and cache resets

The use of caching makes reading a Fressian stream stateful -- readers have to start reading from where caching began in order to understand the cache codes. If you are writing a longer stream of data, and want readers to be able to start reading from some point in the middle, you can create such a point by calling resetCaches:

writer.resetCaches();
Clone this wiki locally