This blog is about Java (advanced Java topics like Reflection, Byte Code transformation, Code Generation), Maven, Web technologies, Raspberry Pi and IT in general.

Mittwoch, 12. September 2012

Java SimpleArchive with RandomAccessFile

 I had a quite simple problem. I want to read several hundred thumbnail images (movie posters) to display them in a Java program.

File system
The simplest approach is to save the images directly in the file system. It's working fine if all images are cached from the OS. But if not it takes several seconds to load all images. Another downside is that most of the files have a size of 6kb - 7kb (in a range of 3kb - 10kb). So you loose some space because the block size is usually 4kb. In my case it was about 25%.

Zip
The next idea was to save all images in a Zip archive. The standard Java library supports Zip archives directly. It looked very promising. It was quite easy to use and it worked fine. But there is one big problem. It's not possible to add files to an existing archive. You had always to recreate the complete file. It's no problem if the archive is small but if it gets bigger than this could consume quite a long time. I looked for other libraries and archive formats. But it seemed that there is no easy to use archive library which supported to add files to an existing archive. So I wrote my own very simple archive library with the RandomAccessFile API.

SimpleArchive (github)
It's very simple and very fast. It can add files to an existing archive without the need to recreate the file. It just adds the file(s) and rewrite the file index which is located at the end of the file. So it will be much faster by adding files to an archive than the Zip implementation. In a very basic test it read the files ~3 times faster than the the Zip implementation. If you want to know more about it just visit the github.

Keine Kommentare:

Kommentar veröffentlichen