use of org.jaudiotagger.audio.asf.data.AsfHeader in project MusicDNA by harjot-oberai.
the class AsfFileReader method read.
/**
* {@inheritDoc}
*/
@Override
public AudioFile read(final File f) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
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);
}
}
}
use of org.jaudiotagger.audio.asf.data.AsfHeader in project MusicDNA by harjot-oberai.
the class AsfFileWriter method writeTag.
/**
* {@inheritDoc}
*/
@Override
protected void writeTag(final Tag tag, final RandomAccessFile raf, final RandomAccessFile rafTemp) throws CannotWriteException, IOException {
/*
* Since this implementation should not change the structure of the ASF
* file (locations of content description chunks), we need to read the
* content description chunk and the extended content description chunk
* from the source file. In the second step we need to determine which
* modifier (asf header or asf extended header) gets the appropriate
* modifiers. The following policies are applied: if the source does not
* contain any descriptor, the necessary descriptors are appended to the
* header object.
*
* if the source contains only one descriptor in the header extension
* object, and the other type is needed as well, the other one will be
* put into the header extension object.
*
* for each descriptor type, if an object is found, an updater will be
* configured.
*/
final AsfHeader sourceHeader = AsfHeaderReader.readTagHeader(raf);
// Reset for the streamer
raf.seek(0);
/*
* Now createField modifiers for metadata descriptor and extended content
* descriptor as implied by the given Tag.
*/
// TODO not convinced that we need to copy fields here
final AsfTag copy = new AsfTag(tag, true);
final MetadataContainer[] distribution = TagConverter.distributeMetadata(copy);
final boolean[] existHeader = searchExistence(sourceHeader, distribution);
final boolean[] existExtHeader = searchExistence(sourceHeader.getExtendedHeader(), distribution);
// Modifiers for the asf header object
final List<ChunkModifier> headerModifier = new ArrayList<ChunkModifier>();
// Modifiers for the asf header extension object
final List<ChunkModifier> extHeaderModifier = new ArrayList<ChunkModifier>();
for (int i = 0; i < distribution.length; i++) {
final WriteableChunkModifer modifier = new WriteableChunkModifer(distribution[i]);
if (existHeader[i]) {
// Will remove or modify chunks in ASF header
headerModifier.add(modifier);
} else if (existExtHeader[i]) {
// Will remove or modify chunks in extended header
extHeaderModifier.add(modifier);
} else {
// Objects (chunks) will be added here.
if (i == 0 || i == 2 || i == 1) {
// Add content description and extended content description
// at header for maximum compatibility
headerModifier.add(modifier);
} else {
// For now, the rest should be created at extended header
// since other positions aren'timer known.
extHeaderModifier.add(modifier);
}
}
}
// change (performance)
if (!extHeaderModifier.isEmpty()) {
headerModifier.add(new AsfExtHeaderModifier(extHeaderModifier));
}
new AsfStreamer().createModifiedCopy(new RandomAccessFileInputstream(raf), new RandomAccessFileOutputStream(rafTemp), headerModifier);
}
use of org.jaudiotagger.audio.asf.data.AsfHeader in project MusicDNA by harjot-oberai.
the class AsfHeaderReader method readHeader.
/**
* This method extracts the full ASF-Header from the given file.<br>
* If no header could be extracted <code>null</code> is returned. <br>
*
* @param file
* the ASF file to read.<br>
* @return AsfHeader-Wrapper, or <code>null</code> if no supported ASF
* header was found.
* @throws IOException
* on I/O Errors.
*/
public static AsfHeader readHeader(final File file) throws IOException {
final InputStream stream = new FileInputStream(file);
final AsfHeader result = FULL_READER.read(Utils.readGUID(stream), stream, 0);
stream.close();
return result;
}
use of org.jaudiotagger.audio.asf.data.AsfHeader in project MusicDNA by harjot-oberai.
the class AsfFileReader method getEncodingInfo.
/**
* (overridden)
*
* @see org.jaudiotagger.audio.generic.AudioFileReader#getEncodingInfo(java.io.RandomAccessFile)
*/
@Override
protected GenericAudioHeader getEncodingInfo(final RandomAccessFile raf) throws CannotReadException, IOException {
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;
}
use of org.jaudiotagger.audio.asf.data.AsfHeader in project MusicDNA by harjot-oberai.
the class AsfFileReader method getTag.
/**
* (overridden)
*
* @see org.jaudiotagger.audio.generic.AudioFileReader#getTag(java.io.RandomAccessFile)
*/
@Override
protected AsfTag getTag(final RandomAccessFile raf) throws CannotReadException, IOException {
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;
}
Aggregations