--- old/src/java.desktop/share/classes/com/sun/media/sound/SunFileReader.java 2015-07-20 23:26:25.000000000 +0300
+++ new/src/java.desktop/share/classes/com/sun/media/sound/SunFileReader.java 2015-07-20 23:26:25.000000000 +0300
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,10 +25,12 @@
package com.sun.media.sound;
+import java.io.BufferedInputStream;
+import java.io.DataInputStream;
import java.io.File;
-import java.io.InputStream;
+import java.io.FileInputStream;
import java.io.IOException;
-import java.io.DataInputStream;
+import java.io.InputStream;
import java.net.URL;
import javax.sound.sampled.AudioFileFormat;
@@ -36,8 +38,6 @@
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.spi.AudioFileReader;
-
-
/**
* Abstract File Reader class.
*
@@ -45,118 +45,109 @@
*/
abstract class SunFileReader extends AudioFileReader {
- // buffer size for temporary input streams
- protected static final int bisBufferSize = 4096;
-
- /**
- * Constructs a new SunFileReader object.
- */
- SunFileReader() {
- }
-
-
- // METHODS TO IMPLEMENT AudioFileReader
-
- /**
- * Obtains the audio file format of the input stream provided. The stream must
- * point to valid audio file data. In general, audio file providers may
- * need to read some data from the stream before determining 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 this, this method may fail
- * with an IOException.
- * @param stream the input stream from which file format information should be
- * extracted
- * @return an AudioFileFormat
object describing the audio file format
- * @throws UnsupportedAudioFileException if the stream does not point to valid audio
- * file data recognized by the system
+ @Override
+ public final AudioFileFormat getAudioFileFormat(final InputStream stream)
+ throws UnsupportedAudioFileException, IOException {
+ stream.mark(200);
+ try {
+ return getAudioFileFormatImpl(stream);
+ } finally {
+ // According to specification the following is not strictly
+ // necessary, if we got correct format. But it was implemented like
+ // that in 1.3.0 - 1.8. So I leave it as it was, but it seems
+ // specification should be updated.
+ stream.reset();
+ }
+ }
+
+ @Override
+ public final AudioFileFormat getAudioFileFormat(final URL url)
+ throws UnsupportedAudioFileException, IOException {
+ try (InputStream is = url.openStream()) {
+ return getAudioFileFormatImpl(new BufferedInputStream(is));
+ }
+ }
+
+ @Override
+ public final AudioFileFormat getAudioFileFormat(final File file)
+ throws UnsupportedAudioFileException, IOException {
+ try (InputStream is = new FileInputStream(file)) {
+ return getAudioFileFormatImpl(new BufferedInputStream(is));
+ }
+ }
+
+ @Override
+ public AudioInputStream getAudioInputStream(final InputStream stream)
+ throws UnsupportedAudioFileException, IOException {
+ stream.mark(200);
+ try {
+ final AudioFileFormat fileFormat = getAudioFileFormatImpl(stream);
+ // we've got everything, the stream is supported and it is at the
+ // beginning of the audio data, so return an AudioInputStream
+ return new AudioInputStream(stream, fileFormat.getFormat(),
+ fileFormat.getFrameLength());
+ } catch (final UnsupportedAudioFileException e) {
+ stream.reset();
+ throw e;
+ }
+ }
+
+ @Override
+ public final AudioInputStream getAudioInputStream(final URL url)
+ throws UnsupportedAudioFileException, IOException {
+ final InputStream urlStream = url.openStream();
+ try {
+ return getAudioInputStream(new BufferedInputStream(urlStream));
+ } catch (final Throwable e) {
+ closeSilently(urlStream);
+ throw e;
+ }
+ }
+
+ @Override
+ public final AudioInputStream getAudioInputStream(final File file)
+ throws UnsupportedAudioFileException, IOException {
+ final InputStream fileStream = new FileInputStream(file);
+ try {
+ return getAudioInputStream(new BufferedInputStream(fileStream));
+ } catch (final Throwable e) {
+ closeSilently(fileStream);
+ throw e;
+ }
+ }
+
+ /**
+ * Obtains the audio file format of the input stream provided. The stream
+ * must point to valid audio file data. Note that default implementation of
+ * {@link #getAudioInputStream(InputStream)} assume that this method leaves
+ * the input stream at the beginning of the audio data.
+ *
+ * @param stream the input stream from which file format information should
+ * be extracted
+ * @return an {@code AudioFileFormat} object describing the audio file
+ * format
+ * @throws UnsupportedAudioFileException if the stream does not point to
+ * valid audio file data recognized by the system
* @throws IOException if an I/O exception occurs
- * @see InputStream#markSupported
- * @see InputStream#mark
*/
- abstract public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException;
-
-
- /**
- * Obtains the audio file format of the URL provided. The URL must
- * point to valid audio file data.
- * @param url the URL from which file format information should be
- * extracted
- * @return an AudioFileFormat
object describing the audio file format
- * @throws UnsupportedAudioFileException if the URL does not point to valid audio
- * file data recognized by the system
- * @throws IOException if an I/O exception occurs
- */
- abstract public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException;
-
-
- /**
- * Obtains the audio file format of the File provided. The File must
- * point to valid audio file data.
- * @param file the File from which file format information should be
- * extracted
- * @return an AudioFileFormat
object describing the audio file format
- * @throws UnsupportedAudioFileException if the File does not point to valid audio
- * file data recognized by the system
- * @throws IOException if an I/O exception occurs
- */
- abstract public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException;
-
-
- /**
- * Obtains an audio stream from the input stream provided. The stream must
- * point to valid audio file data. In general, audio file providers may
- * need to read some data from the stream before determining 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 this, this method may fail
- * with an IOException.
- * @param stream the input stream from which the AudioInputStream
should be
- * constructed
- * @return an AudioInputStream
object based on the audio file data contained
- * in the input stream.
- * @throws UnsupportedAudioFileException if the stream does not point to valid audio
- * file data recognized by the system
- * @throws IOException if an I/O exception occurs
- * @see InputStream#markSupported
- * @see InputStream#mark
- */
- abstract public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException;
-
-
- /**
- * Obtains an audio stream from the URL provided. The URL must
- * point to valid audio file data.
- * @param url the URL for which the AudioInputStream
should be
- * constructed
- * @return an AudioInputStream
object based on the audio file data pointed
- * to by the URL
- * @throws UnsupportedAudioFileException if the URL does not point to valid audio
- * file data recognized by the system
- * @throws IOException if an I/O exception occurs
- */
- abstract public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException;
-
-
- /**
- * Obtains an audio stream from the File provided. The File must
- * point to valid audio file data.
- * @param file the File for which the AudioInputStream
should be
- * constructed
- * @return an AudioInputStream
object based on the audio file data pointed
- * to by the File
- * @throws UnsupportedAudioFileException if the File does not point to valid audio
- * file data recognized by the system
- * @throws IOException if an I/O exception occurs
- */
- abstract public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException;
-
+ abstract AudioFileFormat getAudioFileFormatImpl(InputStream stream)
+ throws UnsupportedAudioFileException, IOException;
// HELPER METHODS
-
+ /**
+ * Closes the InputStream when we have read all necessary data from it, and
+ * ignores an IOException.
+ *
+ * @param is the InputStream which should be closed
+ */
+ private static void closeSilently(final InputStream is) {
+ try {
+ is.close();
+ } catch (final IOException ignored) {
+ // IOException is ignored
+ }
+ }
/**
* rllong