Forum Stats

  • 3,824,981 Users
  • 2,260,447 Discussions
  • 7,896,369 Comments

Discussions

IOException reading WAV from client via applet

Phil Freihofner
Phil Freihofner Member Posts: 28
edited Jun 30, 2011 5:27PM in Java Sound
I just managed to enable an Applet to allow reading of files from a client computer, using the JNLP API. The following code returned a diagnostic that indicates the file selected is readable.
    InputStream in = null;
...
    vsg.updateDiagnostic("about to try fos instantiation");
    FileOpenService fos = (FileOpenService) 
    ServiceManager.lookup("javax.jnlp.FileOpenService");
    FileContents contents = fos.openFileDialog(".", new String[] {"wav"});
    if (contents != null) 
    {
        in = contents.getInputStream();
        if (contents.canRead())
        {
            vsg.updateDiagnostic("can read");
        }
    }
However, I am throwing an IOException when I try to instantiate an AudioFileFormat object from this InputStream as follows:
    AudioFileFormat aff = AudioSystem.getAudioFileFormat(in);
The very same WAV file loads fine when I run this program with the following code in an "application" context, where fs is a JFileChooser.
    File soundFile = fs.getSelectedFile().getAbsoluteFile();
    AudioiFileFormat aff = AudioSystem.getAudioFileFormat(soundFile);
An InputStream is considered a valid argument for the getAudioFileFormat() method, yes? And I verified that the newly created InputStream was readable.

Any thoughts on what I might be overlooking? I'm so new to JNLP that I don't know if there is some aspect of the Applet context that is causing a problem. Should I post this question in the JNLP & WebStart forum?

I am posting the Applet at a remote site via FTP, going to the site with a Mozilla Firefox browser, and trying to read a file from my computer's "My Documents" area.

Thank you for your time and help!

Edited by: 807197 on Jun 30, 2011 12:18 AM
Tagged:

Best Answer

  • 793415
    793415 Member Posts: 7,279 Bronze Badge
    Answer ✓
    You might try loading the entire input stream provided by the <tt>FileContents</tt> into a <tt>ByteArrayOutputStream</tt>. Provide the <tt>byte[]</tt> from that to the constructor of a <tt>ByteArrayInputStream</tt> & use the BAIS for further operations.

Answers

  • EJP
    EJP Member Posts: 32,920 Gold Crown
    I am throwing an IOException
    With what message text?
    EJP
  • "IOException: mark/reset not supported"

    Sorry for not including this initially. I don't know how to retrieve error messages from Applets, and had to build a text field on the GUI itself to intercept the full message.

    I recall running into this situation before, but not a solution. Is there a way to wrap the stream in something that CAN do a mark & reset? I will try this while awaiting further replies.

    Thanks!
  • I just tried wrapping the InputStream from the FileOpenService in a BufferedInputStream and wrapping that in a PushbackInputStream. But I get a .markSupported() = false on the BushbackInputStream, so trying to get an AudioFileFormat still does not work.

    I kind of need the AudioFileFormat in order to get the frame length of the audio file (used to build an internal array), as well as use it for prescreening supported audio file formats.

    Is there be a way to make this file "markable"? Or should one take another approach?

    Thanks again!
  • 793415
    793415 Member Posts: 7,279 Bronze Badge
    Answer ✓
    You might try loading the entire input stream provided by the <tt>FileContents</tt> into a <tt>ByteArrayOutputStream</tt>. Provide the <tt>byte[]</tt> from that to the constructor of a <tt>ByteArrayInputStream</tt> & use the BAIS for further operations.
  • Beautiful!

    That was exactly what I was trying just now, and it worked. Since you already described the answer, I don't have to.

    Many, many thanks!
This discussion has been closed.