2323import java .io .IOException ;
2424import java .io .InputStream ;
2525import java .io .RandomAccessFile ;
26+ import java .util .Objects ;
2627
2728/**
2829 * A <code>SharedFileInputStream</code> is a
3435 * A <code>RandomAccessFile</code> object is used to
3536 * access the file data. <p>
3637 *
37- * Note that when the SharedFileInputStream is closed,
38- * all streams created with the <code>newStream</code>
39- * method are also closed. This allows the creator of the
40- * SharedFileInputStream object to control access to the
41- * underlying file and ensure that it is closed when
42- * needed, to avoid leaking file descriptors. Note also
43- * that this behavior contradicts the requirements of
44- * SharedInputStream and may change in a future release.
45- *
4638 * @author Bill Shannon
4739 * @since JavaMail 1.4
4840 */
@@ -78,12 +70,6 @@ public class SharedFileInputStream extends BufferedInputStream
7870 */
7971 protected long datalen ;
8072
81- /**
82- * True if this is a top level stream created directly by "new".
83- * False if this is a derived stream created by newStream.
84- */
85- private boolean master = true ;
86-
8773 /**
8874 * A shared class that keeps track of the references
8975 * to a particular file so it can be closed when the
@@ -111,22 +97,8 @@ public synchronized void close() throws IOException {
11197 in .close ();
11298 }
11399
114- public synchronized void forceClose () throws IOException {
115- if (cnt > 0 ) {
116- // normal case, close exceptions propagated
117- cnt = 0 ;
118- in .close ();
119- } else {
120- // should already be closed, ignore exception
121- try {
122- in .close ();
123- } catch (IOException ioex ) {
124- }
125- }
126- }
127-
128100 @ Override
129- protected void finalize () throws Throwable {
101+ protected synchronized void finalize () throws Throwable {
130102 try {
131103 in .close ();
132104 } finally {
@@ -214,7 +186,6 @@ private void init(SharedFile sf, int size) throws IOException {
214186 private SharedFileInputStream (SharedFile sf , long start , long len ,
215187 int bufsize ) {
216188 super (null );
217- this .master = false ;
218189 this .sf = sf ;
219190 this .in = sf .open ();
220191 this .start = start ;
@@ -465,14 +436,12 @@ public void close() throws IOException {
465436 if (in == null )
466437 return ;
467438 try {
468- if (master )
469- sf .forceClose ();
470- else
471- sf .close ();
439+ sf .close ();
472440 } finally {
473441 sf = null ;
474442 in = null ;
475443 buf = null ;
444+ Objects .requireNonNull (this ); //TODO: replace with Reference.reachabilityFence
476445 }
477446 }
478447
@@ -505,14 +474,19 @@ public long getPosition() {
505474 */
506475 @ Override
507476 public synchronized InputStream newStream (long start , long end ) {
508- if (in == null )
509- throw new RuntimeException ("Stream closed" );
510- if (start < 0 )
511- throw new IllegalArgumentException ("start < 0" );
512- if (end == -1 )
513- end = datalen ;
514- return new SharedFileInputStream (sf ,
515- this .start + start , end - start , bufsize );
477+ try {
478+ if (in == null )
479+ throw new RuntimeException ("Stream closed" );
480+ if (start < 0 )
481+ throw new IllegalArgumentException ("start < 0" );
482+ if (end == -1 )
483+ end = datalen ;
484+
485+ return new SharedFileInputStream (sf ,
486+ this .start + start , end - start , bufsize );
487+ } finally {
488+ Objects .requireNonNull (this ); //TODO: replace with Reference.reachabilityFence
489+ }
516490 }
517491
518492 // for testing...
@@ -537,7 +511,7 @@ public static void main(String[] argv) throws Exception {
537511 * Force this stream to close.
538512 */
539513 @ Override
540- protected void finalize () throws Throwable {
514+ protected synchronized void finalize () throws Throwable {
541515 super .finalize ();
542516 close ();
543517 }
0 commit comments