Skip to content

Commit

Permalink
GH-323 looking for a file descriptor leak.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Corless committed Dec 6, 2023
1 parent 12d2762 commit 9851759
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
21 changes: 13 additions & 8 deletions core/core-awt/src/main/java/org/icepdf/core/pobjects/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ public static String getLibraryVersion() {
private static boolean isCachingEnabled;

private final Library library;
// todo put file channel input library?

private FileChannel documentFileChannel;
private RandomAccessFile randomAccessFile;
private ByteBuffer documentByteBuffer;
private CrossReferenceRoot crossReferenceRoot;

Expand Down Expand Up @@ -191,7 +192,8 @@ public void setFile(String filepath)
setDocumentOrigin(filepath);

File file = new File(filepath);
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
try {
randomAccessFile = new RandomAccessFile(file, "r");
documentFileChannel = randomAccessFile.getChannel();
ByteBuffer mappedFileByteBuffer = documentFileChannel.map(
FileChannel.MapMode.READ_ONLY, 0, documentFileChannel.size());
Expand Down Expand Up @@ -266,7 +268,8 @@ public void setInputStream(InputStream inputStream, String pathOrURL)

setDocumentCachedFilePath(tempFile.getAbsolutePath());

try (RandomAccessFile randomAccessFile = new RandomAccessFile(tempFile, "r")) {
try {
randomAccessFile = new RandomAccessFile(tempFile, "r");
documentFileChannel = randomAccessFile.getChannel();
ByteBuffer mappedFileByteBuffer = documentFileChannel.map(
FileChannel.MapMode.READ_ONLY, 0, documentFileChannel.size());
Expand Down Expand Up @@ -321,7 +324,8 @@ public void setByteArray(byte[] data, int offset, int length, String pathOrURL)

setDocumentCachedFilePath(tempFile.getAbsolutePath());

try (RandomAccessFile randomAccessFile = new RandomAccessFile(tempFile, "r")) {
try {
randomAccessFile = new RandomAccessFile(tempFile, "r");
documentFileChannel = randomAccessFile.getChannel();
ByteBuffer mappedFileByteBuffer = documentFileChannel.map(
FileChannel.MapMode.READ_ONLY, 0, documentFileChannel.size());
Expand Down Expand Up @@ -530,13 +534,14 @@ public void paintPage(int pageNumber, Graphics g, final int renderHintType,
* Dispose of Document, freeing up all used resources.
*/
public void dispose() {

if (documentFileChannel != null) {
// clean up file it will clean up any file channels and file descriptors too
if (randomAccessFile != null) {
try {
documentFileChannel.close();
randomAccessFile.close();
} catch (IOException e) {
logger.log(Level.FINE, "Error closing document input stream.", e);
logger.log(Level.FINE, "Error closing document random access file.", e);
}
randomAccessFile = null;
documentFileChannel = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public long createDocument(
document,
out,
documentLength);

channel.close();
return documentLength + appendedLength;
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public long writeDocument(
writer.writeFullTrailer();
}
}
output.close();

return writer.getBytesWritten();
}
Expand Down

0 comments on commit 9851759

Please sign in to comment.