Java: Efficiently converting an array of longs to an array of bytes

后端 未结 3 559
后悔当初
后悔当初 2021-01-21 20:22

I have an array of longs I want to write to disk. The most efficient disk I/O functions take in byte arrays, for example:

FileOutputStream.write(by         


        
3条回答
  •  梦谈多话
    2021-01-21 20:50

    OP here.

    I have thought of one approach: ByteBuffer.asLongBuffer() returns an instance of ByteBufferAsLongBufferB, a class which wraps ByteBuffer in an interface for treating the data as longs while properly managing endianness. I could extend ByteBufferAsLongBufferB, and add a method to return the raw byte buffer (which is protected).

    But this seems so esoteric and convoluted I feel there must be an easier way. Either that, or something in my approach is flawed.

提交回复
热议问题