src/java.desktop/share/classes/javax/sound/midi/spi/MidiFileWriter.java
author serb
Fri, 22 Jun 2018 19:19:43 -0700
changeset 50832 e069b9e49ca7
parent 47216 71c04702a3d5
permissions -rw-r--r--
8205456: Unification of iterations over arrays Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50832
e069b9e49ca7 8205456: Unification of iterations over arrays
serb
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.sound.midi.spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
50832
e069b9e49ca7 8205456: Unification of iterations over arrays
serb
parents: 47216
diff changeset
    31
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.sound.midi.Sequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    36
 * A {@code MidiFileWriter} supplies MIDI file-writing services. Classes that
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    37
 * implement this interface can write one or more types of MIDI file from a
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    38
 * {@link Sequence} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public abstract class MidiFileWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Obtains the set of MIDI file types for which file writing support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * provided by this file writer.
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    48
     *
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    49
     * @return array of file types. If no file types are supported, an array of
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    50
     *         length 0 is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public abstract int[] getMidiFileTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    55
     * Obtains the file types that this file writer can write from the sequence
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    56
     * specified.
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    57
     *
47195
b309b58eb190 8181566: JavaSound javadoc clarification
serb
parents: 34806
diff changeset
    58
     * @param  sequence the sequence for which MIDI file type support is queried
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    59
     * @return array of file types. If no file types are supported, returns an
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    60
     *         array of length 0.
34806
65f9c9d7b623 8143909: Behavior of null arguments not specified in javax.sound.midi.spi
serb
parents: 25859
diff changeset
    61
     * @throws NullPointerException if {@code sequence} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public abstract int[] getMidiFileTypes(Sequence sequence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Indicates whether file writing support for the specified MIDI file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * is provided by this file writer.
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    68
     *
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    69
     * @param  fileType the file type for which write capabilities are queried
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    70
     * @return {@code true} if the file type is supported, otherwise
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    71
     *         {@code false}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
50832
e069b9e49ca7 8205456: Unification of iterations over arrays
serb
parents: 47216
diff changeset
    73
    public boolean isFileTypeSupported(final int fileType) {
e069b9e49ca7 8205456: Unification of iterations over arrays
serb
parents: 47216
diff changeset
    74
        return Arrays.stream(getMidiFileTypes())
e069b9e49ca7 8205456: Unification of iterations over arrays
serb
parents: 47216
diff changeset
    75
                     .anyMatch(type -> fileType == type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Indicates whether a MIDI file of the file type specified can be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * from the sequence indicated.
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    81
     *
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    82
     * @param  fileType the file type for which write capabilities are queried
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    83
     * @param  sequence the sequence for which file writing support is queried
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    84
     * @return {@code true} if the file type is supported for this sequence,
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    85
     *         otherwise {@code false}
34806
65f9c9d7b623 8143909: Behavior of null arguments not specified in javax.sound.midi.spi
serb
parents: 25859
diff changeset
    86
     * @throws NullPointerException if {@code sequence} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
50832
e069b9e49ca7 8205456: Unification of iterations over arrays
serb
parents: 47216
diff changeset
    88
    public boolean isFileTypeSupported(final int fileType,
e069b9e49ca7 8205456: Unification of iterations over arrays
serb
parents: 47216
diff changeset
    89
                                       final Sequence sequence) {
e069b9e49ca7 8205456: Unification of iterations over arrays
serb
parents: 47216
diff changeset
    90
        return Arrays.stream(getMidiFileTypes(sequence))
e069b9e49ca7 8205456: Unification of iterations over arrays
serb
parents: 47216
diff changeset
    91
                     .anyMatch(type -> fileType == type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Writes a stream of bytes representing a MIDI file of the file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * indicated to the output stream provided.
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    97
     *
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    98
     * @param  in sequence containing MIDI data to be written to the file
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
    99
     * @param  fileType type of the file to be written to the output stream
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   100
     * @param  out stream to which the file data should be written
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return the number of bytes written to the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws IllegalArgumentException if the file type is not supported by
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   104
     *         this file writer
34806
65f9c9d7b623 8143909: Behavior of null arguments not specified in javax.sound.midi.spi
serb
parents: 25859
diff changeset
   105
     * @throws NullPointerException if {@code in} or {@code out} are
65f9c9d7b623 8143909: Behavior of null arguments not specified in javax.sound.midi.spi
serb
parents: 25859
diff changeset
   106
     *         {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @see #isFileTypeSupported(int, Sequence)
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   108
     * @see #getMidiFileTypes(Sequence)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   110
    public abstract int write(Sequence in, int fileType, OutputStream out)
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   111
            throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Writes a stream of bytes representing a MIDI file of the file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * indicated to the external file provided.
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   116
     *
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   117
     * @param  in sequence containing MIDI data to be written to the external
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   118
     *         file
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   119
     * @param  fileType type of the file to be written to the external file
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   120
     * @param  out external file to which the file data should be written
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return the number of bytes written to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IllegalArgumentException if the file type is not supported by
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   124
     *         this file writer
34806
65f9c9d7b623 8143909: Behavior of null arguments not specified in javax.sound.midi.spi
serb
parents: 25859
diff changeset
   125
     * @throws NullPointerException if {@code in} or {@code out} are
65f9c9d7b623 8143909: Behavior of null arguments not specified in javax.sound.midi.spi
serb
parents: 25859
diff changeset
   126
     *         {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @see #isFileTypeSupported(int, Sequence)
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   128
     * @see #getMidiFileTypes(Sequence)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
23688
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   130
    public abstract int write(Sequence in, int fileType, File out)
6781b500dfca 8037117: Javadoc cleanup of javax.sound.midi.spi package
serb
parents: 5506
diff changeset
   131
            throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}