<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Yikes, Good point Klaus! Forgot the caller wants to actually use a valid stream for the non-exceptional case. Would have to move the is.close() back into a catch clause. I’ll try to post a better one later. (Any unit tests of this sort of thing exist in the tree now? - if not, I could try a unit test too).<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 9, 2015, at 12:34 PM, Klaus Jaensch <<a href="mailto:klausj@phonetik.uni-muenchen.de" class="">klausj@phonetik.uni-muenchen.de</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
  
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" class="">
  
  <div bgcolor="#FFFFFF" text="#000000" class="">
    <div class="moz-cite-prefix">Hi Dan,<br class="">
      <br class="">
      Am 09.01.2015 um 05:37 schrieb Dan Rollo:<br class="">
    </div>
    <blockquote cite="mid:DF9F7353-D802-4007-8A2E-4A7FEEF1E8E1@gmail.com" type="cite" class="">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" class="">
      Even better, no need for the “catch/throw” chunk, because the
      method declares those caught exceptions:
      <div class=""><br class="">
      </div>
      <div class=""><br class="">
      </div>
      <div class="">public AudioInputStream getAudioInputStream(File
        file)<br class="">
                throws UnsupportedAudioFileException, IOException {<br class="">
        <br class="">
            final FileInputStream fis = new FileInputStream(file);<br class="">
            try {<br class="">
             
          return getAudioInputStream(new BufferedInputStream(fis));<br class="">
            } finally {<br class="">
                fis.close();<br class="">
            }<br class="">
        }<br class="">
      </div>
    </blockquote>
    <br class="">
    I think your code will not work. If the call to 
    getAudioInputStream(InputStream) succeeds the code always closes the
    stream before it is returned. <br class="">
    <br class="">
    <br class="">
    I suggest this approach:  <br class="">
    <br class="">
    public AudioInputStream getAudioInputStream(File file)<br class="">
                throws UnsupportedAudioFileException, IOException {<br class="">
            FileInputStream fis=new FileInputStream(file);<br class="">
            BufferedInputStream bis=new BufferedInputStream(fis);<br class="">
            AudioInputStream ais=null;<br class="">
            try{<br class="">
                ais=getAudioInputStream(bis);<br class="">
            } catch(IOException|UnsupportedAudioFileException e) { <br class="">
                if(bis!=null){<br class="">
                    bis.close();<br class="">
                }<br class="">
                throw e;<br class="">
            }<br class="">
            return ais;<br class="">
        }<br class="">
    <br class="">
    Closes the BufferedInputStream as well as the underlying
    FileInputStream.<br class="">
    <br class="">
    I think the method:<br class="">
    public AudioInputStream getAudioInputStream(URL url)<br class="">
    <br class="">
    needs to be changed in the same way.<br class="">
    <br class="">
    <br class="">
    Best regards,<br class="">
    Klaus<br class="">
    <br class="">
    <br class="">
    <blockquote cite="mid:DF9F7353-D802-4007-8A2E-4A7FEEF1E8E1@gmail.com" type="cite" class="">
      <div class="">
        <div class=""><br class="">
        </div>
        <div class=""><br class="">
          <blockquote type="cite" class="">
            <div class="">On Jan 7, 2015, at 11:41 PM, Dan Rollo <<a moz-do-not-send="true" href="mailto:danrollo@gmail.com" class="">danrollo@gmail.com</a>> wrote:</div>
            <br class="Apple-interchange-newline">
            <div class="">
              <meta http-equiv="Content-Type" content="text/html;
                charset=utf-8" class="">
              <div style="word-wrap: break-word; -webkit-nbsp-mode:
                space; -webkit-line-break: after-white-space;" class="">If
                this approach is taken, I’d like to suggest using a
                ‘final’ var instead of ‘null init/null check’, for
                example:
                <div class=""><br class="">
                </div>
                <div class="">public AudioInputStream getAudioInputStream(File
                  file)<br class="">
                       
                    throws UnsupportedAudioFileException, IOException {<br class="">
                  <br class="">
                      final FileInputStream fis
                  = new FileInputStream(file);<br class="">
                      try {<br class="">
                       
                    return getAudioInputStream(new BufferedInputStream(fis));<br class="">
                      } catch(IOException|UnsupportedAudioFileException
                  e) {<br class="">
                          throw e;<br class="">
                      } finally {<br class="">
                          fis.close();<br class="">
                      }<br class="">
                  }<br class="">
                  <div class=""><br class="">
                  </div>
                  <div class=""><br class="">
                    <blockquote type="cite" class="">
                      <div class="">On Jan 7, 2015, at 10:51 PM, Sergey
                        Bylokhov <<a moz-do-not-send="true" href="mailto:Sergey.Bylokhov@oracle.com" class="">Sergey.Bylokhov@oracle.com</a>>
                        wrote:</div>
                      <br class="Apple-interchange-newline">
                      <div class="">
                        <div class="moz-cite-prefix" style="font-family:
                          Helvetica; font-size: 14px; font-style:
                          normal; font-variant: normal; font-weight:
                          normal; letter-spacing: normal; line-height:
                          normal; orphans: auto; text-align: start;
                          text-indent: 0px; text-transform: none;
                          white-space: normal; widows: auto;
                          word-spacing: 0px; -webkit-text-stroke-width:
                          0px; background-color: rgb(255, 255, 255);">On
                          08.01.2015 1:13, Phil Race wrote:<br class="">
                        </div>
                        <blockquote cite="mid:54ADAF7D.10902@oracle.com" type="cite" style="font-family: Helvetica;
                          font-size: 14px; font-style: normal;
                          font-variant: normal; font-weight: normal;
                          letter-spacing: normal; line-height: normal;
                          orphans: auto; text-align: start; text-indent:
                          0px; text-transform: none; white-space:
                          normal; widows: auto; word-spacing: 0px;
                          -webkit-text-stroke-width: 0px;
                          background-color: rgb(255, 255, 255);" class="">Its not clear to me if the bug
                          description is implying an exception was
                          thrown<br class="">
                        </blockquote>
                        <span style="font-family: Helvetica; font-size:
                          14px; font-style: normal; font-variant:
                          normal; font-weight: normal; letter-spacing:
                          normal; line-height: normal; orphans: auto;
                          text-align: start; text-indent: 0px;
                          text-transform: none; white-space: normal;
                          widows: auto; word-spacing: 0px;
                          -webkit-text-stroke-width: 0px;
                          background-color: rgb(255, 255, 255); float:
                          none; display: inline !important;" class="">UnsupportedAudioFileException
                          is thrown if the URL/File does not point to
                          valid audio file data recognized by the
                          specific reader, so AudioSystem will try to
                          move to the next reader and a leak will occur.</span><br style="font-family: Helvetica; font-size:
                          14px; font-style: normal; font-variant:
                          normal; font-weight: normal; letter-spacing:
                          normal; line-height: normal; orphans: auto;
                          text-align: start; text-indent: 0px;
                          text-transform: none; white-space: normal;
                          widows: auto; word-spacing: 0px;
                          -webkit-text-stroke-width: 0px;
                          background-color: rgb(255, 255, 255);" class="">
                        <span style="font-family: Helvetica; font-size:
                          14px; font-style: normal; font-variant:
                          normal; font-weight: normal; letter-spacing:
                          normal; line-height: normal; orphans: auto;
                          text-align: start; text-indent: 0px;
                          text-transform: none; white-space: normal;
                          widows: auto; word-spacing: 0px;
                          -webkit-text-stroke-width: 0px;
                          background-color: rgb(255, 255, 255); float:
                          none; display: inline !important;" class="">Actually
                          most of our readers are affected.</span><br style="font-family: Helvetica; font-size:
                          14px; font-style: normal; font-variant:
                          normal; font-weight: normal; letter-spacing:
                          normal; line-height: normal; orphans: auto;
                          text-align: start; text-indent: 0px;
                          text-transform: none; white-space: normal;
                          widows: auto; word-spacing: 0px;
                          -webkit-text-stroke-width: 0px;
                          background-color: rgb(255, 255, 255);" class="">
                        <blockquote cite="mid:54ADAF7D.10902@oracle.com" type="cite" style="font-family: Helvetica;
                          font-size: 14px; font-style: normal;
                          font-variant: normal; font-weight: normal;
                          letter-spacing: normal; line-height: normal;
                          orphans: auto; text-align: start; text-indent:
                          0px; text-transform: none; white-space:
                          normal; widows: auto; word-spacing: 0px;
                          -webkit-text-stroke-width: 0px;
                          background-color: rgb(255, 255, 255);" class="">Still, something like what you
                          suggest seems to be needed.<span class="Apple-converted-space"> </span><br class="">
                        </blockquote>
                        <span style="font-family: Helvetica; font-size:
                          14px; font-style: normal; font-variant:
                          normal; font-weight: normal; letter-spacing:
                          normal; line-height: normal; orphans: auto;
                          text-align: start; text-indent: 0px;
                          text-transform: none; white-space: normal;
                          widows: auto; word-spacing: 0px;
                          -webkit-text-stroke-width: 0px;
                          background-color: rgb(255, 255, 255); float:
                          none; display: inline !important;" class="">right.</span><br style="font-family: Helvetica; font-size:
                          14px; font-style: normal; font-variant:
                          normal; font-weight: normal; letter-spacing:
                          normal; line-height: normal; orphans: auto;
                          text-align: start; text-indent: 0px;
                          text-transform: none; white-space: normal;
                          widows: auto; word-spacing: 0px;
                          -webkit-text-stroke-width: 0px;
                          background-color: rgb(255, 255, 255);" class="">
                        <blockquote cite="mid:54ADAF7D.10902@oracle.com" type="cite" style="font-family: Helvetica;
                          font-size: 14px; font-style: normal;
                          font-variant: normal; font-weight: normal;
                          letter-spacing: normal; line-height: normal;
                          orphans: auto; text-align: start; text-indent:
                          0px; text-transform: none; white-space:
                          normal; widows: auto; word-spacing: 0px;
                          -webkit-text-stroke-width: 0px;
                          background-color: rgb(255, 255, 255);" class=""><br class="">
                          The owner of this bug is out until next week
                          so I'll let him comment further<span class="Apple-converted-space"> </span><br class="">
                          after his return.<span class="Apple-converted-space"> </span><br class="">
                          <br class="">
                          -phil.<span class="Apple-converted-space"> </span><br class="">
                          <br class="">
                          On 01/07/2015 12:42 PM, Mike Clark wrote:<span class="Apple-converted-space"> </span><br class="">
                          <blockquote type="cite" class="">Hello all,<span class="Apple-converted-space"> </span><br class="">
                            <br class="">
                            I wanted to post this as a comment on<span class="Apple-converted-space"> </span><a moz-do-not-send="true" class="moz-txt-link-freetext" href="https://bugs.openjdk.java.net/browse/JDK-8013586">https://bugs.openjdk.java.net/browse/JDK-8013586</a>,
                            but apparently getting comment access to
                            that system is a bit of a hurdle.  Anyway. 
                            What follows is, I believe, a fix for the
                            aforementioned bug:<span class="Apple-converted-space"> </span><br class="">
                            <br class="">
                            There is a file handle leak in some of the
                            subclasses of
                            javax.sound.sampled.spi.AudioFileReader,
                            such as
                            com.sun.media.sound.WaveFloatFileReader.<span class="Apple-converted-space"> </span><br class="">
                            <br class="">
                            Consider
                            com.sun.media.sound.WaveFloatFileReader's
                            method:<span class="Apple-converted-space"> </span><br class="">
                            <br class="">
                            public AudioInputStream
                            getAudioInputStream(File file)<span class="Apple-converted-space"> </span><br class="">
                                   <span class="Apple-converted-space"> </span>throws
                            UnsupportedAudioFileException, IOException {<span class="Apple-converted-space"> </span><br class="">
                               <span class="Apple-converted-space"> </span>return
                            getAudioInputStream(<span class="Apple-converted-space"> </span><br class="">
                                   <span class="Apple-converted-space"> </span>new
                            BufferedInputStream(new
                            FileInputStream(file)));<span class="Apple-converted-space"> </span><br class="">
                            }<span class="Apple-converted-space"> </span><br class="">
                            <br class="">
                            See how there is no attempt to close the
                            FileInputStream if an exception is thrown? 
                            A file handle will remain open on the file
                            until garbage collection is run. Since
                            garbage collection may never run, the file
                            handle may remain open until the JVM exits.
                            And on Windows the open file handle prevents
                            the file from being deleted, which is
                            problematic.<span class="Apple-converted-space"> </span><br class="">
                            <br class="">
                            Could we fix it by adding a try/catch block?<span class="Apple-converted-space"> </span><br class="">
                            <br class="">
                            public AudioInputStream
                            getAudioInputStream(File file)<span class="Apple-converted-space"> </span><br class="">
                                   <span class="Apple-converted-space"> </span>throws
                            UnsupportedAudioFileException, IOException {<span class="Apple-converted-space"> </span><br class="">
                               <span class="Apple-converted-space"> </span>FileInputStream
                            fis = null;<span class="Apple-converted-space"> </span><br class="">
                               <span class="Apple-converted-space"> </span>try
                            {<span class="Apple-converted-space"> </span><br class="">
                                   <span class="Apple-converted-space"> </span>fis
                            = new FileInputStream(file);<span class="Apple-converted-space"> </span><br class="">
                                   <span class="Apple-converted-space"> </span>return
                            getAudioInputStream(new
                            BufferedInputStream(fis));<span class="Apple-converted-space"> </span><br class="">
                               <span class="Apple-converted-space"> </span>}
                            catch(IOException|UnsupportedAudioFileException
                            e) {<span class="Apple-converted-space"> </span><br class="">
                                    if (fis != null) {<span class="Apple-converted-space"> </span><br class="">
                                        fis.close();<span class="Apple-converted-space"> </span><br class="">
                                   <span class="Apple-converted-space"> </span>}<span class="Apple-converted-space"> </span><br class="">
                                   <span class="Apple-converted-space"> </span>throw
                            e;<span class="Apple-converted-space"> </span><br class="">
                               <span class="Apple-converted-space"> </span>}<span class="Apple-converted-space"> </span><br class="">
                            }<span class="Apple-converted-space"> </span><br class="">
                            <br class="">
                            These AudioFileReader subclass methods are
                            usually called by
                            javax.sound.sampled.AudioSystem.getAudioInputStream(File),
                            which calls getAudioInputStream(File) on all
                            registered subclasses of AudioFileReader. 
                            As such, all subclasses of AudioFileReader
                            in the JRE should be reviewed for this
                            problem.<span class="Apple-converted-space"> </span><br class="">
                            <br class="">
                            best regards,<span class="Apple-converted-space"> </span><br class="">
                            -Mike<span class="Apple-converted-space"> </span><br class="">
                            <br class="">
                          </blockquote>
                          <br class="">
                        </blockquote>
                        <br style="font-family: Helvetica; font-size:
                          14px; font-style: normal; font-variant:
                          normal; font-weight: normal; letter-spacing:
                          normal; line-height: normal; orphans: auto;
                          text-align: start; text-indent: 0px;
                          text-transform: none; white-space: normal;
                          widows: auto; word-spacing: 0px;
                          -webkit-text-stroke-width: 0px;
                          background-color: rgb(255, 255, 255);" class="">
                        <br style="font-family: Helvetica; font-size:
                          14px; font-style: normal; font-variant:
                          normal; font-weight: normal; letter-spacing:
                          normal; line-height: normal; orphans: auto;
                          text-align: start; text-indent: 0px;
                          text-transform: none; white-space: normal;
                          widows: auto; word-spacing: 0px;
                          -webkit-text-stroke-width: 0px;
                          background-color: rgb(255, 255, 255);" class="">
                        <pre class="moz-signature" cols="72" style="font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">-- 
Best regards, Sergey. </pre>
                      </div>
                    </blockquote>
                  </div>
                  <br class="">
                </div>
              </div>
            </div>
          </blockquote>
        </div>
        <br class="">
      </div>
    </blockquote>
    <br class="">
    <br class="">
    <pre class="moz-signature" cols="72">-- 
------------------------------------------
Klaus Jaensch
Muenchen
Germany

Institut fuer Phonetik und Sprachverarbeitung
Schellingstr.3/II
Room 223 VG
80799 München

Phone (Work): +49-(0)89-2180-2806
Fax:          +49-(0)89-2180-5790
EMail: <a class="moz-txt-link-abbreviated" href="mailto:klausj@phonetik.uni-muenchen.de">klausj@phonetik.uni-muenchen.de</a>
</pre>
  </div>

</div></blockquote></div><br class=""></div></body></html>