| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AsfHeaderReader |
|
| 0.0;0 |
| 1 | /* | |
| 2 | * Entagged Audio Tag library | |
| 3 | * Copyright (c) 2004-2005 Christian Laireiter <liree@web.de> | |
| 4 | * | |
| 5 | * This library is free software; you can redistribute it and/or | |
| 6 | * modify it under the terms of the GNU Lesser General Public | |
| 7 | * License as published by the Free Software Foundation; either | |
| 8 | * version 2.1 of the License, or (at your option) any later version. | |
| 9 | * | |
| 10 | * This library is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | * Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU Lesser General Public | |
| 16 | * License along with this library; if not, write to the Free Software | |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
| 18 | */ | |
| 19 | package org.jaudiotagger.audio.asf.io; | |
| 20 | ||
| 21 | import org.jaudiotagger.audio.asf.data.AsfHeader; | |
| 22 | import org.jaudiotagger.audio.asf.data.GUID; | |
| 23 | import org.jaudiotagger.audio.asf.util.Utils; | |
| 24 | ||
| 25 | import java.io.*; | |
| 26 | import java.math.BigInteger; | |
| 27 | import java.util.ArrayList; | |
| 28 | import java.util.List; | |
| 29 | ||
| 30 | /** | |
| 31 | * This <i>class </i> reads an ASF header out of an input stream an creates an | |
| 32 | * {@link org.jaudiotagger.audio.asf.data.AsfHeader} object if successful. <br> | |
| 33 | * For now only ASF ver 1.0 is supported, because ver 2.0 seems not to be used | |
| 34 | * anywhere. <br> | |
| 35 | * ASF headers contains other chunks. As of this other readers of current | |
| 36 | * <b>package </b> are called from within. | |
| 37 | * | |
| 38 | * @author Christian Laireiter | |
| 39 | */ | |
| 40 | 80 | public class AsfHeaderReader extends ChunkContainerReader<AsfHeader> |
| 41 | { | |
| 42 | ||
| 43 | /** | |
| 44 | * ASF reader configured to extract all information. | |
| 45 | */ | |
| 46 | private final static AsfHeaderReader FULL_READER; | |
| 47 | ||
| 48 | /** | |
| 49 | * ASF reader configured to just extract information about audio streams.<br> | |
| 50 | * If the ASF file only contains one audio stream it works fine.<br> | |
| 51 | */ | |
| 52 | private final static AsfHeaderReader INFO_READER; | |
| 53 | ||
| 54 | /** | |
| 55 | * ASF reader configured to just extract metadata information.<br> | |
| 56 | */ | |
| 57 | private final static AsfHeaderReader TAG_READER; | |
| 58 | ||
| 59 | static | |
| 60 | { | |
| 61 | 42 | List<Class<? extends ChunkReader>> readers = new ArrayList<Class<? extends ChunkReader>>(); |
| 62 | 42 | readers.add(FileHeaderReader.class); |
| 63 | 42 | readers.add(StreamChunkReader.class); |
| 64 | 42 | INFO_READER = new AsfHeaderReader(readers, true); |
| 65 | 42 | readers.clear(); |
| 66 | 42 | readers.add(ContentDescriptionReader.class); |
| 67 | 42 | readers.add(ExtContentDescReader.class); |
| 68 | /* | |
| 69 | * Create the header extension object readers with just content description reader as well | |
| 70 | * as extended content description reader. | |
| 71 | */ | |
| 72 | 42 | AsfExtHeaderReader extReader = new AsfExtHeaderReader(readers, true); |
| 73 | 42 | AsfExtHeaderReader extReader2 = new AsfExtHeaderReader(readers, true); |
| 74 | 42 | TAG_READER = new AsfHeaderReader(readers, true); |
| 75 | 42 | TAG_READER.setExtendedHeaderReader(extReader); |
| 76 | 42 | readers.add(FileHeaderReader.class); |
| 77 | 42 | readers.add(StreamChunkReader.class); |
| 78 | 42 | readers.add(EncodingChunkReader.class); |
| 79 | 42 | readers.add(EncryptionChunkReader.class); |
| 80 | 42 | readers.add(StreamBitratePropertiesReader.class); |
| 81 | 42 | FULL_READER = new AsfHeaderReader(readers, false); |
| 82 | 42 | FULL_READER.setExtendedHeaderReader(extReader2); |
| 83 | 42 | } |
| 84 | ||
| 85 | /** | |
| 86 | * Creates a Stream that will read from the specified | |
| 87 | * {@link RandomAccessFile};<br> | |
| 88 | * | |
| 89 | * @param raf | |
| 90 | * data source to read from. | |
| 91 | * @return a stream which accesses the source. | |
| 92 | */ | |
| 93 | private static InputStream createStream(RandomAccessFile raf) | |
| 94 | { | |
| 95 | 25 | return new FullRequestInputStream(new BufferedInputStream(new RandomAccessFileInputstream(raf))); |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * This method extracts the full ASF-Header from the given file.<br> | |
| 100 | * If no header could be extracted <code>null</code> is returned. <br> | |
| 101 | * | |
| 102 | * @param file the ASF file to read.<br> | |
| 103 | * @return AsfHeader-Wrapper, or <code>null</code> if no supported ASF | |
| 104 | * header was found. | |
| 105 | * @throws IOException on I/O Errors. | |
| 106 | */ | |
| 107 | public static AsfHeader readHeader(File file) throws IOException | |
| 108 | { | |
| 109 | 16 | AsfHeader result = null; |
| 110 | 16 | InputStream stream = new FileInputStream(file); |
| 111 | 16 | result = FULL_READER.read(Utils.readGUID(stream), stream, 0); |
| 112 | 16 | stream.close(); |
| 113 | 16 | return result; |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * This method tries to extract a full ASF-header out of the given stream. <br> | |
| 118 | * If no header could be extracted <code>null</code> is returned. <br> | |
| 119 | * | |
| 120 | * @param in | |
| 121 | * File which contains the ASF header. | |
| 122 | * @return AsfHeader-Wrapper, or <code>null</code> if no supported Asf | |
| 123 | * header was found. | |
| 124 | * @throws IOException | |
| 125 | * Read errors | |
| 126 | */ | |
| 127 | public static AsfHeader readHeader(RandomAccessFile in) throws IOException | |
| 128 | { | |
| 129 | 0 | final InputStream stream = createStream(in); |
| 130 | 0 | return FULL_READER.read(Utils.readGUID(stream), stream, 0); |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * This method tries to extract an ASF-header out of the given stream, which | |
| 135 | * only contains information about the audio stream.<br> | |
| 136 | * If no header could be extracted <code>null</code> is returned. <br> | |
| 137 | * | |
| 138 | * @param in | |
| 139 | * File which contains the ASF header. | |
| 140 | * @return AsfHeader-Wrapper, or <code>null</code> if no supported Asf | |
| 141 | * header was found. | |
| 142 | * @throws IOException | |
| 143 | * Read errors | |
| 144 | */ | |
| 145 | public static AsfHeader readInfoHeader(RandomAccessFile in) throws IOException | |
| 146 | { | |
| 147 | 0 | final InputStream stream = createStream(in); |
| 148 | 0 | return INFO_READER.read(Utils.readGUID(stream), stream, 0); |
| 149 | } | |
| 150 | ||
| 151 | /** | |
| 152 | * This method tries to extract an ASF-header out of the given stream, which | |
| 153 | * only contains metadata.<br> | |
| 154 | * If no header could be extracted <code>null</code> is returned. <br> | |
| 155 | * | |
| 156 | * @param in | |
| 157 | * File which contains the ASF header. | |
| 158 | * @return AsfHeader-Wrapper, or <code>null</code> if no supported Asf | |
| 159 | * header was found. | |
| 160 | * @throws IOException | |
| 161 | * Read errors | |
| 162 | */ | |
| 163 | public static AsfHeader readTagHeader(RandomAccessFile in) throws IOException | |
| 164 | { | |
| 165 | 25 | final InputStream stream = createStream(in); |
| 166 | 25 | return TAG_READER.read(Utils.readGUID(stream), stream, 0); |
| 167 | } | |
| 168 | ||
| 169 | /** | |
| 170 | * Creates an instance of this reader. | |
| 171 | * | |
| 172 | * @param toRegister The chunk readers to utilize. | |
| 173 | * | |
| 174 | * @param readChunkOnce if <code>true</code>, each chunk type (identified by chunk | |
| 175 | * GUID) will handled only once, if a reader is available, other | |
| 176 | * chunks will be discarded. | |
| 177 | */ | |
| 178 | public AsfHeaderReader(List<Class<? extends ChunkReader>> toRegister, boolean readChunkOnce) | |
| 179 | { | |
| 180 | 168 | super(toRegister, readChunkOnce); |
| 181 | 168 | } |
| 182 | ||
| 183 | /** | |
| 184 | * {@inheritDoc} | |
| 185 | */ | |
| 186 | @Override | |
| 187 | protected AsfHeader createContainer(long streamPosition, BigInteger chunkLength, InputStream stream) throws IOException | |
| 188 | { | |
| 189 | 80 | long chunkCount = Utils.readUINT32(stream); |
| 190 | /* | |
| 191 | * 2 reserved bytes. first should be equal to 0x01 and second 0x02. ASF specification | |
| 192 | * suggests to not read the content if second byte is not 0x02. | |
| 193 | */ | |
| 194 | 80 | if (stream.read() != 1) |
| 195 | { | |
| 196 | 0 | throw new IOException("No ASF"); //$NON-NLS-1$ |
| 197 | } | |
| 198 | 80 | if (stream.read() != 2) |
| 199 | { | |
| 200 | 0 | throw new IOException("No ASF"); //$NON-NLS-1$ |
| 201 | } | |
| 202 | /* | |
| 203 | * Creating the resulting object | |
| 204 | */ | |
| 205 | 80 | return new AsfHeader(streamPosition, chunkLength, chunkCount); |
| 206 | } | |
| 207 | ||
| 208 | /** | |
| 209 | * {@inheritDoc} | |
| 210 | */ | |
| 211 | public GUID getApplyingId() | |
| 212 | { | |
| 213 | 80 | return GUID.GUID_HEADER; |
| 214 | } | |
| 215 | ||
| 216 | /** | |
| 217 | * Sets the {@link AsfExtHeaderReader}, which is to be used, when an header extension object | |
| 218 | * is found. | |
| 219 | * | |
| 220 | * @param extReader header extension object reader. | |
| 221 | */ | |
| 222 | public void setExtendedHeaderReader(final AsfExtHeaderReader extReader) | |
| 223 | { | |
| 224 | 126 | if (extReader != null) |
| 225 | { | |
| 226 | 126 | this.readerMap.put(extReader.getApplyingId(), extReader); |
| 227 | } | |
| 228 | else | |
| 229 | { | |
| 230 | 0 | throw new NullPointerException("Argument must not be null."); |
| 231 | } | |
| 232 | 126 | } |
| 233 | ||
| 234 | ||
| 235 | } |