Path: blob/master/src/java.desktop/share/classes/javax/sound/sampled/spi/AudioFileWriter.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.sampled.spi;2627import java.io.File;28import java.io.IOException;29import java.io.OutputStream;30import java.util.Arrays;3132import javax.sound.sampled.AudioInputStream;33import javax.sound.sampled.AudioSystem;3435import static javax.sound.sampled.AudioFileFormat.Type;3637/**38* Provider for audio file writing services. Classes providing concrete39* implementations can write one or more types of audio file from an audio40* stream.41*42* @author Kara Kytle43* @since 1.344*/45public abstract class AudioFileWriter {4647/**48* Constructor for subclasses to call.49*/50protected AudioFileWriter() {}5152/**53* Obtains the file types for which file writing support is provided by this54* audio file writer.55*56* @return array of file types. If no file types are supported, an array of57* length 0 is returned.58*/59public abstract Type[] getAudioFileTypes();6061/**62* Indicates whether file writing support for the specified file type is63* provided by this audio file writer.64*65* @param fileType the file type for which write capabilities are queried66* @return {@code true} if the file type is supported, otherwise67* {@code false}68* @throws NullPointerException if {@code fileType} is {@code null}69*/70public boolean isFileTypeSupported(final Type fileType) {71return Arrays.stream(getAudioFileTypes()).anyMatch(fileType::equals);72}7374/**75* Obtains the file types that this audio file writer can write from the76* audio input stream specified.77*78* @param stream the audio input stream for which audio file type support79* is queried80* @return array of file types. If no file types are supported, an array of81* length 0 is returned.82* @throws NullPointerException if {@code stream} is {@code null}83*/84public abstract Type[] getAudioFileTypes(AudioInputStream stream);8586/**87* Indicates whether an audio file of the type specified can be written from88* the audio input stream indicated.89*90* @param fileType file type for which write capabilities are queried91* @param stream for which file writing support is queried92* @return {@code true} if the file type is supported for this audio input93* stream, otherwise {@code false}94* @throws NullPointerException if {@code fileType} or {@code stream} are95* {@code null}96*/97public boolean isFileTypeSupported(final Type fileType,98final AudioInputStream stream) {99return Arrays.stream(getAudioFileTypes(stream))100.anyMatch(fileType::equals);101}102103/**104* Writes a stream of bytes representing an audio file of the file type105* indicated to the output stream provided. Some file types require that the106* length be written into the file header, and cannot be written from start107* to finish unless the length is known in advance. An attempt to write such108* a file type will fail with an {@code IOException} if the length in the109* audio file format is {@link AudioSystem#NOT_SPECIFIED}.110*111* @param stream the audio input stream containing audio data to be written112* to the output stream113* @param fileType file type to be written to the output stream114* @param out stream to which the file data should be written115* @return the number of bytes written to the output stream116* @throws IOException if an I/O exception occurs117* @throws IllegalArgumentException if the file type is not supported by the118* system119* @throws NullPointerException if {@code stream} or {@code fileType} or120* {@code out} are {@code null}121* @see #isFileTypeSupported(Type, AudioInputStream)122* @see #getAudioFileTypes123*/124public abstract int write(AudioInputStream stream, Type fileType,125OutputStream out) throws IOException;126127/**128* Writes a stream of bytes representing an audio file of the file format129* indicated to the external file provided.130*131* @param stream the audio input stream containing audio data to be written132* to the file133* @param fileType file type to be written to the file134* @param out external file to which the file data should be written135* @return the number of bytes written to the file136* @throws IOException if an I/O exception occurs137* @throws IllegalArgumentException if the file format is not supported by138* the system139* @throws NullPointerException if {@code stream} or {@code fileType} or140* {@code out} are {@code null}141* @see #isFileTypeSupported(Type, AudioInputStream)142* @see #getAudioFileTypes143*/144public abstract int write(AudioInputStream stream, Type fileType, File out)145throws IOException;146}147148149