--- old/src/java.desktop/share/classes/com/sun/media/sound/AiffFileReader.java 2015-07-16 21:57:59.000000000 +0300 +++ new/src/java.desktop/share/classes/com/sun/media/sound/AiffFileReader.java 2015-07-16 21:57:59.000000000 +0300 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2014, 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 @@ -27,19 +27,14 @@ import java.io.DataInputStream; import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.net.URL; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; -import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.UnsupportedAudioFileException; - /** * AIFF file reader and writer. * @@ -49,177 +44,10 @@ */ public final class AiffFileReader extends SunFileReader { - private static final int MAX_READ_LENGTH = 8; - - // 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 - * @throws IOException if an I/O exception occurs - * @see InputStream#markSupported - * @see InputStream#mark - */ - public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException { - // fix for 4489272: AudioSystem.getAudioFileFormat() fails for InputStream, but works for URL - AudioFileFormat aff = getCOMM(stream, true); - // the following is not strictly necessary - but was implemented like that in 1.3.0 - 1.4.1 - // so I leave it as it was. May remove this for 1.5.0 - stream.reset(); - return aff; - } - - - /** - * 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 - */ - public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException { - AudioFileFormat fileFormat = null; - InputStream urlStream = url.openStream(); // throws IOException - try { - fileFormat = getCOMM(urlStream, false); - } finally { - urlStream.close(); - } - return fileFormat; - } - - - /** - * 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 - */ - public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException { - AudioFileFormat fileFormat = null; - FileInputStream fis = new FileInputStream(file); // throws IOException - // part of fix for 4325421 - try { - fileFormat = getCOMM(fis, false); - } finally { - fis.close(); - } - - return fileFormat; - } - - - - - /** - * 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 - */ - public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException { - // getCOMM leaves the input stream at the beginning of the audio data - AudioFileFormat fileFormat = getCOMM(stream, true); // throws UnsupportedAudioFileException, IOException - - // we've got everything, and the stream is at the - // beginning of the audio data, so return an AudioInputStream. - return new AudioInputStream(stream, fileFormat.getFormat(), fileFormat.getFrameLength()); - } - - - /** - * 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 - */ - public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException { - InputStream urlStream = url.openStream(); // throws IOException - AudioFileFormat fileFormat = null; - try { - fileFormat = getCOMM(urlStream, false); - } finally { - if (fileFormat == null) { - urlStream.close(); - } - } - return new AudioInputStream(urlStream, fileFormat.getFormat(), fileFormat.getFrameLength()); - } - - - /** - * 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 - */ - public AudioInputStream getAudioInputStream(File file) - throws UnsupportedAudioFileException, IOException { - - FileInputStream fis = new FileInputStream(file); // throws IOException - AudioFileFormat fileFormat = null; - // part of fix for 4325421 - try { - fileFormat = getCOMM(fis, false); - } finally { - if (fileFormat == null) { - fis.close(); - } - } - return new AudioInputStream(fis, fileFormat.getFormat(), fileFormat.getFrameLength()); - } - - //-------------------------------------------------------------------- - - private AudioFileFormat getCOMM(InputStream is, boolean doReset) - throws UnsupportedAudioFileException, IOException { - - DataInputStream dis = new DataInputStream(is); - - if (doReset) { - dis.mark(MAX_READ_LENGTH); - } + @Override + AudioFileFormat getAudioFileFormatImpl(final InputStream stream) + throws UnsupportedAudioFileException, IOException { + DataInputStream dis = new DataInputStream(stream); // assumes a stream at the beginning of the file which has already // passed the magic number test... @@ -234,9 +62,6 @@ // $$fb: fix for 4369044: javax.sound.sampled.AudioSystem.getAudioInputStream() works wrong with Cp037 if (magic != AiffFileFormat.AIFF_MAGIC) { // not AIFF, throw exception - if (doReset) { - dis.reset(); - } throw new UnsupportedAudioFileException("not an AIFF file"); } --- old/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java 2015-07-16 21:58:00.000000000 +0300 +++ new/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java 2015-07-16 21:58:00.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,21 +25,15 @@ package com.sun.media.sound; -import java.io.BufferedInputStream; import java.io.DataInputStream; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.net.URL; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; -import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.UnsupportedAudioFileException; - /** * AU file reader. * @@ -49,33 +43,12 @@ */ public final class AuFileReader extends 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 - * @throws IOException if an I/O exception occurs - * @see InputStream#markSupported - * @see InputStream#mark - */ - public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException { - - AudioFormat format = null; - AuFileFormat fileFormat = null; - int maxReadLength = 28; + // TODO this method changed to skipp bytes in the header, so we will remove this code from getAudioInputStream(strem) + // TODO test is needed + @Override + public AudioFileFormat getAudioFileFormatImpl(final InputStream stream) + throws UnsupportedAudioFileException, IOException { boolean bigendian = false; - int magic = -1; int headerSize = -1; int dataSize = -1; int encoding_local = -1; @@ -90,15 +63,12 @@ DataInputStream dis = new DataInputStream( stream ); - dis.mark(maxReadLength); - - magic = dis.readInt(); + final int magic = dis.readInt(); nread += 4; if (! (magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC) || (magic == AuFileFormat.AU_SUN_INV_MAGIC) || (magic == AuFileFormat.AU_DEC_INV_MAGIC) ) { - // not AU, reset the stream, place into exception, throw exception - dis.reset(); + // not AU, throw exception throw new UnsupportedAudioFileException("not an AU file"); } @@ -112,7 +82,6 @@ sampleRate = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4; channels = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4; if (channels <= 0) { - dis.reset(); throw new UnsupportedAudioFileException("Invalid number of channels"); } @@ -172,7 +141,6 @@ */ default: // unsupported filetype, throw exception - dis.reset(); throw new UnsupportedAudioFileException("not a valid AU file"); } @@ -184,189 +152,13 @@ //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED length = dataSize / frameSize; } - - format = new AudioFormat( encoding, (float)sampleRate, sampleSizeInBits, - channels, frameSize, (float)frameRate, bigendian); - - fileFormat = new AuFileFormat( AudioFileFormat.Type.AU, dataSize+headerSize, - format, length); - - dis.reset(); // Throws IOException - return fileFormat; - - } - - - /** - * 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 - */ - public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException { - - InputStream urlStream = null; - BufferedInputStream bis = null; - AudioFileFormat fileFormat = null; - AudioFormat format = null; - - urlStream = url.openStream(); // throws IOException - - try { - bis = new BufferedInputStream( urlStream, bisBufferSize ); - - fileFormat = getAudioFileFormat( bis ); // throws UnsupportedAudioFileException - } finally { - urlStream.close(); - } - - return fileFormat; - } - - - /** - * 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 - */ - public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException { - - FileInputStream fis = null; - BufferedInputStream bis = null; - AudioFileFormat fileFormat = null; - AudioFormat format = null; - - fis = new FileInputStream( file ); // throws IOException - // part of fix for 4325421 - try { - bis = new BufferedInputStream( fis, bisBufferSize ); - fileFormat = getAudioFileFormat( bis ); // throws UnsupportedAudioFileException - } finally { - fis.close(); - } - - return fileFormat; - } - - - /** - * 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 - */ - public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException { - - DataInputStream dis = null; - int headerSize; - AudioFileFormat fileFormat = null; - AudioFormat format = null; - - - fileFormat = getAudioFileFormat( stream ); // throws UnsupportedAudioFileException, IOException - - // if we passed this call, we have an AU file. - - format = fileFormat.getFormat(); - - dis = new DataInputStream(stream); - // now seek past the header - - dis.readInt(); // magic - headerSize = (format.isBigEndian()==true ? dis.readInt() : rllong(dis) ); - dis.skipBytes( headerSize - 8 ); - - - // we've got everything, and the stream should be at the - // beginning of the data chunk, so return an AudioInputStream. - - return new AudioInputStream(dis, format, fileFormat.getFrameLength()); - } - - - /** - * 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 - */ - public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException { - - InputStream urlStream = null; - BufferedInputStream bis = null; - AudioFileFormat fileFormat = null; - - urlStream = url.openStream(); // throws IOException - AudioInputStream result = null; - try { - bis = new BufferedInputStream( urlStream, bisBufferSize ); - result = getAudioInputStream( (InputStream)bis ); - } finally { - if (result == null) { - urlStream.close(); - } - } - return result; - } - - - /** - * 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 - */ - public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException { - - FileInputStream fis = null; - BufferedInputStream bis = null; - AudioFileFormat fileFormat = null; - - fis = new FileInputStream( file ); // throws IOException - AudioInputStream result = null; - // part of fix for 4325421 - try { - bis = new BufferedInputStream( fis, bisBufferSize ); - result = getAudioInputStream( (InputStream)bis ); - } finally { - if (result == null) { - fis.close(); - } - } - - return result; + dis.skipBytes(headerSize - nread); + AudioFormat format = new AudioFormat(encoding, sampleRate, + sampleSizeInBits, channels, + frameSize, (float) frameRate, + bigendian); + return new AuFileFormat(AudioFileFormat.Type.AU, dataSize + headerSize, + format, length); } } --- old/src/java.desktop/share/classes/com/sun/media/sound/SunFileReader.java 2015-07-16 21:58:01.000000000 +0300 +++ new/src/java.desktop/share/classes/com/sun/media/sound/SunFileReader.java 2015-07-16 21:58:00.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,13 @@ package com.sun.media.sound; +import java.io.BufferedInputStream; +import java.io.Closeable; +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 +39,6 @@ import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.spi.AudioFileReader; - - /** * Abstract File Reader class. * @@ -45,118 +46,100 @@ */ abstract class SunFileReader extends AudioFileReader { - // buffer size for temporary input streams - protected static final int bisBufferSize = 4096; - - /** - * Constructs a new SunFileReader object. + @Override + public final AudioFileFormat getAudioFileFormat(final InputStream stream) + throws UnsupportedAudioFileException, IOException { + stream.mark(200); + try { + return getAudioFileFormatImpl(stream); + } finally { + stream.reset(); + } + } + + @Override + public final AudioFileFormat getAudioFileFormat(final URL url) + throws UnsupportedAudioFileException, IOException { + try (InputStream stream = url.openStream()) { + return getAudioFileFormatImpl(new BufferedInputStream(stream)); + } + } + + @Override + public final AudioFileFormat getAudioFileFormat(final File file) + throws UnsupportedAudioFileException, IOException { + try (InputStream stream = new FileInputStream(file)) { + return getAudioFileFormatImpl(new BufferedInputStream(stream)); + } + } + + @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 */ - 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 - * @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 - + private static void closeSilently(final Closeable is) { + try { + is.close(); + } catch (final IOException ignored) { + // IOException is ignored + } + } /** * rllong --- old/src/java.desktop/share/classes/com/sun/media/sound/WaveExtensibleFileReader.java 2015-07-16 21:58:01.000000000 +0300 +++ new/src/java.desktop/share/classes/com/sun/media/sound/WaveExtensibleFileReader.java 2015-07-16 21:58:01.000000000 +0300 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 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 @@ -22,55 +22,40 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + package com.sun.media.sound; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.net.URL; import java.util.HashMap; import java.util.Map; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioFormat.Encoding; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.UnsupportedAudioFileException; -import javax.sound.sampled.AudioFormat.Encoding; -import javax.sound.sampled.spi.AudioFileReader; /** * WAVE file reader for files using format WAVE_FORMAT_EXTENSIBLE (0xFFFE). * * @author Karl Helgason */ -public final class WaveExtensibleFileReader extends AudioFileReader { - - static private class GUID { - long i1; - - int s1; - - int s2; - - int x1; - - int x2; - - int x3; - - int x4; - - int x5; - - int x6; - - int x7; - - int x8; +public final class WaveExtensibleFileReader extends SunFileReader { + private static class GUID { + private long i1; + private int s1; + private int s2; + private int x1; + private int x2; + private int x3; + private int x4; + private int x5; + private int x6; + private int x7; + private int x8; private GUID() { } @@ -105,10 +90,12 @@ return d; } + @Override public int hashCode() { return (int) i1; } + @Override public boolean equals(Object obj) { if (!(obj instanceof GUID)) return false; @@ -161,7 +148,7 @@ private static final GUID SUBTYPE_IEEE_FLOAT = new GUID(0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71); - private String decodeChannelMask(long channelmask) { + private static String decodeChannelMask(long channelmask) { StringBuilder sb = new StringBuilder(); long m = 1; for (int i = 0; i < allchannelnames.length; i++) { @@ -180,20 +167,8 @@ } - public AudioFileFormat getAudioFileFormat(InputStream stream) - throws UnsupportedAudioFileException, IOException { - - stream.mark(200); - AudioFileFormat format; - try { - format = internal_getAudioFileFormat(stream); - } finally { - stream.reset(); - } - return format; - } - - private AudioFileFormat internal_getAudioFileFormat(InputStream stream) + @Override + AudioFileFormat getAudioFileFormatImpl(final InputStream stream) throws UnsupportedAudioFileException, IOException { RIFFReader riffiterator = new RIFFReader(stream); @@ -244,12 +219,9 @@ break; } } - - if (!fmt_found) + if (!fmt_found || !data_found) { throw new UnsupportedAudioFileException(); - if (!data_found) - throw new UnsupportedAudioFileException(); - + } Map p = new HashMap(); String s_channelmask = decodeChannelMask(channelMask); if (s_channelmask != null) @@ -273,24 +245,22 @@ } else if (subFormat.equals(SUBTYPE_IEEE_FLOAT)) { audioformat = new AudioFormat(Encoding.PCM_FLOAT, samplerate, bits, channels, framesize, samplerate, false, p); - } else + } else { throw new UnsupportedAudioFileException(); - - AudioFileFormat fileformat = new AudioFileFormat( - AudioFileFormat.Type.WAVE, audioformat, - AudioSystem.NOT_SPECIFIED); - return fileformat; + } + return new AudioFileFormat(AudioFileFormat.Type.WAVE, audioformat, + AudioSystem.NOT_SPECIFIED); } - public AudioInputStream getAudioInputStream(InputStream stream) + @Override + public AudioInputStream getAudioInputStream(final InputStream stream) throws UnsupportedAudioFileException, IOException { AudioFileFormat format = getAudioFileFormat(stream); + // we've got everything, the stream is supported and it is at the + // beginning of the header, so find the data chunk again and return an + // AudioInputStream RIFFReader riffiterator = new RIFFReader(stream); - if (!riffiterator.getFormat().equals("RIFF")) - throw new UnsupportedAudioFileException(); - if (!riffiterator.getType().equals("WAVE")) - throw new UnsupportedAudioFileException(); while (riffiterator.hasNextChunk()) { RIFFReader chunk = riffiterator.nextChunk(); if (chunk.getFormat().equals("data")) { @@ -300,40 +270,4 @@ } throw new UnsupportedAudioFileException(); } - - public AudioFileFormat getAudioFileFormat(URL url) - throws UnsupportedAudioFileException, IOException { - InputStream stream = url.openStream(); - AudioFileFormat format; - try { - format = getAudioFileFormat(new BufferedInputStream(stream)); - } finally { - stream.close(); - } - return format; - } - - public AudioFileFormat getAudioFileFormat(File file) - throws UnsupportedAudioFileException, IOException { - InputStream stream = new FileInputStream(file); - AudioFileFormat format; - try { - format = getAudioFileFormat(new BufferedInputStream(stream)); - } finally { - stream.close(); - } - return format; - } - - public AudioInputStream getAudioInputStream(URL url) - throws UnsupportedAudioFileException, IOException { - return getAudioInputStream(new BufferedInputStream(url.openStream())); - } - - public AudioInputStream getAudioInputStream(File file) - throws UnsupportedAudioFileException, IOException { - return getAudioInputStream(new BufferedInputStream(new FileInputStream( - file))); - } - } --- old/src/java.desktop/share/classes/com/sun/media/sound/WaveFileReader.java 2015-07-16 21:58:02.000000000 +0300 +++ new/src/java.desktop/share/classes/com/sun/media/sound/WaveFileReader.java 2015-07-16 21:58:02.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 @@ -27,20 +27,14 @@ import java.io.DataInputStream; import java.io.EOFException; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.net.URL; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; -import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.UnsupportedAudioFileException; - - /** * WAVE file reader. * @@ -50,170 +44,12 @@ */ public final class WaveFileReader extends SunFileReader { - private static final int MAX_READ_LENGTH = 12; - - /** - * 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 - * @throws IOException if an I/O exception occurs - * @see InputStream#markSupported - * @see InputStream#mark - */ - public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException { - // fix for 4489272: AudioSystem.getAudioFileFormat() fails for InputStream, but works for URL - AudioFileFormat aff = getFMT(stream, true); - // the following is not strictly necessary - but was implemented like that in 1.3.0 - 1.4.1 - // so I leave it as it was. May remove this for 1.5.0 - stream.reset(); - return aff; - } - - - /** - * 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 - */ - public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException { - InputStream urlStream = url.openStream(); // throws IOException - AudioFileFormat fileFormat = null; - try { - fileFormat = getFMT(urlStream, false); - } finally { - urlStream.close(); - } - return fileFormat; - } - - - /** - * 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 - */ - public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException { - AudioFileFormat fileFormat = null; - FileInputStream fis = new FileInputStream(file); // throws IOException - // part of fix for 4325421 - try { - fileFormat = getFMT(fis, false); - } finally { - fis.close(); - } - - return fileFormat; - } - - - /** - * 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 - */ - public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException { - // getFMT leaves the input stream at the beginning of the audio data - AudioFileFormat fileFormat = getFMT(stream, true); // throws UnsupportedAudioFileException, IOException - - // we've got everything, and the stream is at the - // beginning of the audio data, so return an AudioInputStream. - return new AudioInputStream(stream, fileFormat.getFormat(), fileFormat.getFrameLength()); - } - - - /** - * 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 - */ - public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException { - InputStream urlStream = url.openStream(); // throws IOException - AudioFileFormat fileFormat = null; - try { - fileFormat = getFMT(urlStream, false); - } finally { - if (fileFormat == null) { - urlStream.close(); - } - } - return new AudioInputStream(urlStream, fileFormat.getFormat(), fileFormat.getFrameLength()); - } - - - /** - * 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 - */ - public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException { - FileInputStream fis = new FileInputStream(file); // throws IOException - AudioFileFormat fileFormat = null; - // part of fix for 4325421 - try { - fileFormat = getFMT(fis, false); - } finally { - if (fileFormat == null) { - fis.close(); - } - } - return new AudioInputStream(fis, fileFormat.getFormat(), fileFormat.getFrameLength()); - } - - - //-------------------------------------------------------------------- - - - private AudioFileFormat getFMT(InputStream stream, boolean doReset) throws UnsupportedAudioFileException, IOException { + @Override + AudioFileFormat getAudioFileFormatImpl(final InputStream stream) + throws UnsupportedAudioFileException, IOException { // assumes sream is rewound - int bytesRead; int nread = 0; int fmt; int length = 0; @@ -227,10 +63,6 @@ DataInputStream dis = new DataInputStream( stream ); - if (doReset) { - dis.mark(MAX_READ_LENGTH); - } - int magic = dis.readInt(); int fileLength = rllong(dis); int waveMagic = dis.readInt(); @@ -244,9 +76,6 @@ if ((magic != WaveFileFormat.RIFF_MAGIC) || (waveMagic != WaveFileFormat.WAVE_MAGIC)) { // not WAVE, throw UnsupportedAudioFileException - if (doReset) { - dis.reset(); - } throw new UnsupportedAudioFileException("not a WAVE file"); } --- old/src/java.desktop/share/classes/com/sun/media/sound/WaveFloatFileReader.java 2015-07-16 21:58:02.000000000 +0300 +++ new/src/java.desktop/share/classes/com/sun/media/sound/WaveFloatFileReader.java 2015-07-16 21:58:02.000000000 +0300 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 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 @@ -22,14 +22,11 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + package com.sun.media.sound; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.net.URL; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; @@ -37,29 +34,16 @@ import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.UnsupportedAudioFileException; -import javax.sound.sampled.spi.AudioFileReader; /** * Floating-point encoded (format 3) WAVE file loader. * * @author Karl Helgason */ -public final class WaveFloatFileReader extends AudioFileReader { +public final class WaveFloatFileReader extends SunFileReader { - public AudioFileFormat getAudioFileFormat(InputStream stream) - throws UnsupportedAudioFileException, IOException { - - stream.mark(200); - AudioFileFormat format; - try { - format = internal_getAudioFileFormat(stream); - } finally { - stream.reset(); - } - return format; - } - - private AudioFileFormat internal_getAudioFileFormat(InputStream stream) + @Override + AudioFileFormat getAudioFileFormatImpl(final InputStream stream) throws UnsupportedAudioFileException, IOException { RIFFReader riffiterator = new RIFFReader(stream); @@ -96,30 +80,25 @@ break; } } - - if (!fmt_found) + if (!fmt_found || !data_found) { throw new UnsupportedAudioFileException(); - if (!data_found) - throw new UnsupportedAudioFileException(); - + } AudioFormat audioformat = new AudioFormat( Encoding.PCM_FLOAT, samplerate, bits, channels, framesize, samplerate, false); - AudioFileFormat fileformat = new AudioFileFormat( - AudioFileFormat.Type.WAVE, audioformat, - AudioSystem.NOT_SPECIFIED); - return fileformat; + return new AudioFileFormat(AudioFileFormat.Type.WAVE, audioformat, + AudioSystem.NOT_SPECIFIED); } - public AudioInputStream getAudioInputStream(InputStream stream) + @Override + public AudioInputStream getAudioInputStream(final InputStream stream) throws UnsupportedAudioFileException, IOException { AudioFileFormat format = getAudioFileFormat(stream); + // we've got everything, the stream is supported and it is at the + // beginning of the header, so find the data chunk again and return an + // AudioInputStream RIFFReader riffiterator = new RIFFReader(stream); - if (!riffiterator.getFormat().equals("RIFF")) - throw new UnsupportedAudioFileException(); - if (!riffiterator.getType().equals("WAVE")) - throw new UnsupportedAudioFileException(); while (riffiterator.hasNextChunk()) { RIFFReader chunk = riffiterator.nextChunk(); if (chunk.getFormat().equals("data")) { @@ -129,39 +108,4 @@ } throw new UnsupportedAudioFileException(); } - - public AudioFileFormat getAudioFileFormat(URL url) - throws UnsupportedAudioFileException, IOException { - InputStream stream = url.openStream(); - AudioFileFormat format; - try { - format = getAudioFileFormat(new BufferedInputStream(stream)); - } finally { - stream.close(); - } - return format; - } - - public AudioFileFormat getAudioFileFormat(File file) - throws UnsupportedAudioFileException, IOException { - InputStream stream = new FileInputStream(file); - AudioFileFormat format; - try { - format = getAudioFileFormat(new BufferedInputStream(stream)); - } finally { - stream.close(); - } - return format; - } - - public AudioInputStream getAudioInputStream(URL url) - throws UnsupportedAudioFileException, IOException { - return getAudioInputStream(new BufferedInputStream(url.openStream())); - } - - public AudioInputStream getAudioInputStream(File file) - throws UnsupportedAudioFileException, IOException { - return getAudioInputStream(new BufferedInputStream(new FileInputStream( - file))); - } }