Skip to content

Commit dfd3536

Browse files
authored
Merge pull request #72 from eldonhipolito/master
Fix for running out of file descriptor due to unclosed file
2 parents cae63c7 + a0e4536 commit dfd3536

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/main/java/org/audit4j/core/handler/file/ZeroCopyFileWriter.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public final class ZeroCopyFileWriter extends AuditFileWriter implements Seriali
5353
/** The path. */
5454
private final String path;
5555

56+
/** To track last opened random access file **/
57+
String lastRealPath;
58+
5659
/**
5760
* Instantiates a new zero copy file writer.
5861
*
@@ -89,11 +92,15 @@ public ZeroCopyFileWriter write(String event) {
8992
FileHandlerUtil.generateAuditFileName());
9093

9194
try {
92-
if (FileHandlerUtil.isFileAlreadyExists(realPath)) {
93-
randomAccessFile = new RandomAccessFile(realPath, CoreConstants.READ_WRITE);
94-
} else {
95-
randomAccessFile = new RandomAccessFile(new File(realPath), CoreConstants.READ_WRITE);
96-
}
95+
/** Close last instance for random access file. **/
96+
if (randomAccessFile != null && !realPath.equals(lastRealPath)) {
97+
this.stop();
98+
}
99+
100+
if (randomAccessFile == null) {
101+
lastRealPath = realPath;
102+
randomAccessFile = new RandomAccessFile(new File(realPath), CoreConstants.READ_WRITE);
103+
}
97104
} catch (FileNotFoundException e) {
98105
// TODO Auto-generated catch block
99106
e.printStackTrace();

0 commit comments

Comments
 (0)