mirror of
https://github.com/openjdk/jdk.git
synced 2026-06-06 02:33:12 +00:00
7013521: AudioSystem.write for AIFF files closes source audio stream
Reviewed-by: dav
This commit is contained in:
parent
23f78001e2
commit
eb75b23d93
@ -25,12 +25,10 @@
|
||||
|
||||
package com.sun.media.sound;
|
||||
|
||||
import java.util.Vector;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.DataInputStream;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
@ -398,7 +396,8 @@ public class AiffFileWriter extends SunFileWriter {
|
||||
header = baos.toByteArray();
|
||||
headerStream = new ByteArrayInputStream( header );
|
||||
|
||||
aiffStream = new SequenceInputStream(headerStream,codedAudioStream);
|
||||
aiffStream = new SequenceInputStream(headerStream,
|
||||
new NoCloseInputStream(codedAudioStream));
|
||||
|
||||
return aiffStream;
|
||||
|
||||
|
||||
@ -25,12 +25,10 @@
|
||||
|
||||
package com.sun.media.sound;
|
||||
|
||||
import java.util.Vector;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.IllegalArgumentException;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
@ -131,10 +129,10 @@ public class AuFileWriter extends SunFileWriter {
|
||||
// $$fb: 2001-07-13: done. Fixes Bug 4479981
|
||||
RandomAccessFile raf=new RandomAccessFile(out, "rw");
|
||||
if (raf.length()<=0x7FFFFFFFl) {
|
||||
// skip AU magic and data offset field
|
||||
// skip AU magic and data offset field
|
||||
raf.skipBytes(8);
|
||||
raf.writeInt(bytesWritten-AuFileFormat.AU_HEADERSIZE);
|
||||
// that's all
|
||||
// that's all
|
||||
}
|
||||
raf.close();
|
||||
}
|
||||
@ -303,7 +301,8 @@ public class AuFileWriter extends SunFileWriter {
|
||||
dos.close();
|
||||
header = baos.toByteArray();
|
||||
headerStream = new ByteArrayInputStream( header );
|
||||
auStream = new SequenceInputStream(headerStream,codedAudioStream);
|
||||
auStream = new SequenceInputStream(headerStream,
|
||||
new NoCloseInputStream(codedAudioStream));
|
||||
|
||||
return auStream;
|
||||
}
|
||||
|
||||
@ -30,14 +30,9 @@ import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
import javax.sound.sampled.AudioFileFormat;
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
import javax.sound.sampled.spi.AudioFileWriter;
|
||||
|
||||
|
||||
@ -177,4 +172,62 @@ abstract class SunFileWriter extends AudioFileWriter {
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* InputStream wrapper class which prevent source stream from being closed.
|
||||
* The class is usefull for use with SequenceInputStream to prevent
|
||||
* closing of the source input streams.
|
||||
*/
|
||||
protected class NoCloseInputStream extends InputStream {
|
||||
private final InputStream in;
|
||||
|
||||
public NoCloseInputStream(InputStream in) {
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return in.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte b[]) throws IOException {
|
||||
return in.read(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte b[], int off, int len) throws IOException {
|
||||
return in.read(b, off, len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long skip(long n) throws IOException {
|
||||
return in.skip(n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
return in.available();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
// don't propagate the call
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mark(int readlimit) {
|
||||
in.mark(readlimit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() throws IOException {
|
||||
in.reset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean markSupported() {
|
||||
return in.markSupported();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,12 +25,10 @@
|
||||
|
||||
package com.sun.media.sound;
|
||||
|
||||
import java.util.Vector;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.IllegalArgumentException;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
@ -371,7 +369,8 @@ public class WaveFileWriter extends SunFileWriter {
|
||||
dos.close();
|
||||
header = baos.toByteArray();
|
||||
headerStream = new ByteArrayInputStream( header );
|
||||
waveStream = new SequenceInputStream(headerStream,codedAudioStream);
|
||||
waveStream = new SequenceInputStream(headerStream,
|
||||
new NoCloseInputStream(codedAudioStream));
|
||||
|
||||
return (InputStream)waveStream;
|
||||
}
|
||||
|
||||
127
jdk/test/javax/sound/sampled/FileWriter/WriterCloseInput.java
Normal file
127
jdk/test/javax/sound/sampled/FileWriter/WriterCloseInput.java
Normal file
@ -0,0 +1,127 @@
|
||||
/**
|
||||
* @test
|
||||
* @bug 7013521
|
||||
* @summary AIFF/AU/WAVE writers close input audio stream
|
||||
* @author Alex Menkov
|
||||
*/
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import javax.sound.sampled.AudioFileFormat;
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
|
||||
|
||||
public class WriterCloseInput {
|
||||
|
||||
final static AudioFormat audioFormat = new AudioFormat(44100f, 16, 2, true, true);
|
||||
//final static AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.ULAW, 44100f, 8, 2, 2, 44100f, true);
|
||||
final static int frameLength = 44100 * 2; // 2 seconds
|
||||
final static byte[] dataBuffer
|
||||
= new byte[frameLength * (audioFormat.getSampleSizeInBits()/8)
|
||||
* audioFormat.getChannels()];
|
||||
|
||||
static int testTotal = 0;
|
||||
static int testFailed = 0;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
test(AudioFileFormat.Type.AIFF);
|
||||
test(AudioFileFormat.Type.AU);
|
||||
test(AudioFileFormat.Type.WAVE);
|
||||
|
||||
if (testFailed == 0) {
|
||||
out("All tests passed.");
|
||||
} else {
|
||||
out("" + testFailed + " of " + testTotal + " tests FAILED.");
|
||||
System.out.flush();
|
||||
throw new RuntimeException("Test FAILED.");
|
||||
}
|
||||
}
|
||||
|
||||
static void test(AudioFileFormat.Type fileType) {
|
||||
test(fileType, frameLength);
|
||||
test(fileType, AudioSystem.NOT_SPECIFIED);
|
||||
}
|
||||
|
||||
static void test(AudioFileFormat.Type fileType, int length) {
|
||||
test(fileType, length, false);
|
||||
test(fileType, length, true);
|
||||
}
|
||||
|
||||
static void test(AudioFileFormat.Type fileType, int length, boolean isFile) {
|
||||
testTotal++;
|
||||
out("Testing fileType: " + fileType
|
||||
+ ", frameLength: " + (length >= 0 ? length : "unspecified")
|
||||
+ ", output: " + (isFile ? "File" : "OutputStream"));
|
||||
AudioInputStream inStream = new ThrowAfterCloseStream(
|
||||
new ByteArrayInputStream(dataBuffer), audioFormat, length);
|
||||
|
||||
AudioSystem.isFileTypeSupported(fileType, inStream);
|
||||
|
||||
try {
|
||||
if (isFile) {
|
||||
File f = File.createTempFile("WriterCloseInput" + testTotal, "tmp");
|
||||
AudioSystem.write(inStream, fileType, f);
|
||||
f.delete();
|
||||
} else {
|
||||
OutputStream outStream = new NullOutputStream();
|
||||
AudioSystem.write(inStream, fileType, outStream);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
// this is not failure
|
||||
out("SKIPPED (AudioSystem.write exception): " + ex.getMessage());
|
||||
//out(ex);
|
||||
inStream = null;
|
||||
}
|
||||
|
||||
if (inStream != null) {
|
||||
try {
|
||||
// test if the stream is closed
|
||||
inStream.available();
|
||||
out("PASSED");
|
||||
} catch (IOException ex) {
|
||||
testFailed++;
|
||||
out("FAILED: " + ex.getMessage());
|
||||
//out(ex);
|
||||
}
|
||||
}
|
||||
out("");
|
||||
}
|
||||
|
||||
static class ThrowAfterCloseStream extends AudioInputStream {
|
||||
private boolean closed = false;
|
||||
public ThrowAfterCloseStream(InputStream in, AudioFormat format, long length) {
|
||||
super(in, format, length);
|
||||
}
|
||||
@Override
|
||||
public void close() {
|
||||
closed = true;
|
||||
}
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
if (closed) {
|
||||
throw new IOException("The stream has been closed");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static class NullOutputStream extends OutputStream {
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
// nop
|
||||
}
|
||||
}
|
||||
|
||||
static void out(String s) {
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
static void out(Exception ex) {
|
||||
ex.printStackTrace(System.out);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user