Skip to content

Commit

Permalink
Add again Objects.requireNonNull(this)
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Mar 27, 2024
1 parent 3336cbb commit 24d5702
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 53 deletions.
23 changes: 19 additions & 4 deletions api/src/main/java/jakarta/mail/util/SharedFileInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Objects;

/**
* A <code>SharedFileInputStream</code> is a
Expand Down Expand Up @@ -426,10 +427,24 @@ public boolean markSupported() {
public void close() throws IOException {
if (in == null)
return;
sf.close();
sf = null;
in = null;
buf = null;
try {
sf.close();
} finally {
sf = null;
in = null;
buf = null;
/*
* This avoids that 'new SharedFileInputStream(file)'
* is GCed meanwhile #newStream is invoked, for example:
* new SharedFileInputStream(file).newStream(0, -1)
*
* That could be an issue if a subclass of this one invokes #close
* from #finalize (not a good practice anyway).
*/
Objects.requireNonNull(this);
// TODO Replace it by the next
// Reference.reachabilityFence(this);
}
}

/**
Expand Down
53 changes: 4 additions & 49 deletions api/src/test/java/jakarta/mail/util/SharedFileInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@

package jakarta.mail.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;

import jakarta.mail.util.SharedFileInputStream.SharedFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.reflect.Field;
import java.util.concurrent.TimeoutException;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
* Please note:
Expand Down Expand Up @@ -101,25 +98,6 @@ public void testOpenIfOneOpened() throws Exception {
assertEquals(true, isClosed(ra1));
}

@Test
public void testGC() throws Exception {
File file = new File(SharedFileInputStreamTest.class.getResource("/jakarta/mail/util/sharedinputstream.txt").toURI());
SharedFileInputStream in = new SharedFileInputStream(file);
GCUtil gcUtil = new GCUtil(in);
SharedFileInputStream in0 = (SharedFileInputStream) in.newStream(0, 6);
in.close();
in = null;
gcUtil.waitTillGCed(1000);
gcUtil = new GCUtil(in0);
SharedFileInputStream in1 = (SharedFileInputStream) in0.newStream(6, 12);
assertEquals("test0\n", new String(in0.readAllBytes()));
in0.close();
in0 = null;
gcUtil.waitTillGCed(1000);
assertEquals("test1\n", new String(in1.readAllBytes()));
in1.close();
}

private RandomAccessFile getRandomAccessFile(SharedFileInputStream in) throws Exception {
Field f1 = SharedFileInputStream.class.getDeclaredField("sf");
f1.setAccessible(true);
Expand All @@ -137,27 +115,4 @@ private boolean isClosed(RandomAccessFile rf) throws Exception {
}
}

private static class GCUtil {

private final ReferenceQueue<Object> rq = new ReferenceQueue<>();
private final PhantomReference<Object> phantomRef;

private GCUtil(Object ref) {
phantomRef = new PhantomReference<>(ref, rq);
}

private void waitTillGCed(long timeout) throws Exception {
Reference<? extends Object> gced;
long time = 0;
long sleep = 100;
while ((gced = rq.poll()) != phantomRef) {
Thread.sleep(sleep);
time = time + sleep;
if (time >= timeout) {
throw new TimeoutException("Instance not GCed after " + timeout + " millis");
}
System.gc();
}
}
}
}

0 comments on commit 24d5702

Please sign in to comment.