How to avoid duplication of code regarding primitive types?
If you're reading bulk primitives like your code seems to indicate, using ByteBuffer methods like asDoubleBuffer() or asShortBuffer() will offload some of the lowest level work.
Example:
public void readBytes( final byte[] out, final int offset, final int count, final ByteBuffer buffer ) {
buffer.get( out, offset, count ); // udates ByteBuffer `position` automatically
}
public void readShorts( final short[] out, final int offset, final int count, final ByteBuffer buffer ) {
ShortBuffer sb = buffer.asShortBuffer();
sb.get( out, offset, count ); // note that `count` reads two bytes for each `short`
}
(Code compiles but not tested!)