Path: blob/master/src/java.desktop/share/classes/javax/sound/midi/spi/MidiFileWriter.java
41171 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.midi.spi;2627import java.io.File;28import java.io.IOException;29import java.io.OutputStream;30import java.util.Arrays;3132import javax.sound.midi.Sequence;3334/**35* A {@code MidiFileWriter} supplies MIDI file-writing services. Classes that36* implement this interface can write one or more types of MIDI file from a37* {@link Sequence} object.38*39* @author Kara Kytle40* @since 1.341*/42public abstract class MidiFileWriter {4344/**45* Constructor for subclasses to call.46*/47protected MidiFileWriter() {}4849/**50* Obtains the set of MIDI file types for which file writing support is51* provided by this file writer.52*53* @return array of file types. If no file types are supported, an array of54* length 0 is returned.55*/56public abstract int[] getMidiFileTypes();5758/**59* Obtains the file types that this file writer can write from the sequence60* specified.61*62* @param sequence the sequence for which MIDI file type support is queried63* @return array of file types. If no file types are supported, returns an64* array of length 0.65* @throws NullPointerException if {@code sequence} is {@code null}66*/67public abstract int[] getMidiFileTypes(Sequence sequence);6869/**70* Indicates whether file writing support for the specified MIDI file type71* is provided by this file writer.72*73* @param fileType the file type for which write capabilities are queried74* @return {@code true} if the file type is supported, otherwise75* {@code false}76*/77public boolean isFileTypeSupported(final int fileType) {78return Arrays.stream(getMidiFileTypes())79.anyMatch(type -> fileType == type);80}8182/**83* Indicates whether a MIDI file of the file type specified can be written84* from the sequence indicated.85*86* @param fileType the file type for which write capabilities are queried87* @param sequence the sequence for which file writing support is queried88* @return {@code true} if the file type is supported for this sequence,89* otherwise {@code false}90* @throws NullPointerException if {@code sequence} is {@code null}91*/92public boolean isFileTypeSupported(final int fileType,93final Sequence sequence) {94return Arrays.stream(getMidiFileTypes(sequence))95.anyMatch(type -> fileType == type);96}9798/**99* Writes a stream of bytes representing a MIDI file of the file type100* indicated to the output stream provided.101*102* @param in sequence containing MIDI data to be written to the file103* @param fileType type of the file to be written to the output stream104* @param out stream to which the file data should be written105* @return the number of bytes written to the output stream106* @throws IOException if an I/O exception occurs107* @throws IllegalArgumentException if the file type is not supported by108* this file writer109* @throws NullPointerException if {@code in} or {@code out} are110* {@code null}111* @see #isFileTypeSupported(int, Sequence)112* @see #getMidiFileTypes(Sequence)113*/114public abstract int write(Sequence in, int fileType, OutputStream out)115throws IOException;116117/**118* Writes a stream of bytes representing a MIDI file of the file type119* indicated to the external file provided.120*121* @param in sequence containing MIDI data to be written to the external122* file123* @param fileType type of the file to be written to the external file124* @param out external file to which the file data should be written125* @return the number of bytes written to the file126* @throws IOException if an I/O exception occurs127* @throws IllegalArgumentException if the file type is not supported by128* this file writer129* @throws NullPointerException if {@code in} or {@code out} are130* {@code null}131* @see #isFileTypeSupported(int, Sequence)132* @see #getMidiFileTypes(Sequence)133*/134public abstract int write(Sequence in, int fileType, File out)135throws IOException;136}137138139