1. /*
  2. * @(#)AudioFileWriter.java 1.24 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.sound.sampled.spi;
  8. import java.io.File;
  9. import java.io.InputStream;
  10. import java.io.IOException;
  11. import java.io.OutputStream;
  12. import javax.sound.sampled.AudioFileFormat;
  13. import javax.sound.sampled.AudioInputStream;
  14. /**
  15. * Provider for audio file writing services. Classes providing concrete
  16. * implementations can write one or more types of audio file from an audio
  17. * stream.
  18. *
  19. * @author Kara Kytle
  20. * @version 1.24, 03/12/19
  21. * @since 1.3
  22. */
  23. public abstract class AudioFileWriter {
  24. /**
  25. * Obtains the file types for which file writing support is provided by this
  26. * audio file writer.
  27. * @return array of file types. If no file types are supported,
  28. * an array of length 0 is returned.
  29. */
  30. public abstract AudioFileFormat.Type[] getAudioFileTypes();
  31. /**
  32. * Indicates whether file writing support for the specified file type is provided
  33. * by this audio file writer.
  34. * @param fileType the file type for which write capabilities are queried
  35. * @return <code>true</code> if the file type is supported,
  36. * otherwise <code>false</code>
  37. */
  38. public boolean isFileTypeSupported(AudioFileFormat.Type fileType) {
  39. AudioFileFormat.Type types[] = getAudioFileTypes();
  40. for(int i=0; i<types.length; i++) {
  41. if( fileType.equals( types[i] ) ) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. /**
  48. * Obtains the file types that this audio file writer can write from the
  49. * audio input stream specified.
  50. * @param stream the audio input stream for which audio file type support
  51. * is queried
  52. * @return array of file types. If no file types are supported,
  53. * an array of length 0 is returned.
  54. */
  55. public abstract AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream);
  56. /**
  57. * Indicates whether an audio file of the type specified can be written
  58. * from the audio input stream indicated.
  59. * @param fileType file type for which write capabilities are queried
  60. * @param stream for which file writing support is queried
  61. * @return <code>true</code> if the file type is supported for this audio input stream,
  62. * otherwise <code>false</code>
  63. */
  64. public boolean isFileTypeSupported(AudioFileFormat.Type fileType, AudioInputStream stream) {
  65. AudioFileFormat.Type types[] = getAudioFileTypes( stream );
  66. for(int i=0; i<types.length; i++) {
  67. if( fileType.equals( types[i] ) ) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. /**
  74. * Writes a stream of bytes representing an audio file of the file type
  75. * indicated to the output stream provided. Some file types require that
  76. * the length be written into the file header, and cannot be written from
  77. * start to finish unless the length is known in advance. An attempt
  78. * to write such a file type will fail with an IOException if the length in
  79. * the audio file format is
  80. * {@link javax.sound.sampled.AudioSystem#NOT_SPECIFIED AudioSystem.NOT_SPECIFIED}.
  81. * @param stream the audio input stream containing audio data to be
  82. * written to the output stream
  83. * @param fileType file type to be written to the output stream
  84. * @param out stream to which the file data should be written
  85. * @return the number of bytes written to the output stream
  86. * @throws IOException if an I/O exception occurs
  87. * @throws IllegalArgumentException if the file type is not supported by
  88. * the system
  89. * @see #isFileTypeSupported(AudioFileFormat.Type, AudioInputStream)
  90. * @see #getAudioFileTypes
  91. */
  92. public abstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException;
  93. /**
  94. * Writes a stream of bytes representing an audio file of the file format
  95. * indicated to the external file provided.
  96. * @param stream the audio input stream containing audio data to be
  97. * written to the file
  98. * @param fileType file type to be written to the file
  99. * @param out external file to which the file data should be written
  100. * @return the number of bytes written to the file
  101. * @throws IOException if an I/O exception occurs
  102. * @throws IllegalArgumentException if the file format is not supported by
  103. * the system
  104. * @see #isFileTypeSupported
  105. * @see #getAudioFileTypes
  106. */
  107. public abstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException;
  108. }