0

I need to convert a large file (2GB) to netty byteBuf. I am using the following code its working

File file = (File) fw;
                FileInputStream fileInputStream = new FileInputStream(file);
                FileChannel fileChannel = fileInputStream.getChannel();
                MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
                setDataToTransfer(Unpooled.wrappedBuffer(mappedByteBuffer)); //Data to transfer is a byteBuf

But when I re run the operation it gives me the following error

(The requested operation cannot be performed on a file with a user-mapped section open)
at java.base/java.io.FileOutputStream.open0(Native Method) ~[na:na]
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298) ~[na:na]
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237) ~[na:na]
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:187) ~[na:na]

I tried closing (unmapping) the Mappedbytebuffer using sun.misc.Cleaner but that didnt seem to work.

I also tried the following code

ByteBuf buffer =    buffer()
buffer.writeBytes(new FileInputStream(file), file.length());

This works when I run the code on a 16GB ram but fails on 4GB ram system gives me heap memory issues.

Can you please suggest a code to convert a large file into netty bytebuf which can run on 4GB ram using JDK 8.

dev_101
  • 321
  • 4
  • 14

0 Answers0