Java.Io.Ioexception: Mark/Reset Not Supported

java.io.IOException: mark/reset not supported

The documentation for AudioSystem.getAudioInputStream(InputStream) says:

The implementation of this method may
require multiple parsers to examine
the stream to determine whether they
support it. These parsers must be able
to mark the stream, read enough data
to determine whether they support the
stream, and, if not, reset the
stream's read pointer to its original
position. If the input stream does not
support these operation, this method
may fail with an IOException.

Therefore, the stream you provide to this method must support the optional mark/reset functionality. Decorate your resource stream with a BufferedInputStream.

//read audio data from whatever source (file/classloader/etc.)
InputStream audioSrc = getClass().getResourceAsStream("mySound.au");
//add buffer for mark/reset support
InputStream bufferedIn = new BufferedInputStream(audioSrc);
AudioInputStream audioStream = AudioSystem.getAudioInputStream(bufferedIn);

java.io.IOException: mark/reset not supported Java Audio Input Stream / Buffered Input Stream

Change:

        /************/
InputStream is = getClass().getResourceAsStream(s);
AudioInputStream ais;
BufferedInputStream bis = new BufferedInputStream(is);
ais = AudioSystem.getAudioInputStream(bis);
/************/

To something like:

        /************/
URL url = getClass().getResource(s);
AudioInputStream ais;
ais = AudioSystem.getAudioInputStream(url);
/************/

It will work because getResourceAsStream typically returns a non-repositionable input stream, whereas if you provide the URL to the AudioSystem, it can establish as many streams as it wants from the URL, or wrap it in a repositionable stream.

java.io.IOException: mark/reset not supported (for static)

I had to deal with a very similar problem, and posted it here:

mark/reset exception during getAudioInputStream()

This form: .getResourceAsStream(fileName) returns an InputStream which throws a mark/reset exception if the file is not markable. The explanation I got is that there used to be a default "first guess" of .wav, but this is no longer the first guess (as of Java 7). There is a better, fuller description at Oracle's bug database for #7095006.

Use this form and you should be okay, because it doesn't require the intermediate step (InputStream) that needs to support marking & resetting:

URL url = AudioMixer.class.getResource(fileName); 
AudioInputStream ais = AudioSystem.getAudioInputStream(url);

How to correctly implement InputStream.mark() when mark is not supported

It looks like typical implementations within the JDK do nothing, and throw an IOException if reset is invoked:

java.util.zip.InflaterInputStream:

public synchronized void mark(int readlimit) {
}

public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}

java.io.PushbackInputStream:

public synchronized void mark(int readlimit) {
}

public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}

mark() and reset() method in Java

The mark and reset methods are optional operations that not every InputStream needs to support. You can call markSupported to find out if it does.

PushbackInputStream does not support these methods.

The methods are still there, because they are defined in the InputStream interface. Maybe a bad design decision (could have been added to a separate interface), but that is how it is.



Related Topics



Leave a reply



Submit