FileDocCategorySizeDatePackage
AsfFileReader.javaAPI DocJaudiotagger 2.0.410667Wed Mar 30 16:11:52 BST 2011org.jaudiotagger.audio.asf

AsfFileReader

public class AsfFileReader extends org.jaudiotagger.audio.generic.AudioFileReader
This reader can read ASF files containing any content (stream type).
author
Christian Laireiter

Fields Summary
private static final Logger
LOGGER
Logger instance
private static final AsfHeaderReader
HEADER_READER
This reader will be configured to read tag and audio header information.
Constructors Summary
Methods Summary
private booleandetermineVariableBitrate(org.jaudiotagger.audio.asf.data.AsfHeader header)
Determines if the "isVbr" field is set in the extended content description.

param
header the header to look up.
return
true if "isVbr" is present with a true value.


     
        final List<Class<? extends ChunkReader>> readers = new ArrayList<Class<? extends ChunkReader>>();
        readers.add(ContentDescriptionReader.class);
        readers.add(ContentBrandingReader.class);
        readers.add(MetadataReader.class);
        readers.add(LanguageListReader.class);

        // Create the header extension object reader with just content
        // description reader as well
        // as extended content description reader.
        final AsfExtHeaderReader extReader = new AsfExtHeaderReader(readers,
                true);
        readers.add(FileHeaderReader.class);
        readers.add(StreamChunkReader.class);
        HEADER_READER = new AsfHeaderReader(readers, true);
        HEADER_READER.setExtendedHeaderReader(extReader);
    
        assert header != null;
        boolean result = false;
        final MetadataContainer extDesc = header
                .findExtendedContentDescription();
        if (extDesc != null) {
            final List<MetadataDescriptor> descriptors = extDesc
                    .getDescriptorsByName("IsVBR");
            if (descriptors != null && !descriptors.isEmpty()) {
                result = Boolean.TRUE.toString().equals(
                        descriptors.get(0).getString());
            }
        }
        return result;
    
private org.jaudiotagger.audio.generic.GenericAudioHeadergetAudioHeader(org.jaudiotagger.audio.asf.data.AsfHeader header)
Creates a generic audio header instance with provided data from header.

param
header ASF header which contains the information.
return
generic audio header representation.
throws
CannotReadException If header does not contain mandatory information. (Audio stream chunk and file header chunk)

        final GenericAudioHeader info = new GenericAudioHeader();
        if (header.getFileHeader() == null) {
            throw new CannotReadException(
                    "Invalid ASF/WMA file. File header object not available.");
        }
        if (header.getAudioStreamChunk() == null) {
            throw new CannotReadException(
                    "Invalid ASF/WMA file. No audio stream contained.");
        }
        info.setBitrate(header.getAudioStreamChunk().getKbps());
        info.setChannelNumber((int) header.getAudioStreamChunk()
                .getChannelCount());
        info.setEncodingType("ASF (audio): "
                + header.getAudioStreamChunk().getCodecDescription());
        info
                .setLossless(header.getAudioStreamChunk()
                        .getCompressionFormat() == AudioStreamChunk.WMA_LOSSLESS);
        info.setPreciseLength(header.getFileHeader().getPreciseDuration());
        info.setSamplingRate((int) header.getAudioStreamChunk()
                .getSamplingRate());
        info.setVariableBitRate(determineVariableBitrate(header));
        return info;
    
protected org.jaudiotagger.audio.generic.GenericAudioHeadergetEncodingInfo(java.io.RandomAccessFile raf)
(overridden)

see
org.jaudiotagger.audio.generic.AudioFileReader#getEncodingInfo(java.io.RandomAccessFile)

        raf.seek(0);
        GenericAudioHeader info;
        try {
            final AsfHeader header = AsfHeaderReader.readInfoHeader(raf);
            if (header == null) {
                throw new CannotReadException(
                        "Some values must have been "
                                + "incorrect for interpretation as asf with wma content.");
            }
            info = getAudioHeader(header);
        } catch (final Exception e) {
            if (e instanceof IOException) {
                throw (IOException) e;
            } else if (e instanceof CannotReadException) {
                throw (CannotReadException) e;
            } else {
                throw new CannotReadException("Failed to read. Cause: "
                        + e.getMessage(), e);
            }
        }
        return info;
    
private org.jaudiotagger.tag.asf.AsfTaggetTag(org.jaudiotagger.audio.asf.data.AsfHeader header)
Creates a tag instance with provided data from header.

param
header ASF header which contains the information.
return
generic audio header representation.

        return TagConverter.createTagOf(header);
    
protected org.jaudiotagger.tag.asf.AsfTaggetTag(java.io.RandomAccessFile raf)
(overridden)

see
org.jaudiotagger.audio.generic.AudioFileReader#getTag(java.io.RandomAccessFile)

        raf.seek(0);
        AsfTag tag;
        try {
            final AsfHeader header = AsfHeaderReader.readTagHeader(raf);
            if (header == null) {
                throw new CannotReadException(
                        "Some values must have been "
                                + "incorrect for interpretation as asf with wma content.");
            }

            tag = TagConverter.createTagOf(header);

        } catch (final Exception e) {
            logger.severe(e.getMessage());
            if (e instanceof IOException) {
                throw (IOException) e;
            } else if (e instanceof CannotReadException) {
                throw (CannotReadException) e;
            } else {
                throw new CannotReadException("Failed to read. Cause: "
                        + e.getMessage());
            }
        }
        return tag;
    
public org.jaudiotagger.audio.AudioFileread(java.io.File f)
{@inheritDoc}

        if (!f.canRead()) {
            throw new CannotReadException(
                    ErrorMessage.GENERAL_READ_FAILED_DO_NOT_HAVE_PERMISSION_TO_READ_FILE
                            .getMsg(f.getAbsolutePath()));
        }
        InputStream stream = null;
        try {
            stream = new FullRequestInputStream(new BufferedInputStream(
                    new FileInputStream(f)));
            final AsfHeader header = HEADER_READER.read(Utils.readGUID(stream),
                    stream, 0);
            if (header == null) {
                throw new CannotReadException(ErrorMessage.ASF_HEADER_MISSING
                        .getMsg(f.getAbsolutePath()));
            }
            if (header.getFileHeader() == null) {
                throw new CannotReadException(
                        ErrorMessage.ASF_FILE_HEADER_MISSING.getMsg(f
                                .getAbsolutePath()));
            }

            // Just log a warning because file seems to play okay
            if (header.getFileHeader().getFileSize().longValue() != f.length()) {
                logger
                        .warning(ErrorMessage.ASF_FILE_HEADER_SIZE_DOES_NOT_MATCH_FILE_SIZE
                                .getMsg(f.getAbsolutePath(), header
                                        .getFileHeader().getFileSize()
                                        .longValue(), f.length()));
            }

            return new AudioFile(f, getAudioHeader(header), getTag(header));

        } catch (final CannotReadException e) {
            throw e;
        } catch (final Exception e) {
            throw new CannotReadException("\"" + f + "\" :" + e, e);
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (final Exception ex) {
                LOGGER.severe("\"" + f + "\" :" + ex);
            }
        }