0

I am trying to read one big file in chunk . So the read operation will be called multiple times being offset one of the parameter . The read is working perfectly fine .

But the real problem is starts when I try to delete the file after read is complete . It is throwing IO exception .

I do not want to forcefully garbage collect(System.gc()) .

Read Code :

public static GenericExcelRead ReadFileContent(String fileName, int offset, String status) throws IOException
{
    GenericExcelRead aGenericExcelRead = new GenericExcelRead();
    //FileInputStream fileStream = null;
    FileChannel fileChannel = null;
    MappedByteBuffer buffer;
    try(FileInputStream fileStream = new FileInputStream(fileName)) {

        fileChannel = fileStream.getChannel();
        buffer = null;
        if (status != "Completed")
        {
            if(fileChannel.size()>=(offset+1048756))
            {
                buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, offset, 1048756);
                aGenericExcelRead.setStatus("Partial");
                aGenericExcelRead.setEndOffset(offset+1048756);
            }
            else
            {
                buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, offset, (fileChannel.size()-offset));
                aGenericExcelRead.setStatus("Completed");
                aGenericExcelRead.setEndOffset((int)fileChannel.size());
            }       
            byte[] b = new byte[buffer.remaining()];
            buffer.get(b);
            String encodedcontent = new String(Base64.encodeBase64(b));
            buffer.clear();
            fileChannel.close();
            aGenericExcelRead.setData(encodedcontent);
            fileStream.close();
        }


    } catch (IOException e) {

        throw new IOException("IO Exception/File not found");
    }finally {

        if(fileChannel != null)
            fileChannel.close();
    }
    return aGenericExcelRead;
}
  • Why do you catch `IOException`, then throw a new `IOException` that discards all the important information of the original exception? It's not just unnecessary, it's harmful. – Kayaman Dec 22 '17 at 12:24
  • Yes you are right. It is just a dummy code and not applicable for prod env . – atanu2destiny Dec 22 '17 at 12:33
  • 2
    It shouldn't be applicable for any environment. You're getting an exception and you don't see the stacktrace. Include the stacktrace in your question. – Kayaman Dec 22 '17 at 12:36
  • Possible duplicate of [How to unmap a file from memory mapped using FileChannel in java?](https://stackoverflow.com/questions/2972986/how-to-unmap-a-file-from-memory-mapped-using-filechannel-in-java) – Andrew Henle Dec 22 '17 at 14:31
  • There is no way to force an unmap of a file in Java. From 2002: [JDK-4724038 : (fs) Add unmap method to MappedByteBuffer](https://bugs.java.com/view_bug.do?bug_id=4724038): "There is no unmap() method on mapped byte buffers because there is no known technique for providing one without running into insurmountable security and performance issues." – Andrew Henle Dec 22 '17 at 14:33
  • `fileStream.close()` is redundant since you're using `fileStream` with a [`try-with-resources`](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) statement. – Leponzo Mar 21 '21 at 02:33

0 Answers0