1 /* 2 * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.sound.sampled.spi; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.io.OutputStream; 31 import java.util.Arrays; 32 33 import javax.sound.sampled.AudioInputStream; 34 import javax.sound.sampled.AudioSystem; 35 36 import static javax.sound.sampled.AudioFileFormat.Type; 37 38 /** 39 * Provider for audio file writing services. Classes providing concrete 40 * implementations can write one or more types of audio file from an audio 41 * stream. 42 * 43 * @author Kara Kytle 44 * @since 1.3 45 */ 46 public abstract class AudioFileWriter { 47 48 /** 49 * Constructs an {@code AudioFileWriter}. 50 */ 51 protected AudioFileWriter() {} 52 53 /** 54 * Obtains the file types for which file writing support is provided by this 55 * audio file writer. 56 * 57 * @return array of file types. If no file types are supported, an array of 58 * length 0 is returned. 59 */ 60 public abstract Type[] getAudioFileTypes(); 61 62 /** 63 * Indicates whether file writing support for the specified file type is 64 * provided by this audio file writer. 65 * 66 * @param fileType the file type for which write capabilities are queried 67 * @return {@code true} if the file type is supported, otherwise 68 * {@code false} 69 * @throws NullPointerException if {@code fileType} is {@code null} 70 */ 71 public boolean isFileTypeSupported(final Type fileType) { 72 return Arrays.stream(getAudioFileTypes()).anyMatch(fileType::equals); 73 } 74 75 /** 76 * Obtains the file types that this audio file writer can write from the 77 * audio input stream specified. 78 * 79 * @param stream the audio input stream for which audio file type support 80 * is queried 81 * @return array of file types. If no file types are supported, an array of 82 * length 0 is returned. 83 * @throws NullPointerException if {@code stream} is {@code null} 84 */ 85 public abstract Type[] getAudioFileTypes(AudioInputStream stream); 86 87 /** 88 * Indicates whether an audio file of the type specified can be written from 89 * the audio input stream indicated. 90 * 91 * @param fileType file type for which write capabilities are queried 92 * @param stream for which file writing support is queried 93 * @return {@code true} if the file type is supported for this audio input 94 * stream, otherwise {@code false} 95 * @throws NullPointerException if {@code fileType} or {@code stream} are 96 * {@code null} 97 */ 98 public boolean isFileTypeSupported(final Type fileType, 99 final AudioInputStream stream) { 100 return Arrays.stream(getAudioFileTypes(stream)) 101 .anyMatch(fileType::equals); 102 } 103 104 /** 105 * Writes a stream of bytes representing an audio file of the file type 106 * indicated to the output stream provided. Some file types require that the 107 * length be written into the file header, and cannot be written from start 108 * to finish unless the length is known in advance. An attempt to write such 109 * a file type will fail with an {@code IOException} if the length in the 110 * audio file format is {@link AudioSystem#NOT_SPECIFIED}. 111 * 112 * @param stream the audio input stream containing audio data to be written 113 * to the output stream 114 * @param fileType file type to be written to the output stream 115 * @param out stream to which the file data should be written 116 * @return the number of bytes written to the output stream 117 * @throws IOException if an I/O exception occurs 118 * @throws IllegalArgumentException if the file type is not supported by the 119 * system 120 * @throws NullPointerException if {@code stream} or {@code fileType} or 121 * {@code out} are {@code null} 122 * @see #isFileTypeSupported(Type, AudioInputStream) 123 * @see #getAudioFileTypes 124 */ 125 public abstract int write(AudioInputStream stream, Type fileType, 126 OutputStream out) throws IOException; 127 128 /** 129 * Writes a stream of bytes representing an audio file of the file format 130 * indicated to the external file provided. 131 * 132 * @param stream the audio input stream containing audio data to be written 133 * to the file 134 * @param fileType file type to be written to the file 135 * @param out external file to which the file data should be written 136 * @return the number of bytes written to the file 137 * @throws IOException if an I/O exception occurs 138 * @throws IllegalArgumentException if the file format is not supported by 139 * the system 140 * @throws NullPointerException if {@code stream} or {@code fileType} or 141 * {@code out} are {@code null} 142 * @see #isFileTypeSupported(Type, AudioInputStream) 143 * @see #getAudioFileTypes 144 */ 145 public abstract int write(AudioInputStream stream, Type fileType, File out) 146 throws IOException; 147 }