0

When it comes to Java NIO MappedByteBuffer, is it possible to map part of different files into the buffer? Something like this:

buffer = infilechannel1.map(FileChannel.MapMode.READ_WRITE, infilechannel1.position(), infilechannel1.size());

Then something like this:

buffer = infilechannel2.map(FileChanne2.MapMode.READ_WRITE, infilechannel2.position(), infilechannel2.size());

Currently, what happens is that in the first "put", the limit of the buffer is already set to a certain value. The second "put" into the buffer causes a buffer overflow exception. I have allocated enough space to the buffer.

How would I update (i.e. append) this buffer with the values from the second file?

EDIT: My initial question was incorrect. Editing it so that it makes more sense.

I have some big files (say 4, and for now equal in size) and the files, even individually will not fit in memory. I have to perform operations on the 4 files and create a single output file. Since my memory is quite small compared to the size of the file, I can process only 4 blocks at a time, i.e., one block from each of the file at a time. I can either create multiple MemoryByteBuffers which maps to that particular block in files and then write into a ByteBuffer for processing OR I can directly write each block to the ByteBuffer.

Is there any advantage/ meaning in using MemoryByteBuffer in this scenario?

el_sid
  • 1
  • 1
  • Not sure what you mean. Your first statement creates an object and assigns it to the variable `buffer`, and your second statement creates a different object and assigns it to the variable `buffer`, erasing the previous reference (not its content). How do you expect there to be any connection between them? – RealSkeptic Sep 15 '15 at 18:17
  • Indeed. I would like to append to the buffer a map of file2. However, there is no "append" method available. How is it possible to have buffer to have contents of both file1 and file2? – el_sid Sep 15 '15 at 20:52
  • A `MappedByteBuffer` represents a "window" into the file that is mapped to memory. You can then write data to it like any byte buffer, or read data from it. But I'm not sure what you expect a buffer that maps two files to do. It's like a single window belonging to two houses... – RealSkeptic Sep 15 '15 at 20:59
  • Ah I see. Thanks! If we are indeed writing data in to a byte buffer, is there any meaning/ advantage in using a MappedByteBuffer as an intermediary? Can we not write from the file channels to the byte buffer instead? – el_sid Sep 15 '15 at 21:11
  • I think you should specify in your question what it is you want to *do* with the data, and then we can say whether you need a `MappedByteBuffer` or not. – RealSkeptic Sep 15 '15 at 21:18
  • The short answer to your question is 'no'. – user207421 Sep 15 '15 at 22:18
  • I take your word for it EJP :) Thanks for your comments folks! – el_sid Sep 17 '15 at 18:07

0 Answers0