diff --git a/api/src/main/java/jakarta/mail/util/SharedFileInputStream.java b/api/src/main/java/jakarta/mail/util/SharedFileInputStream.java index 8d62026b..4df19084 100644 --- a/api/src/main/java/jakarta/mail/util/SharedFileInputStream.java +++ b/api/src/main/java/jakarta/mail/util/SharedFileInputStream.java @@ -476,15 +476,19 @@ public long getPosition() { */ @Override public synchronized InputStream newStream(long start, long end) { - if (in == null) - throw new RuntimeException("Stream closed"); - if (start < 0) - throw new IllegalArgumentException("start < 0"); - if (end == -1) - end = datalen; - - return new SharedFileInputStream(sf, - this.start + start, end - start, bufsize); + try { + if (in == null) + throw new RuntimeException("Stream closed"); + if (start < 0) + throw new IllegalArgumentException("start < 0"); + if (end == -1) + end = datalen; + + return new SharedFileInputStream(sf, + this.start + start, end - start, bufsize); + } finally { + Objects.requireNonNull(this); //TODO: replace with Reference.reachabilityFence + } } // for testing...