1. /*
  2. * @(#)MidiFileWriter.java 1.17 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.midi.spi;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. import javax.sound.midi.Sequence;
  12. import javax.sound.midi.MidiFileFormat;
  13. /**
  14. * A <code>MidiFileWriter</code> supplies MIDI file-writing services. Classes
  15. * that implement this interface can write one or more types of MIDI file from
  16. * a <code>{@link Sequence}</code> object.
  17. *
  18. * @author Kara Kytle
  19. * @version 1.17, 03/12/19
  20. * @since 1.3
  21. */
  22. public abstract class MidiFileWriter {
  23. /**
  24. * Obtains the set of MIDI file types for which file writing support is
  25. * provided by this file writer.
  26. * @return array of file types. If no file types are supported,
  27. * an array of length 0 is returned.
  28. */
  29. public abstract int[] getMidiFileTypes();
  30. /**
  31. * Obtains the file types that this file writer can write from the
  32. * sequence specified.
  33. * @param sequence the sequence for which MIDI file type support
  34. * is queried
  35. * @return array of file types. If no file types are supported,
  36. * returns an array of length 0.
  37. */
  38. public abstract int[] getMidiFileTypes(Sequence sequence);
  39. /**
  40. * Indicates whether file writing support for the specified MIDI file type
  41. * is provided by this file writer.
  42. * @param fileType the file type for which write capabilities are queried
  43. * @return <code>true</code> if the file type is supported,
  44. * otherwise <code>false</code>
  45. */
  46. public boolean isFileTypeSupported(int fileType) {
  47. int types[] = getMidiFileTypes();
  48. for(int i=0; i<types.length; i++) {
  49. if( fileType == types[i] ) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. /**
  56. * Indicates whether a MIDI file of the file type specified can be written
  57. * from the sequence indicated.
  58. * @param fileType the file type for which write capabilities are queried
  59. * @param sequence the sequence for which file writing support is queried
  60. * @return <code>true</code> if the file type is supported for this sequence,
  61. * otherwise <code>false</code>
  62. */
  63. public boolean isFileTypeSupported(int fileType, Sequence sequence) {
  64. int types[] = getMidiFileTypes( sequence );
  65. for(int i=0; i<types.length; i++) {
  66. if( fileType == types[i] ) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. /**
  73. * Writes a stream of bytes representing a MIDI file of the file type
  74. * indicated to the output stream provided.
  75. * @param in sequence containing MIDI data to be written to the file
  76. * @param fileType type of the file to be written to the output stream
  77. * @param out stream to which the file data should be written
  78. * @return the number of bytes written to the output stream
  79. * @throws IOException if an I/O exception occurs
  80. * @throws IllegalArgumentException if the file type is not supported by
  81. * this file writer
  82. * @see #isFileTypeSupported(int, Sequence)
  83. * @see #getMidiFileTypes(Sequence)
  84. */
  85. public abstract int write(Sequence in, int fileType, OutputStream out) throws IOException;
  86. /**
  87. * Writes a stream of bytes representing a MIDI file of the file type
  88. * indicated to the external file provided.
  89. * @param in sequence containing MIDI data to be written to the external file
  90. * @param fileType type of the file to be written to the external file
  91. * @param out external file to which the file data should be written
  92. * @return the number of bytes written to the file
  93. * @throws IOException if an I/O exception occurs
  94. * @throws IllegalArgumentException if the file type is not supported by
  95. * this file writer
  96. * @see #isFileTypeSupported(int, Sequence)
  97. * @see #getMidiFileTypes(Sequence)
  98. */
  99. public abstract int write(Sequence in, int fileType, File out) throws IOException;
  100. }