1

Topic: Record of heterogeneous types in ByteBuffer

There is the abstract structure which the code is presented by three variables: long, int [], double []. How most correctly to write down the data in ByteBuffer? The problem arose that such methods as ByteBuffer:: asDoubleBuffer () create a type which at record does not update position the initial buffer. Better to say, the following code is incorrect: public void write (WritableChannel channel, long a, int [] b, double [] c) {ByteBuffer buffer = ByteBuffer.allocate (BIG_ENOUGH); buffer.putLong (a); buffer.asIntBuffer ().put (b);//does not update buffer.position buffer.asDoubleBuffer ().put (c)//similarly buffer.flip ();//installs limit = 8 channel.write (buffer);//writes down only ' a '} Decisions: 1. It is possible to install manually value position call ByteBuffer:: position (int). 2. It is possible to use ByteBuffer:: wrap (), turning same byte [] with offset: byte [] buffer = new byte [BIG_ENOUGH]; ByteBuffer.wrap (buffer).putLong (a); ByteBuffer.wrap (buffer, Long. BYTES, BIG_ENOUGH - Long. BYTES).asIntBuffer ().put (b);... In the latter case value position to us is unimportant, because in the end we cause: channel.write (ByteBuffer.wrap (buffer)) it is obvious, as so and so it is possible to commit silly errors, and generally  usages ByteBuffer (and nio) disappears. You as though made?

2

Re: Record of heterogeneous types in ByteBuffer

M> buffer.asIntBuffer ().put (b);//does not update buffer.position M> buffer.asDoubleBuffer ().put (c)//similarly Read, on how many moved the pointer in the new buffer after record https://docs.oracle.com/javase/7/docs/a … l#position () And drift a position in old https://docs.oracle.com/javase/7/docs/a … l#position (int)

3

Re: Record of heterogeneous types in ByteBuffer

M>> buffer.asIntBuffer ().put (b);//does not update buffer.position M>> buffer.asDoubleBuffer ().put (c)//similarly B> Read, on the pointer in the new buffer after record B> how many moved And drift a position in old And, read up up to the end - make a method and test a unit tests, here and there will be no errors write (ByteBuffer bb, double [] arr) {db = bb.asDoubleBuffer (); db.put (arr); bb.position (bb.position () +db.position ());} ByteBuffer buffer = ByteBuffer.allocate (BIG_ENOUGH); buffer.putLong (a); write (buffer, b); write (buffer, c)