kaitai-io / kaitai_struct_java_runtime

Kaitai Struct: runtime for Java
MIT License
42 stars 18 forks source link

Add support for reading floating point formats (Java Runtime) #1

Closed LogicAndTrick closed 7 years ago

LogicAndTrick commented 7 years ago

I found Java's ByteBuffer class which supports converting byte arrays into float/double - it looks like it's got native support for short/int/long as well. Should we change all the readS methods to use the ByteBuffer class? Seems more maintainable than the hand-coded method. There's no built-in methods to deal with unsigned types so the readU methods will still need to be manual.

GreyCat commented 7 years ago

Actually, it might be a good idea to rewrite everything to use ByteBuffer, but there are quite a few traps along the way.

First of all, I'd like to start rewriting Java runtime with a solid benchmarking suite, to be able to measure which is actually faster or slower.

ByteBuffer can be be used even for reading things directly from local memory-mapped file. I've heard that it's really mapped to underlying mmap, if OS supports it, so it would be very fast on Linux and Solaris, but it may come down to slow emulation sometimes (Windows?) and in these cases regular Java buffered inputs work faster.

Also, ByteBuffer has a totally different implementation on Android - and it's a good idea to benchmark it too.

There are also a few interesting alternatives like using Unsafe - http://senior-java-developer.com/unsafe/ or even https://github.com/airlift/slice