Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sound/sampled/AudioFileFormat.java
38918 views
/*1* Copyright (c) 1999, 2013, 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;2627import java.io.File;28import java.io.OutputStream;29import java.io.IOException;30import java.util.Collections;31import java.util.HashMap;32import java.util.Map;333435/**36* An instance of the <code>AudioFileFormat</code> class describes37* an audio file, including the file type, the file's length in bytes,38* the length in sample frames of the audio data contained in the file,39* and the format of the audio data.40* <p>41* The <code>{@link AudioSystem}</code> class includes methods for determining the format42* of an audio file, obtaining an audio input stream from an audio file, and43* writing an audio file from an audio input stream.44*45* <p>An <code>AudioFileFormat</code> object can46* include a set of properties. A property is a pair of key and value:47* the key is of type <code>String</code>, the associated property48* value is an arbitrary object.49* Properties specify additional informational50* meta data (like a author, copyright, or file duration).51* Properties are optional information, and file reader and file52* writer implementations are not required to provide or53* recognize properties.54*55* <p>The following table lists some common properties that should56* be used in implementations:57*58* <table border=1>59* <caption>Audio File Format Properties</caption>60* <tr>61* <th>Property key</th>62* <th>Value type</th>63* <th>Description</th>64* </tr>65* <tr>66* <td>"duration"</td>67* <td>{@link java.lang.Long Long}</td>68* <td>playback duration of the file in microseconds</td>69* </tr>70* <tr>71* <td>"author"</td>72* <td>{@link java.lang.String String}</td>73* <td>name of the author of this file</td>74* </tr>75* <tr>76* <td>"title"</td>77* <td>{@link java.lang.String String}</td>78* <td>title of this file</td>79* </tr>80* <tr>81* <td>"copyright"</td>82* <td>{@link java.lang.String String}</td>83* <td>copyright message</td>84* </tr>85* <tr>86* <td>"date"</td>87* <td>{@link java.util.Date Date}</td>88* <td>date of the recording or release</td>89* </tr>90* <tr>91* <td>"comment"</td>92* <td>{@link java.lang.String String}</td>93* <td>an arbitrary text</td>94* </tr>95* </table>96*97*98* @author David Rivas99* @author Kara Kytle100* @author Florian Bomers101* @see AudioInputStream102* @since 1.3103*/104public class AudioFileFormat {105106107// INSTANCE VARIABLES108109110/**111* File type.112*/113private Type type;114115/**116* File length in bytes117*/118private int byteLength;119120/**121* Format of the audio data contained in the file.122*/123private AudioFormat format;124125/**126* Audio data length in sample frames127*/128private int frameLength;129130131/** The set of properties */132private HashMap<String, Object> properties;133134135/**136* Constructs an audio file format object.137* This protected constructor is intended for use by providers of file-reading138* services when returning information about an audio file or about supported audio file139* formats.140* @param type the type of the audio file141* @param byteLength the length of the file in bytes, or <code>AudioSystem.NOT_SPECIFIED</code>142* @param format the format of the audio data contained in the file143* @param frameLength the audio data length in sample frames, or <code>AudioSystem.NOT_SPECIFIED</code>144*145* @see #getType146*/147protected AudioFileFormat(Type type, int byteLength, AudioFormat format, int frameLength) {148149this.type = type;150this.byteLength = byteLength;151this.format = format;152this.frameLength = frameLength;153this.properties = null;154}155156157/**158* Constructs an audio file format object.159* This public constructor may be used by applications to describe the160* properties of a requested audio file.161* @param type the type of the audio file162* @param format the format of the audio data contained in the file163* @param frameLength the audio data length in sample frames, or <code>AudioSystem.NOT_SPECIFIED</code>164*/165public AudioFileFormat(Type type, AudioFormat format, int frameLength) {166167168this(type,AudioSystem.NOT_SPECIFIED,format,frameLength);169}170171/**172* Construct an audio file format object with a set of173* defined properties.174* This public constructor may be used by applications to describe the175* properties of a requested audio file. The properties map176* will be copied to prevent any changes to it.177*178* @param type the type of the audio file179* @param format the format of the audio data contained in the file180* @param frameLength the audio data length in sample frames, or181* <code>AudioSystem.NOT_SPECIFIED</code>182* @param properties a <code>Map<String,Object></code> object183* with properties184*185* @since 1.5186*/187public AudioFileFormat(Type type, AudioFormat format,188int frameLength, Map<String, Object> properties) {189this(type,AudioSystem.NOT_SPECIFIED,format,frameLength);190this.properties = new HashMap<String, Object>(properties);191}192193194/**195* Obtains the audio file type, such as <code>WAVE</code> or <code>AU</code>.196* @return the audio file type197*198* @see Type#WAVE199* @see Type#AU200* @see Type#AIFF201* @see Type#AIFC202* @see Type#SND203*/204public Type getType() {205return type;206}207208/**209* Obtains the size in bytes of the entire audio file (not just its audio data).210* @return the audio file length in bytes211* @see AudioSystem#NOT_SPECIFIED212*/213public int getByteLength() {214return byteLength;215}216217/**218* Obtains the format of the audio data contained in the audio file.219* @return the audio data format220*/221public AudioFormat getFormat() {222return format;223}224225/**226* Obtains the length of the audio data contained in the file, expressed in sample frames.227* @return the number of sample frames of audio data in the file228* @see AudioSystem#NOT_SPECIFIED229*/230public int getFrameLength() {231return frameLength;232}233234/**235* Obtain an unmodifiable map of properties.236* The concept of properties is further explained in237* the {@link AudioFileFormat class description}.238*239* @return a <code>Map<String,Object></code> object containing240* all properties. If no properties are recognized, an empty map is241* returned.242*243* @see #getProperty(String)244* @since 1.5245*/246public Map<String,Object> properties() {247Map<String,Object> ret;248if (properties == null) {249ret = new HashMap<String,Object>(0);250} else {251ret = (Map<String,Object>) (properties.clone());252}253return (Map<String,Object>) Collections.unmodifiableMap(ret);254}255256257/**258* Obtain the property value specified by the key.259* The concept of properties is further explained in260* the {@link AudioFileFormat class description}.261*262* <p>If the specified property is not defined for a263* particular file format, this method returns264* <code>null</code>.265*266* @param key the key of the desired property267* @return the value of the property with the specified key,268* or <code>null</code> if the property does not exist.269*270* @see #properties()271* @since 1.5272*/273public Object getProperty(String key) {274if (properties == null) {275return null;276}277return properties.get(key);278}279280281/**282* Provides a string representation of the file format.283* @return the file format as a string284*/285public String toString() {286287StringBuffer buf = new StringBuffer();288289//$$fb2002-11-01: fix for 4672864: AudioFileFormat.toString() throws unexpected NullPointerException290if (type != null) {291buf.append(type.toString() + " (." + type.getExtension() + ") file");292} else {293buf.append("unknown file format");294}295296if (byteLength != AudioSystem.NOT_SPECIFIED) {297buf.append(", byte length: " + byteLength);298}299300buf.append(", data format: " + format);301302if (frameLength != AudioSystem.NOT_SPECIFIED) {303buf.append(", frame length: " + frameLength);304}305306return new String(buf);307}308309310/**311* An instance of the <code>Type</code> class represents one of the312* standard types of audio file. Static instances are provided for the313* common types.314*/315public static class Type {316317// FILE FORMAT TYPE DEFINES318319/**320* Specifies a WAVE file.321*/322public static final Type WAVE = new Type("WAVE", "wav");323324/**325* Specifies an AU file.326*/327public static final Type AU = new Type("AU", "au");328329/**330* Specifies an AIFF file.331*/332public static final Type AIFF = new Type("AIFF", "aif");333334/**335* Specifies an AIFF-C file.336*/337public static final Type AIFC = new Type("AIFF-C", "aifc");338339/**340* Specifies a SND file.341*/342public static final Type SND = new Type("SND", "snd");343344345// INSTANCE VARIABLES346347/**348* File type name.349*/350private final String name;351352/**353* File type extension.354*/355private final String extension;356357358// CONSTRUCTOR359360/**361* Constructs a file type.362* @param name the string that names the file type363* @param extension the string that commonly marks the file type364* without leading dot.365*/366public Type(String name, String extension) {367368this.name = name;369this.extension = extension;370}371372373// METHODS374375/**376* Finalizes the equals method377*/378public final boolean equals(Object obj) {379if (toString() == null) {380return (obj != null) && (obj.toString() == null);381}382if (obj instanceof Type) {383return toString().equals(obj.toString());384}385return false;386}387388/**389* Finalizes the hashCode method390*/391public final int hashCode() {392if (toString() == null) {393return 0;394}395return toString().hashCode();396}397398/**399* Provides the file type's name as the <code>String</code> representation400* of the file type.401* @return the file type's name402*/403public final String toString() {404return name;405}406407/**408* Obtains the common file name extension for this file type.409* @return file type extension410*/411public String getExtension() {412return extension;413}414415} // class Type416417} // class AudioFileFormat418419420