1 /* 2 * Copyright (c) 1999, 2018, 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 * Obtains the file types for which file writing support is provided by this 50 * audio file writer. 51 * 52 * @return array of file types. If no file types are supported, an array of 53 * length 0 is returned. 54 */ 55 public abstract Type[] getAudioFileTypes(); 56 57 /** 58 * Indicates whether file writing support for the specified file type is 59 * provided by this audio file writer. 60 * 61 * @param fileType the file type for which write capabilities are queried 62 * @return {@code true} if the file type is supported, otherwise 63 * {@code false} 64 * @throws NullPointerException if {@code fileType} is {@code null} 65 */ 66 public boolean isFileTypeSupported(final Type fileType) { 67 return Arrays.stream(getAudioFileTypes()).anyMatch(fileType::equals); 68 } 69 70 /** 71 * Obtains the file types that this audio file writer can write from the 72 * audio input stream specified. 73 * 74 * @param stream the audio input stream for which audio file type support 75 * is queried 76 * @return array of file types. If no file types are supported, an array of 77 * length 0 is returned. 78 * @throws NullPointerException if {@code stream} is {@code null} 79 */ 80 public abstract Type[] getAudioFileTypes(AudioInputStream stream); 81 82 /** 83 * Indicates whether an audio file of the type specified can be written from 84 * the audio input stream indicated. 85 * 86 * @param fileType file type for which write capabilities are queried 87 * @param stream for which file writing support is queried 88 * @return {@code true} if the file type is supported for this audio input 89 * stream, otherwise {@code false} 90 * @throws NullPointerException if {@code fileType} or {@code stream} are 91 * {@code null} 92 */ 93 public boolean isFileTypeSupported(final Type fileType, 94 final AudioInputStream stream) { 95 return Arrays.stream(getAudioFileTypes(stream)) 96 .anyMatch(fileType::equals); 97 } 98 99 /** 100 * Writes a stream of bytes representing an audio file of the file type 101 * indicated to the output stream provided. Some file types require that the 102 * length be written into the file header, and cannot be written from start 103 * to finish unless the length is known in advance. An attempt to write such 104 * a file type will fail with an {@code IOException} if the length in the 105 * audio file format is {@link AudioSystem#NOT_SPECIFIED}. 106 * 107 * @param stream the audio input stream containing audio data to be written 108 * to the output stream 109 * @param fileType file type to be written to the output stream 110 * @param out stream to which the file data should be written 111 * @return the number of bytes written to the output stream 112 * @throws IOException if an I/O exception occurs 113 * @throws IllegalArgumentException if the file type is not supported by the 114 * system 115 * @throws NullPointerException if {@code stream} or {@code fileType} or 116 * {@code out} are {@code null} 117 * @see #isFileTypeSupported(Type, AudioInputStream) 118 * @see #getAudioFileTypes 119 */ 120 public abstract int write(AudioInputStream stream, Type fileType, 121 OutputStream out) throws IOException; 122 123 /** 124 * Writes a stream of bytes representing an audio file of the file format 125 * indicated to the external file provided. 126 * 127 * @param stream the audio input stream containing audio data to be written 128 * to the file 129 * @param fileType file type to be written to the file 130 * @param out external file to which the file data should be written 131 * @return the number of bytes written to the file 132 * @throws IOException if an I/O exception occurs 133 * @throws IllegalArgumentException if the file format is not supported by 134 * the system 135 * @throws NullPointerException if {@code stream} or {@code fileType} or 136 * {@code out} are {@code null} 137 * @see #isFileTypeSupported(Type, AudioInputStream) 138 * @see #getAudioFileTypes 139 */ 140 public abstract int write(AudioInputStream stream, Type fileType, File out) 141 throws IOException; 142 }