AudioFileWriter.java revision 17756:c6f9f9e5f186
1/* 2 * Copyright (c) 1999, 2017, 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 26package javax.sound.sampled.spi; 27 28import java.io.File; 29import java.io.IOException; 30import java.io.OutputStream; 31import java.util.Objects; 32 33import javax.sound.sampled.AudioInputStream; 34import javax.sound.sampled.AudioSystem; 35 36import 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 */ 46public 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(Type fileType) { 67 Objects.requireNonNull(fileType); 68 69 Type types[] = getAudioFileTypes(); 70 71 for(int i=0; i<types.length; i++) { 72 if( fileType.equals( types[i] ) ) { 73 return true; 74 } 75 } 76 return false; 77 } 78 79 /** 80 * Obtains the file types that this audio file writer can write from the 81 * audio input stream specified. 82 * 83 * @param stream the audio input stream for which audio file type support 84 * is queried 85 * @return array of file types. If no file types are supported, an array of 86 * length 0 is returned. 87 * @throws NullPointerException if {@code stream} is {@code null} 88 */ 89 public abstract Type[] getAudioFileTypes(AudioInputStream stream); 90 91 /** 92 * Indicates whether an audio file of the type specified can be written from 93 * the audio input stream indicated. 94 * 95 * @param fileType file type for which write capabilities are queried 96 * @param stream for which file writing support is queried 97 * @return {@code true} if the file type is supported for this audio input 98 * stream, otherwise {@code false} 99 * @throws NullPointerException if {@code fileType} or {@code stream} are 100 * {@code null} 101 */ 102 public boolean isFileTypeSupported(Type fileType, AudioInputStream stream) { 103 Objects.requireNonNull(fileType); 104 Type types[] = getAudioFileTypes( stream ); 105 106 for(int i=0; i<types.length; i++) { 107 if( fileType.equals( types[i] ) ) { 108 return true; 109 } 110 } 111 return false; 112 } 113 114 /** 115 * Writes a stream of bytes representing an audio file of the file type 116 * indicated to the output stream provided. Some file types require that the 117 * length be written into the file header, and cannot be written from start 118 * to finish unless the length is known in advance. An attempt to write such 119 * a file type will fail with an {@code IOException} if the length in the 120 * audio file format is {@link AudioSystem#NOT_SPECIFIED}. 121 * 122 * @param stream the audio input stream containing audio data to be written 123 * to the output stream 124 * @param fileType file type to be written to the output stream 125 * @param out stream to which the file data should be written 126 * @return the number of bytes written to the output stream 127 * @throws IOException if an I/O exception occurs 128 * @throws IllegalArgumentException if the file type is not supported by the 129 * system 130 * @throws NullPointerException if {@code stream} or {@code fileType} or 131 * {@code out} are {@code null} 132 * @see #isFileTypeSupported(Type, AudioInputStream) 133 * @see #getAudioFileTypes 134 */ 135 public abstract int write(AudioInputStream stream, Type fileType, 136 OutputStream out) throws IOException; 137 138 /** 139 * Writes a stream of bytes representing an audio file of the file format 140 * indicated to the external file provided. 141 * 142 * @param stream the audio input stream containing audio data to be written 143 * to the file 144 * @param fileType file type to be written to the file 145 * @param out external file to which the file data should be written 146 * @return the number of bytes written to the file 147 * @throws IOException if an I/O exception occurs 148 * @throws IllegalArgumentException if the file format is not supported by 149 * the system 150 * @throws NullPointerException if {@code stream} or {@code fileType} or 151 * {@code out} are {@code null} 152 * @see #isFileTypeSupported(Type, AudioInputStream) 153 * @see #getAudioFileTypes 154 */ 155 public abstract int write(AudioInputStream stream, Type fileType, File out) 156 throws IOException; 157} 158