code
stringlengths
11
173k
docstring
stringlengths
2
593k
func_name
stringlengths
2
189
language
stringclasses
1 value
repo
stringclasses
844 values
path
stringlengths
11
294
url
stringlengths
60
339
license
stringclasses
4 values
public GUID[] getApplyingIds() { return APPLYING.clone(); }
{@inheritDoc}
ContentDescriptionReader::getApplyingIds
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ContentDescriptionReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ContentDescriptionReader.java
Apache-2.0
private int[] getStringSizes(final InputStream stream) throws IOException { final int[] result = new int[5]; for (int i = 0; i < result.length; i++) { result[i] = Utils.readUINT16(stream); } return result; }
Returns the next 5 UINT16 values as an array.<br> @param stream stream to read from @return 5 int values read from stream. @throws IOException on I/O Errors.
ContentDescriptionReader::getStringSizes
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ContentDescriptionReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ContentDescriptionReader.java
Apache-2.0
public Chunk read(final GUID guid, final InputStream stream, final long chunkStart) throws IOException { final BigInteger chunkSize = Utils.readBig64(stream); /* * Now comes 16-Bit values representing the length of the Strings which * follows. */ final int[] stringSizes = getStringSizes(stream); /* * Now we know the String length of each occuring String. */ final String[] strings = new String[stringSizes.length]; for (int i = 0; i < strings.length; i++) { if (stringSizes[i] > 0) { strings[i] = Utils.readFixedSizeUTF16Str(stream, stringSizes[i]); } } /* * Now create the result */ final ContentDescription result = new ContentDescription(chunkStart, chunkSize); if (stringSizes[0] > 0) { result.setTitle(strings[0]); } if (stringSizes[1] > 0) { result.setAuthor(strings[1]); } if (stringSizes[2] > 0) { result.setCopyright(strings[2]); } if (stringSizes[3] > 0) { result.setComment(strings[3]); } if (stringSizes[4] > 0) { result.setRating(strings[4]); } return result; }
{@inheritDoc}
ContentDescriptionReader::read
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ContentDescriptionReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ContentDescriptionReader.java
Apache-2.0
public static ChunkHeaderReader getInstance() { return INSTANCE; }
Returns an instance of the reader. @return instance.
ChunkHeaderReader::getInstance
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkHeaderReader.java
Apache-2.0
private ChunkHeaderReader() { // Hidden }
Hidden Utility class constructor.
ChunkHeaderReader::ChunkHeaderReader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkHeaderReader.java
Apache-2.0
public boolean canFail() { return false; }
{@inheritDoc}
ChunkHeaderReader::canFail
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkHeaderReader.java
Apache-2.0
public GUID[] getApplyingIds() { return APPLYING.clone(); }
{@inheritDoc}
ChunkHeaderReader::getApplyingIds
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkHeaderReader.java
Apache-2.0
public Chunk read(final GUID guid, final InputStream stream, final long chunkStart) throws IOException { final BigInteger chunkLen = Utils.readBig64(stream); stream.skip(chunkLen.longValue() - 24); return new Chunk(guid, chunkStart, chunkLen); }
{@inheritDoc}
ChunkHeaderReader::read
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkHeaderReader.java
Apache-2.0
public RandomAccessFileInputstream(final RandomAccessFile file) { super(); if (file == null) { throw new IllegalArgumentException("null"); } this.source = file; }
Creates an instance that will provide {@link InputStream} functionality on the given {@link RandomAccessFile} by delegating calls.<br> @param file The file to read.
RandomAccessFileInputstream::RandomAccessFileInputstream
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/RandomAccessFileInputstream.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/RandomAccessFileInputstream.java
Apache-2.0
public boolean canFail() { return false; }
{@inheritDoc}
MetadataReader::canFail
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/MetadataReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/MetadataReader.java
Apache-2.0
public GUID[] getApplyingIds() { return APPLYING.clone(); }
{@inheritDoc}
MetadataReader::getApplyingIds
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/MetadataReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/MetadataReader.java
Apache-2.0
public Chunk read(final GUID guid, final InputStream stream, final long streamPosition) throws IOException { final BigInteger chunkLen = Utils.readBig64(stream); final MetadataContainer result = new MetadataContainer(guid, streamPosition, chunkLen); // isExtDesc will be set to true, if a extended content description // chunk is read // otherwise it is a metadata object, there are only slight differences final boolean isExtDesc = result.getContainerType() == ContainerType.EXTENDED_CONTENT; final int recordCount = Utils.readUINT16(stream); for (int i = 0; i < recordCount; i++) { int languageIndex = 0; int streamNumber = 0; if (!isExtDesc) { /* * Metadata objects have a language index and a stream number */ languageIndex = Utils.readUINT16(stream); assert languageIndex >= 0 && languageIndex < MetadataDescriptor.MAX_LANG_INDEX; assert result.getContainerType() == ContainerType.METADATA_LIBRARY_OBJECT || languageIndex == 0; streamNumber = Utils.readUINT16(stream); assert streamNumber >= 0 && streamNumber <= MetadataDescriptor.MAX_STREAM_NUMBER; } final int nameLen = Utils.readUINT16(stream); String recordName = null; if (isExtDesc) { recordName = Utils.readFixedSizeUTF16Str(stream, nameLen); } final int dataType = Utils.readUINT16(stream); assert dataType >= 0 && dataType <= 6; final long dataLen = isExtDesc ? Utils.readUINT16(stream) : Utils.readUINT32(stream); assert dataLen >= 0; assert result.getContainerType() == ContainerType.METADATA_LIBRARY_OBJECT || dataLen <= MetadataDescriptor.DWORD_MAXVALUE; if (!isExtDesc) { recordName = Utils.readFixedSizeUTF16Str(stream, nameLen); } final MetadataDescriptor descriptor = new MetadataDescriptor(result.getContainerType(), recordName, dataType, streamNumber, languageIndex ); switch (dataType) { case MetadataDescriptor.TYPE_STRING: descriptor.setStringValue(Utils.readFixedSizeUTF16Str(stream, (int) dataLen)); break; case MetadataDescriptor.TYPE_BINARY: descriptor.setBinaryValue(Utils.readBinary(stream, dataLen)); break; case MetadataDescriptor.TYPE_BOOLEAN: assert isExtDesc && dataLen == 4 || !isExtDesc && dataLen == 2; descriptor.setBooleanValue(readBoolean(stream, (int) dataLen)); break; case MetadataDescriptor.TYPE_DWORD: assert dataLen == 4; descriptor.setDWordValue(Utils.readUINT32(stream)); break; case MetadataDescriptor.TYPE_WORD: assert dataLen == 2; descriptor.setWordValue(Utils.readUINT16(stream)); break; case MetadataDescriptor.TYPE_QWORD: assert dataLen == 8; descriptor.setQWordValue(Utils.readUINT64(stream)); break; case MetadataDescriptor.TYPE_GUID: assert dataLen == GUID.GUID_LENGTH; descriptor.setGUIDValue(Utils.readGUID(stream)); break; default: // Unknown, hopefully the convention for the size of the // value // is given, so we could read it binary descriptor.setStringValue("Invalid datatype: " + new String(Utils.readBinary(stream, dataLen))); } result.addDescriptor(descriptor); } return result; }
{@inheritDoc}
MetadataReader::read
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/MetadataReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/MetadataReader.java
Apache-2.0
private boolean readBoolean(final InputStream stream, final int bytes) throws IOException { final byte[] tmp = new byte[bytes]; stream.read(tmp); boolean result = false; for (int i = 0; i < bytes; i++) { if (i == bytes - 1) { result = tmp[i] == 1; assert tmp[i] == 0 || tmp[i] == 1; } else { assert tmp[i] == 0; } } return result; }
Reads the given amount of bytes and checks the last byte, if its equal to one or zero (true / false).<br> All other bytes must be zero. (if assertions enabled). @param stream stream to read from. @param bytes amount of bytes @return <code>true</code> or <code>false</code>. @throws IOException on I/O Errors
MetadataReader::readBoolean
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/MetadataReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/MetadataReader.java
Apache-2.0
private static InputStream createStream(final RandomAccessFile raf) { return new FullRequestInputStream(new BufferedInputStream(new RandomAccessFileInputstream(raf))); }
Creates a Stream that will read from the specified {@link RandomAccessFile};<br> @param raf data source to read from. @return a stream which accesses the source.
AsfHeaderReader::createStream
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
Apache-2.0
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; }
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.
AsfHeaderReader::readHeader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
Apache-2.0
public static AsfHeader readHeader(final RandomAccessFile file) throws IOException { final InputStream stream = createStream(file); return FULL_READER.read(Utils.readGUID(stream), stream, 0); }
This method tries to extract a full ASF-header out of the given stream. <br> If no header could be extracted <code>null</code> is returned. <br> @param file File which contains the ASF header. @return AsfHeader-Wrapper, or <code>null</code> if no supported ASF header was found. @throws IOException Read errors
AsfHeaderReader::readHeader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
Apache-2.0
public static AsfHeader readInfoHeader(final RandomAccessFile file) throws IOException { final InputStream stream = createStream(file); return INFO_READER.read(Utils.readGUID(stream), stream, 0); }
This method tries to extract an ASF-header out of the given stream, which only contains information about the audio stream.<br> If no header could be extracted <code>null</code> is returned. <br> @param file File which contains the ASF header. @return AsfHeader-Wrapper, or <code>null</code> if no supported ASF header was found. @throws IOException Read errors
AsfHeaderReader::readInfoHeader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
Apache-2.0
public static AsfHeader readTagHeader(final RandomAccessFile file) throws IOException { final InputStream stream = createStream(file); return TAG_READER.read(Utils.readGUID(stream), stream, 0); }
This method tries to extract an ASF-header out of the given stream, which only contains metadata.<br> If no header could be extracted <code>null</code> is returned. <br> @param file File which contains the ASF header. @return AsfHeader-Wrapper, or <code>null</code> if no supported ASF header was found. @throws IOException Read errors
AsfHeaderReader::readTagHeader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
Apache-2.0
public AsfHeaderReader(final List<Class<? extends ChunkReader>> toRegister, final boolean readChunkOnce) { super(toRegister, readChunkOnce); }
Creates an instance of this reader. @param toRegister The chunk readers to utilize. @param readChunkOnce if <code>true</code>, each chunk type (identified by chunk GUID) will handled only once, if a reader is available, other chunks will be discarded.
AsfHeaderReader::AsfHeaderReader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
Apache-2.0
public boolean canFail() { return false; }
{@inheritDoc}
AsfHeaderReader::canFail
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
Apache-2.0
public GUID[] getApplyingIds() { return APPLYING.clone(); }
{@inheritDoc}
AsfHeaderReader::getApplyingIds
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
Apache-2.0
public void setExtendedHeaderReader(final AsfExtHeaderReader extReader) { for (final GUID curr : extReader.getApplyingIds()) { this.readerMap.put(curr, extReader); } }
Sets the {@link AsfExtHeaderReader}, which is to be used, when an header extension object is found. @param extReader header extension object reader.
AsfHeaderReader::setExtendedHeaderReader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfHeaderReader.java
Apache-2.0
protected StreamBitratePropertiesReader() { // NOTHING toDo }
Should not be used for now.
StreamBitratePropertiesReader::StreamBitratePropertiesReader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/StreamBitratePropertiesReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/StreamBitratePropertiesReader.java
Apache-2.0
public boolean canFail() { return false; }
{@inheritDoc}
StreamBitratePropertiesReader::canFail
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/StreamBitratePropertiesReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/StreamBitratePropertiesReader.java
Apache-2.0
public GUID[] getApplyingIds() { return APPLYING.clone(); }
{@inheritDoc}
StreamBitratePropertiesReader::getApplyingIds
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/StreamBitratePropertiesReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/StreamBitratePropertiesReader.java
Apache-2.0
public Chunk read(final GUID guid, final InputStream stream, final long chunkStart) throws IOException { final BigInteger chunkLen = Utils.readBig64(stream); final StreamBitratePropertiesChunk result = new StreamBitratePropertiesChunk(chunkLen); /* * Read the amount of bitrate records */ final long recordCount = Utils.readUINT16(stream); for (int i = 0; i < recordCount; i++) { final int flags = Utils.readUINT16(stream); final long avgBitrate = Utils.readUINT32(stream); result.addBitrateRecord(flags & 0x00FF, avgBitrate); } result.setPosition(chunkStart); return result; }
{@inheritDoc}
StreamBitratePropertiesReader::read
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/StreamBitratePropertiesReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/StreamBitratePropertiesReader.java
Apache-2.0
public CountingOutputstream(final OutputStream outputStream) { super(); assert outputStream != null; this.wrapped = outputStream; }
Creates an instance which will delegate the write calls to the given output stream. @param outputStream stream to wrap.
CountingOutputstream::CountingOutputstream
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/CountingOutputstream.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/CountingOutputstream.java
Apache-2.0
public long getCount() { return this.count; }
@return the count
CountingOutputstream::getCount
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/CountingOutputstream.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/CountingOutputstream.java
Apache-2.0
public AsfExtHeaderModifier(final List<ChunkModifier> modifiers) { assert modifiers != null; this.modifierList = new ArrayList<ChunkModifier>(modifiers); }
Creates an instance.<br> @param modifiers modifiers to apply.
AsfExtHeaderModifier::AsfExtHeaderModifier
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfExtHeaderModifier.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfExtHeaderModifier.java
Apache-2.0
private void copyChunk(final GUID guid, final InputStream source, final OutputStream destination) throws IOException { final long chunkSize = Utils.readUINT64(source); destination.write(guid.getBytes()); Utils.writeUINT64(chunkSize, destination); Utils.copy(source, destination, chunkSize - 24); }
Simply copies a chunk from <code>source</code> to <code>destination</code>.<br> The method assumes, that the GUID has already been read and will write the provided one to the destination.<br> The chunk length however will be read and used to determine the amount of bytes to copy. @param guid GUID of the current CHUNK. @param source source of an ASF chunk, which is to be located at the chunk length field. @param destination the destination to copy the chunk to. @throws IOException on I/O errors.
AsfExtHeaderModifier::copyChunk
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfExtHeaderModifier.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfExtHeaderModifier.java
Apache-2.0
public boolean isApplicable(final GUID guid) { return GUID.GUID_HEADER_EXTENSION.equals(guid); }
{@inheritDoc}
AsfExtHeaderModifier::isApplicable
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfExtHeaderModifier.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfExtHeaderModifier.java
Apache-2.0
public ModificationResult modify(final GUID guid, final InputStream source, final OutputStream destination) throws IOException { assert GUID.GUID_HEADER_EXTENSION.equals(guid); long difference = 0; final List<ChunkModifier> modders = new ArrayList<ChunkModifier>(this.modifierList); final Set<GUID> occuredGuids = new HashSet<GUID>(); occuredGuids.add(guid); final BigInteger chunkLen = Utils.readBig64(source); final GUID reserved1 = Utils.readGUID(source); final int reserved2 = Utils.readUINT16(source); final long dataSize = Utils.readUINT32(source); assert dataSize == 0 || dataSize >= 24; assert chunkLen.subtract(BigInteger.valueOf(46)).longValue() == dataSize; /* * Stream buffer for the chunk list */ final ByteArrayOutputStream bos = new ByteArrayOutputStream(); /* * Stream which counts read bytes. Dirty but quick way of implementing * this. */ final CountingInputStream cis = new CountingInputStream(source); while (cis.getReadCount() < dataSize) { // read GUID final GUID curr = Utils.readGUID(cis); boolean handled = false; for (int i = 0; i < modders.size() && !handled; i++) { if (modders.get(i).isApplicable(curr)) { final ModificationResult modRes = modders.get(i).modify(curr, cis, bos); difference += modRes.getByteDifference(); occuredGuids.addAll(modRes.getOccuredGUIDs()); modders.remove(i); handled = true; } } if (!handled) { occuredGuids.add(curr); copyChunk(curr, cis, bos); } } // Now apply the left modifiers. for (final ChunkModifier curr : modders) { // chunks, which were not in the source file, will be added to the // destination final ModificationResult result = curr.modify(null, null, bos); difference += result.getByteDifference(); occuredGuids.addAll(result.getOccuredGUIDs()); } destination.write(GUID.GUID_HEADER_EXTENSION.getBytes()); Utils.writeUINT64(chunkLen.add(BigInteger.valueOf(difference)).longValue(), destination); destination.write(reserved1.getBytes()); Utils.writeUINT16(reserved2, destination); Utils.writeUINT32(dataSize + difference, destination); destination.write(bos.toByteArray()); return new ModificationResult(0, difference, occuredGuids); }
{@inheritDoc}
AsfExtHeaderModifier::modify
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfExtHeaderModifier.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfExtHeaderModifier.java
Apache-2.0
private void copyChunk(final GUID guid, final InputStream source, final OutputStream destination) throws IOException { final long chunkSize = Utils.readUINT64(source); destination.write(guid.getBytes()); Utils.writeUINT64(chunkSize, destination); Utils.copy(source, destination, chunkSize - 24); }
Simply copies a chunk from <code>source</code> to <code>destination</code>.<br> The method assumes, that the GUID has already been read and will write the provided one to the destination.<br> The chunk length however will be read and used to determine the amount of bytes to copy. @param guid GUID of the current chunk. @param source source of an ASF chunk, which is to be located at the chunk length field. @param destination the destination to copy the chunk to. @throws IOException on I/O errors.
AsfStreamer::copyChunk
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfStreamer.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfStreamer.java
Apache-2.0
public void createModifiedCopy(final InputStream source, final OutputStream dest, final List<ChunkModifier> modifiers) throws IOException { final List<ChunkModifier> modders = new ArrayList<ChunkModifier>(); if (modifiers != null) { modders.addAll(modifiers); } // Read and check ASF GUID final GUID readGUID = Utils.readGUID(source); if (GUID.GUID_HEADER.equals(readGUID)) { // used to calculate differences long totalDiff = 0; long chunkDiff = 0; // read header information final long headerSize = Utils.readUINT64(source); final long chunkCount = Utils.readUINT32(source); final byte[] reserved = new byte[2]; reserved[0] = (byte) (source.read() & 0xFF); reserved[1] = (byte) (source.read() & 0xFF); /* * bos will get all unmodified and modified header chunks. This is * necessary, because the header chunk (and file properties chunk) * need to be adjusted but are written in front of the others. */ final ByteArrayOutputStream bos = new ByteArrayOutputStream(); // fileHeader will get the binary representation of the file // properties chunk, without GUID byte[] fileHeader = null; // Iterate through all chunks for (long i = 0; i < chunkCount; i++) { // Read GUID final GUID curr = Utils.readGUID(source); // special case for file properties chunk if (GUID.GUID_FILE.equals(curr)) { final ByteArrayOutputStream tmp = new ByteArrayOutputStream(); final long size = Utils.readUINT64(source); Utils.writeUINT64(size, tmp); Utils.copy(source, tmp, size - 24); fileHeader = tmp.toByteArray(); } else { /* * Now look for ChunkModifier objects which modify the * current chunk */ boolean handled = false; for (int j = 0; j < modders.size() && !handled; j++) { if (modders.get(j).isApplicable(curr)) { // alter current chunk final ModificationResult result = modders.get(j).modify(curr, source, bos); // remember size differences. chunkDiff += result.getChunkCountDifference(); totalDiff += result.getByteDifference(); // remove current modifier from index. modders.remove(j); handled = true; } } if (!handled) { // copy chunks which are not modified. copyChunk(curr, source, bos); } } } // Now apply the left modifiers. for (final ChunkModifier curr : modders) { // chunks, which were not in the source file, will be added to // the destination final ModificationResult result = curr.modify(null, null, bos); chunkDiff += result.getChunkCountDifference(); totalDiff += result.getByteDifference(); } /* * Now all header objects have been read or manipulated and stored * in the internal buffer (bos). */ // write ASF GUID dest.write(readGUID.getBytes()); // write altered header object size Utils.writeUINT64(headerSize + totalDiff, dest); // write altered number of chunks Utils.writeUINT32(chunkCount + chunkDiff, dest); // write the reserved 2 bytes (0x01,0x02). dest.write(reserved); // write the new file header modifyFileHeader(new ByteArrayInputStream(fileHeader), dest, totalDiff); // write the header objects (chunks) dest.write(bos.toByteArray()); // copy the rest of the file (data and index) Utils.flush(source, dest); } else { throw new IllegalArgumentException("No ASF header object."); } }
Reads the <code>source</code> and applies the modifications provided by the given <code>modifiers</code>, and puts it to <code>dest</code>.<br> Each {@linkplain ChunkModifier modifier} is used only once, so if one should be used multiple times, it should be added multiple times into the list.<br> @param source the source ASF file @param dest the destination to write the modified version to. @param modifiers list of chunk modifiers to apply. @throws IOException on I/O errors.
AsfStreamer::createModifiedCopy
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfStreamer.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfStreamer.java
Apache-2.0
private void modifyFileHeader(final InputStream source, final OutputStream destination, final long fileSizeDiff) throws IOException { destination.write(GUID.GUID_FILE.getBytes()); final long chunkSize = Utils.readUINT64(source); Utils.writeUINT64(chunkSize, destination); destination.write(Utils.readGUID(source).getBytes()); final long fileSize = Utils.readUINT64(source); Utils.writeUINT64(fileSize + fileSizeDiff, destination); Utils.copy(source, destination, chunkSize - 48); }
This is a slight variation of {@link #copyChunk(GUID, InputStream, OutputStream)}, it only handles file property chunks correctly.<br> The copied chunk will have the file size field modified by the given <code>fileSizeDiff</code> value. @param source source of file properties chunk, located at its chunk length field. @param destination the destination to copy the chunk to. @param fileSizeDiff the difference which should be applied. (negative values would subtract the stored file size) @throws IOException on I/O errors.
AsfStreamer::modifyFileHeader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/AsfStreamer.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/AsfStreamer.java
Apache-2.0
public ModificationResult(final int chunkCountDiff, final long bytesDiffer, final GUID... occurred) { assert occurred != null && occurred.length > 0; this.chunkDifference = chunkCountDiff; this.byteDifference = bytesDiffer; this.occuredGUIDs.addAll(Arrays.asList(occurred)); }
Creates an instance.<br> @param chunkCountDiff amount of chunks appeared, disappeared @param bytesDiffer amount of bytes added or removed. @param occurred all GUIDs which have been occurred, during processing
ModificationResult::ModificationResult
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ModificationResult.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ModificationResult.java
Apache-2.0
public ModificationResult(final int chunkCountDiff, final long bytesDiffer, final Set<GUID> occurred) { this.chunkDifference = chunkCountDiff; this.byteDifference = bytesDiffer; this.occuredGUIDs.addAll(occurred); }
Creates an instance.<br> @param chunkCountDiff amount of chunks appeared, disappeared @param bytesDiffer amount of bytes added or removed. @param occurred all GUIDs which have been occurred, during processing
ModificationResult::ModificationResult
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ModificationResult.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ModificationResult.java
Apache-2.0
public long getByteDifference() { return this.byteDifference; }
Returns the difference of bytes. @return the byte difference
ModificationResult::getByteDifference
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ModificationResult.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ModificationResult.java
Apache-2.0
public int getChunkCountDifference() { return this.chunkDifference; }
Returns the difference of the amount of chunks. @return the chunk count difference
ModificationResult::getChunkCountDifference
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ModificationResult.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ModificationResult.java
Apache-2.0
public Set<GUID> getOccuredGUIDs() { return new HashSet<GUID>(this.occuredGUIDs); }
Returns all GUIDs which have been occurred during processing. @return see description.s
ModificationResult::getOccuredGUIDs
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ModificationResult.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ModificationResult.java
Apache-2.0
public ChunkRemover(final GUID... guids) { this.toRemove = new HashSet<GUID>(); for (final GUID current : guids) { this.toRemove.add(current); } }
Creates an instance, for removing selected chunks.<br> @param guids the GUIDs which are about to be removed by this modifier.
ChunkRemover::ChunkRemover
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkRemover.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkRemover.java
Apache-2.0
public boolean isApplicable(final GUID guid) { return this.toRemove.contains(guid); }
{@inheritDoc}
ChunkRemover::isApplicable
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkRemover.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkRemover.java
Apache-2.0
public ModificationResult modify(final GUID guid, final InputStream source, final OutputStream destination) throws IOException { ModificationResult result; if (guid == null) { // Now a chunk should be added, however, this implementation is for // removal. result = new ModificationResult(0, 0); } else { assert isApplicable(guid); // skip the chunk length minus 24 bytes for the already read length // and the guid. final long chunkLen = Utils.readUINT64(source); source.skip(chunkLen - 24); result = new ModificationResult(-1, -1 * chunkLen, guid); } return result; }
{@inheritDoc}
ChunkRemover::modify
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkRemover.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ChunkRemover.java
Apache-2.0
protected FileHeaderReader() { // NOTHING toDo }
Should not be used for now.
FileHeaderReader::FileHeaderReader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/FileHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/FileHeaderReader.java
Apache-2.0
public boolean canFail() { return false; }
{@inheritDoc}
FileHeaderReader::canFail
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/FileHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/FileHeaderReader.java
Apache-2.0
public GUID[] getApplyingIds() { return APPLYING.clone(); }
{@inheritDoc}
FileHeaderReader::getApplyingIds
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/FileHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/FileHeaderReader.java
Apache-2.0
public Chunk read(final GUID guid, final InputStream stream, final long chunkStart) throws IOException { final BigInteger chunkLen = Utils.readBig64(stream); // Skip client GUID. stream.skip(16); final BigInteger fileSize = Utils.readBig64(stream); // fileTime in 100 ns since midnight of 1st january 1601 GMT final BigInteger fileTime = Utils.readBig64(stream); final BigInteger packageCount = Utils.readBig64(stream); final BigInteger timeEndPos = Utils.readBig64(stream); final BigInteger duration = Utils.readBig64(stream); final BigInteger timeStartPos = Utils.readBig64(stream); final long flags = Utils.readUINT32(stream); final long minPkgSize = Utils.readUINT32(stream); final long maxPkgSize = Utils.readUINT32(stream); final long uncompressedFrameSize = Utils.readUINT32(stream); final FileHeader result = new FileHeader(chunkLen, fileSize, fileTime, packageCount, duration, timeStartPos, timeEndPos, flags, minPkgSize, maxPkgSize, uncompressedFrameSize); result.setPosition(chunkStart); return result; }
{@inheritDoc}
FileHeaderReader::read
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/FileHeaderReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/FileHeaderReader.java
Apache-2.0
protected ContentBrandingReader() { // NOTHING toDo }
Should not be used for now.
ContentBrandingReader::ContentBrandingReader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ContentBrandingReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ContentBrandingReader.java
Apache-2.0
public boolean canFail() { return false; }
{@inheritDoc}
ContentBrandingReader::canFail
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ContentBrandingReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ContentBrandingReader.java
Apache-2.0
public GUID[] getApplyingIds() { return APPLYING.clone(); }
{@inheritDoc}
ContentBrandingReader::getApplyingIds
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ContentBrandingReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ContentBrandingReader.java
Apache-2.0
public Chunk read(final GUID guid, final InputStream stream, final long streamPosition) throws IOException { assert GUID.GUID_CONTENT_BRANDING.equals(guid); final BigInteger chunkSize = Utils.readBig64(stream); final long imageType = Utils.readUINT32(stream); assert imageType >= 0 && imageType <= 3 : imageType; final long imageDataSize = Utils.readUINT32(stream); assert imageType > 0 || imageDataSize == 0 : imageDataSize; assert imageDataSize < Integer.MAX_VALUE; final byte[] imageData = Utils.readBinary(stream, imageDataSize); final long copyRightUrlLen = Utils.readUINT32(stream); final String copyRight = new String(Utils.readBinary(stream, copyRightUrlLen)); final long imageUrlLen = Utils.readUINT32(stream); final String imageUrl = new String(Utils.readBinary(stream, imageUrlLen)); final ContentBranding result = new ContentBranding(streamPosition, chunkSize); result.setImage(imageType, imageData); result.setCopyRightURL(copyRight); result.setBannerImageURL(imageUrl); return result; }
{@inheritDoc}
ContentBrandingReader::read
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/asf/io/ContentBrandingReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/asf/io/ContentBrandingReader.java
Apache-2.0
public OggPageHeader readOggPageHeader(RandomAccessFile raf, int count) throws CannotReadException, IOException { OggPageHeader pageHeader = OggPageHeader.read(raf); while (count > 0) { raf.seek(raf.getFilePointer() + pageHeader.getPageLength()); pageHeader = OggPageHeader.read(raf); count--; } return pageHeader; }
Return count Ogg Page header, count starts from zero count=0; should return PageHeader that contains Vorbis Identification Header count=1; should return Pageheader that contains VorbisComment and possibly SetupHeader count>=2; should return PageHeader containing remaining VorbisComment,SetupHeader and/or Audio @param raf @param count @return @throws CannotReadException @throws IOException
OggFileReader::readOggPageHeader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggFileReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggFileReader.java
Apache-2.0
public void summarizeOggPageHeaders(File oggFile) throws CannotReadException, IOException { RandomAccessFile raf = new RandomAccessFile(oggFile, "r"); while (raf.getFilePointer() < raf.length()) { System.out.println("pageHeader starts at absolute file position:" + raf.getFilePointer()); OggPageHeader pageHeader = OggPageHeader.read(raf); System.out.println("pageHeader finishes at absolute file position:" + raf.getFilePointer()); System.out.println(pageHeader + "\n"); raf.seek(raf.getFilePointer() + pageHeader.getPageLength()); } System.out.println("Raf File Pointer at:" + raf.getFilePointer() + "File Size is:" + raf.length()); raf.close(); }
Summarize all the ogg headers in a file A useful utility function @param oggFile @throws CannotReadException @throws IOException
OggFileReader::summarizeOggPageHeaders
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggFileReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggFileReader.java
Apache-2.0
public void shortSummarizeOggPageHeaders(File oggFile) throws CannotReadException, IOException { RandomAccessFile raf = new RandomAccessFile(oggFile, "r"); int i = 0; while (raf.getFilePointer() < raf.length()) { System.out.println("pageHeader starts at absolute file position:" + raf.getFilePointer()); OggPageHeader pageHeader = OggPageHeader.read(raf); System.out.println("pageHeader finishes at absolute file position:" + raf.getFilePointer()); System.out.println(pageHeader + "\n"); raf.seek(raf.getFilePointer() + pageHeader.getPageLength()); i++; if(i>=5) { break; } } System.out.println("Raf File Pointer at:" + raf.getFilePointer() + "File Size is:" + raf.length()); raf.close(); }
Summarizes the first five pages, normally all we are interested in @param oggFile @throws CannotReadException @throws IOException
OggFileReader::shortSummarizeOggPageHeaders
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggFileReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggFileReader.java
Apache-2.0
public Tag read(RandomAccessFile raf) throws CannotReadException, IOException { logger.config("Starting to read ogg vorbis tag from file:"); byte[] rawVorbisCommentData = readRawPacketData(raf); //Begin tag reading VorbisCommentTag tag = vorbisCommentReader.read(rawVorbisCommentData, true); logger.fine("CompletedReadCommentTag"); return tag; }
Read the Logical VorbisComment Tag from the file <p>Read the CommenyTag, within an OggVorbis file the VorbisCommentTag is mandatory @param raf @return @throws CannotReadException @throws IOException
OggVorbisTagReader::read
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public int readOggVorbisRawSize(RandomAccessFile raf) throws CannotReadException, IOException { byte[] rawVorbisCommentData = readRawPacketData(raf); return rawVorbisCommentData.length + VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH; }
Retrieve the Size of the VorbisComment packet including the oggvorbis header @param raf @return @throws CannotReadException @throws IOException
OggVorbisTagReader::readOggVorbisRawSize
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public byte[] readRawPacketData(RandomAccessFile raf) throws CannotReadException, IOException { logger.fine("Read 1st page"); //1st page = codec infos OggPageHeader pageHeader = OggPageHeader.read(raf); //Skip over data to end of page header 1 raf.seek(raf.getFilePointer() + pageHeader.getPageLength()); logger.fine("Read 2nd page"); //2nd page = comment, may extend to additional pages or not , may also have setup header pageHeader = OggPageHeader.read(raf); //Now at start of packets on page 2 , check this is the vorbis comment header byte[] b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH]; raf.read(b); if (!isVorbisCommentHeader(b)) { throw new CannotReadException("Cannot find comment block (no vorbiscomment header)"); } //Convert the comment raw data which maybe over many pages back into raw packet byte[] rawVorbisCommentData = convertToVorbisCommentPacket(pageHeader, raf); return rawVorbisCommentData; }
Retrieve the raw VorbisComment packet data, does not include the OggVorbis header @param raf @return @throws CannotReadException if unable to find vorbiscomment header @throws IOException
OggVorbisTagReader::readRawPacketData
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public boolean isVorbisCommentHeader(byte[] headerData) { String vorbis = new String(headerData, VorbisHeader.FIELD_CAPTURE_PATTERN_POS, VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH, StandardCharsets.ISO_8859_1); return !(headerData[VorbisHeader.FIELD_PACKET_TYPE_POS] != VorbisPacketType.COMMENT_HEADER.getType() || !vorbis.equals(VorbisHeader.CAPTURE_PATTERN)); }
Is this a Vorbis Comment header, check Note this check only applies to Vorbis Comments embedded within an OggVorbis File which is why within here @param headerData @return true if the headerData matches a VorbisComment header i.e is a Vorbis header of type COMMENT_HEADER
OggVorbisTagReader::isVorbisCommentHeader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public boolean isVorbisSetupHeader(byte[] headerData) { String vorbis = new String(headerData, VorbisHeader.FIELD_CAPTURE_PATTERN_POS, VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH, StandardCharsets.ISO_8859_1); return !(headerData[VorbisHeader.FIELD_PACKET_TYPE_POS] != VorbisPacketType.SETUP_HEADER.getType() || !vorbis.equals(VorbisHeader.CAPTURE_PATTERN)); }
Is this a Vorbis SetupHeader check @param headerData @return true if matches vorbis setupheader
OggVorbisTagReader::isVorbisSetupHeader
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
private byte[] convertToVorbisCommentPacket(OggPageHeader startVorbisCommentPage, RandomAccessFile raf) throws IOException, CannotReadException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[startVorbisCommentPage.getPacketList().get(0).getLength() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH)]; raf.read(b); baos.write(b); //Because there is at least one other packet (SetupHeaderPacket) this means the Comment Packet has finished //on this page so thats all we need and we can return if (startVorbisCommentPage.getPacketList().size() > 1) { logger.config("Comments finish on 2nd Page because there is another packet on this page"); return baos.toByteArray(); } //There is only the VorbisComment packet on page if it has completed on this page we can return if (!startVorbisCommentPage.isLastPacketIncomplete()) { logger.config("Comments finish on 2nd Page because this packet is complete"); return baos.toByteArray(); } //The VorbisComment extends to the next page, so should be at end of page already //so carry on reading pages until we get to the end of comment while (true) { logger.config("Reading next page"); OggPageHeader nextPageHeader = OggPageHeader.read(raf); b = new byte[nextPageHeader.getPacketList().get(0).getLength()]; raf.read(b); baos.write(b); //Because there is at least one other packet (SetupHeaderPacket) this means the Comment Packet has finished //on this page so thats all we need and we can return if (nextPageHeader.getPacketList().size() > 1) { logger.config("Comments finish on Page because there is another packet on this page"); return baos.toByteArray(); } //There is only the VorbisComment packet on page if it has completed on this page we can return if (!nextPageHeader.isLastPacketIncomplete()) { logger.config("Comments finish on Page because this packet is complete"); return baos.toByteArray(); } } }
The Vorbis Comment may span multiple pages so we we need to identify the pages they contain and then extract the packet data from the pages @param startVorbisCommentPage @param raf @throws org.jaudiotagger.audio.exceptions.CannotReadException @throws java.io.IOException @return
OggVorbisTagReader::convertToVorbisCommentPacket
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public byte[] convertToVorbisSetupHeaderPacket(long fileOffsetOfStartingOggPage, RandomAccessFile raf) throws IOException, CannotReadException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); //Seek to specified offset raf.seek(fileOffsetOfStartingOggPage); //Read Page OggPageHeader setupPageHeader = OggPageHeader.read(raf); //Assume that if multiple packets first packet is VorbisComment and second packet //is setupheader if (setupPageHeader.getPacketList().size() > 1) { raf.skipBytes(setupPageHeader.getPacketList().get(0).getLength()); } //Now should be at start of next packet, check this is the vorbis setup header byte[] b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH]; raf.read(b); if (!isVorbisSetupHeader(b)) { throw new CannotReadException("Unable to find setup header(2), unable to write ogg file"); } //Go back to start of setupheader data raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH)); //Read data if (setupPageHeader.getPacketList().size() > 1) { b = new byte[setupPageHeader.getPacketList().get(1).getLength()]; raf.read(b); baos.write(b); } else { b = new byte[setupPageHeader.getPacketList().get(0).getLength()]; raf.read(b); baos.write(b); } //Return Data if (!setupPageHeader.isLastPacketIncomplete() || setupPageHeader.getPacketList().size() > 2) { logger.config("Setupheader finishes on this page"); return baos.toByteArray(); } //The Setupheader extends to the next page, so should be at end of page already //so carry on reading pages until we get to the end of comment while (true) { logger.config("Reading another page"); OggPageHeader nextPageHeader = OggPageHeader.read(raf); b = new byte[nextPageHeader.getPacketList().get(0).getLength()]; raf.read(b); baos.write(b); //Because there is at least one other packet this means the Setupheader Packet has finished //on this page so thats all we need and we can return if (nextPageHeader.getPacketList().size() > 1) { logger.config("Setupheader finishes on this page"); return baos.toByteArray(); } //There is only the Setupheader packet on page if it has completed on this page we can return if (!nextPageHeader.isLastPacketIncomplete()) { logger.config("Setupheader finish on Page because this packet is complete"); return baos.toByteArray(); } } }
The Vorbis Setup Header may span multiple(2) pages, athough it doesnt normally. We pass the start of the file offset of the OggPage it belongs on, it probably won't be first packet. @param fileOffsetOfStartingOggPage @param raf @throws org.jaudiotagger.audio.exceptions.CannotReadException @throws java.io.IOException @return
OggVorbisTagReader::convertToVorbisSetupHeaderPacket
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public byte[] convertToVorbisSetupHeaderPacketAndAdditionalPackets(long fileOffsetOfStartingOggPage, RandomAccessFile raf) throws IOException, CannotReadException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); //Seek to specified offset raf.seek(fileOffsetOfStartingOggPage); //Read Page OggPageHeader setupPageHeader = OggPageHeader.read(raf); //Assume that if multiple packets first packet is VorbisComment and second packet //is setupheader if (setupPageHeader.getPacketList().size() > 1) { raf.skipBytes(setupPageHeader.getPacketList().get(0).getLength()); } //Now should be at start of next packet, check this is the vorbis setup header byte[] b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH]; raf.read(b); if (!isVorbisSetupHeader(b)) { throw new CannotReadException("Unable to find setup header(2), unable to write ogg file"); } //Go back to start of setupheader data raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH)); //Read data if (setupPageHeader.getPacketList().size() > 1) { b = new byte[setupPageHeader.getPacketList().get(1).getLength()]; raf.read(b); baos.write(b); } else { b = new byte[setupPageHeader.getPacketList().get(0).getLength()]; raf.read(b); baos.write(b); } //Return Data if (!setupPageHeader.isLastPacketIncomplete() || setupPageHeader.getPacketList().size() > 2) { logger.config("Setupheader finishes on this page"); if (setupPageHeader.getPacketList().size() > 2) { for (int i = 2; i < setupPageHeader.getPacketList().size(); i++) { b = new byte[setupPageHeader.getPacketList().get(i).getLength()]; raf.read(b); baos.write(b); } } return baos.toByteArray(); } //The Setupheader extends to the next page, so should be at end of page already //so carry on reading pages until we get to the end of comment while (true) { logger.config("Reading another page"); OggPageHeader nextPageHeader = OggPageHeader.read(raf); b = new byte[nextPageHeader.getPacketList().get(0).getLength()]; raf.read(b); baos.write(b); //Because there is at least one other packet this means the Setupheader Packet has finished //on this page so thats all we need and we can return if (nextPageHeader.getPacketList().size() > 1) { logger.config("Setupheader finishes on this page"); return baos.toByteArray(); } //There is only the Setupheader packet on page if it has completed on this page we can return if (!nextPageHeader.isLastPacketIncomplete()) { logger.config("Setupheader finish on Page because this packet is complete"); return baos.toByteArray(); } } }
The Vorbis Setup Header may span multiple(2) pages, athough it doesnt normally. We pass the start of the file offset of the OggPage it belongs on, it probably won't be first packet, also returns any addditional packets that immediately follow the setup header in original file @param fileOffsetOfStartingOggPage @param raf @throws org.jaudiotagger.audio.exceptions.CannotReadException @throws java.io.IOException @return
OggVorbisTagReader::convertToVorbisSetupHeaderPacketAndAdditionalPackets
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public OggVorbisHeaderSizes readOggVorbisHeaderSizes(RandomAccessFile raf) throws CannotReadException, IOException { logger.fine("Started to read comment and setup header sizes:"); //Stores filepointers so return file in same state long filepointer = raf.getFilePointer(); //Extra Packets on same page as setup header List<OggPageHeader.PacketStartAndLength> extraPackets = new ArrayList<OggPageHeader.PacketStartAndLength>(); long commentHeaderStartPosition; long setupHeaderStartPosition; int commentHeaderSize = 0; int setupHeaderSize; //1st page = codec infos OggPageHeader pageHeader = OggPageHeader.read(raf); //Skip over data to end of page header 1 raf.seek(raf.getFilePointer() + pageHeader.getPageLength()); //2nd page = comment, may extend to additional pages or not , may also have setup header pageHeader = OggPageHeader.read(raf); commentHeaderStartPosition = raf.getFilePointer() - (OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageHeader.getSegmentTable().length); //Now at start of packets on page 2 , check this is the vorbis comment header byte[] b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH]; raf.read(b); if (!isVorbisCommentHeader(b)) { throw new CannotReadException("Cannot find comment block (no vorbiscomment header)"); } raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH)); logger.config("Found start of comment header at:" + raf.getFilePointer()); //Calculate Comment Size (not inc header) while (true) { List<OggPageHeader.PacketStartAndLength> packetList = pageHeader.getPacketList(); commentHeaderSize += packetList.get(0).getLength(); raf.skipBytes(packetList.get(0).getLength()); //If this page contains multiple packets or if this last packet is complete then the Comment header //end son this page and we can break if (packetList.size() > 1 || !pageHeader.isLastPacketIncomplete()) { //done comment size logger.config("Found end of comment:size:" + commentHeaderSize + "finishes at file position:" + raf.getFilePointer()); break; } pageHeader = OggPageHeader.read(raf); } //If there are no more packets on this page we need to go to next page to get the setup header OggPageHeader.PacketStartAndLength packet; if(pageHeader.getPacketList().size()==1) { pageHeader = OggPageHeader.read(raf); List<OggPageHeader.PacketStartAndLength> packetList = pageHeader.getPacketList(); packet = pageHeader.getPacketList().get(0); //Now at start of next packet , check this is the vorbis setup header b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH]; raf.read(b); if (!isVorbisSetupHeader(b)) { throw new CannotReadException(ErrorMessage.OGG_VORBIS_NO_VORBIS_HEADER_FOUND.getMsg()); } raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH)); logger.config("Found start of vorbis setup header at file position:" + raf.getFilePointer()); //Set this to the start of the OggPage that setupheader was found on setupHeaderStartPosition = raf.getFilePointer() - (OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageHeader.getSegmentTable().length); //Add packet data to size to the setup header size setupHeaderSize = packet.getLength(); logger.fine("Adding:" + packet.getLength() + " to setup header size"); //Skip over the packet data raf.skipBytes(packet.getLength()); //If there are other packets that follow this one, or if the last packet is complete then we must have //got the size of the setup header. if (packetList.size() > 1 || !pageHeader.isLastPacketIncomplete()) { logger.config("Found end of setupheader:size:" + setupHeaderSize + "finishes at:" + raf.getFilePointer()); if (packetList.size() > 1) { extraPackets = packetList.subList(1, packetList.size()); } } //The setup header continues onto the next page else { pageHeader = OggPageHeader.read(raf); packetList = pageHeader.getPacketList(); while (true) { setupHeaderSize += packetList.get(0).getLength(); logger.fine("Adding:" + packetList.get(0).getLength() + " to setup header size"); raf.skipBytes(packetList.get(0).getLength()); if (packetList.size() > 1 || !pageHeader.isLastPacketIncomplete()) { //done setup size logger.fine("Found end of setupheader:size:" + setupHeaderSize + "finishes at:" + raf.getFilePointer()); if (packetList.size() > 1) { extraPackets = packetList.subList(1, packetList.size()); } break; } //Continues onto another page pageHeader = OggPageHeader.read(raf); } } } //else its next packet on this page else { packet = pageHeader.getPacketList().get(1); List<OggPageHeader.PacketStartAndLength> packetList = pageHeader.getPacketList(); //Now at start of next packet , check this is the vorbis setup header b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH]; raf.read(b); if (!isVorbisSetupHeader(b)) { logger.warning("Expecting but got:"+new String(b)+ "at "+(raf.getFilePointer() - b.length)); throw new CannotReadException(ErrorMessage.OGG_VORBIS_NO_VORBIS_HEADER_FOUND.getMsg()); } raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH)); logger.config("Found start of vorbis setup header at file position:" + raf.getFilePointer()); //Set this to the start of the OggPage that setupheader was found on setupHeaderStartPosition = raf.getFilePointer() - (OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageHeader.getSegmentTable().length) - pageHeader.getPacketList().get(0).getLength(); //Add packet data to size to the setup header size setupHeaderSize = packet.getLength(); logger.fine("Adding:" + packet.getLength() + " to setup header size"); //Skip over the packet data raf.skipBytes(packet.getLength()); //If there are other packets that follow this one, or if the last packet is complete then we must have //got the size of the setup header. if (packetList.size() > 2 || !pageHeader.isLastPacketIncomplete()) { logger.fine("Found end of setupheader:size:" + setupHeaderSize + "finishes at:" + raf.getFilePointer()); if (packetList.size() > 2) { extraPackets = packetList.subList(2, packetList.size()); } } //The setup header continues onto the next page else { pageHeader = OggPageHeader.read(raf); packetList = pageHeader.getPacketList(); while (true) { setupHeaderSize += packetList.get(0).getLength(); logger.fine("Adding:" + packetList.get(0).getLength() + " to setup header size"); raf.skipBytes(packetList.get(0).getLength()); if (packetList.size() > 1 || !pageHeader.isLastPacketIncomplete()) { //done setup size logger.fine("Found end of setupheader:size:" + setupHeaderSize + "finishes at:" + raf.getFilePointer()); if (packetList.size() > 1) { extraPackets = packetList.subList(1, packetList.size()); } break; } //Continues onto another page pageHeader = OggPageHeader.read(raf); } } } //Reset filepointer to location that it was in at start of method raf.seek(filepointer); return new OggVorbisHeaderSizes(commentHeaderStartPosition, setupHeaderStartPosition, commentHeaderSize, setupHeaderSize, extraPackets); }
Calculate the size of the packet data for the comment and setup headers @param raf @return @throws CannotReadException @throws IOException
OggVorbisTagReader::readOggVorbisHeaderSizes
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public int getCommentHeaderSize() { return commentHeaderSize; }
@return the size of the raw packet data for the vorbis comment header (includes vorbis header)
OggVorbisHeaderSizes::getCommentHeaderSize
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public int getSetupHeaderSize() { return setupHeaderSize; }
@return he size of the raw packet data for the vorbis setup header (includes vorbis header)
OggVorbisHeaderSizes::getSetupHeaderSize
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public int getExtraPacketDataSize() { int extraPacketSize = 0; for (OggPageHeader.PacketStartAndLength packet : packetList) { extraPacketSize += packet.getLength(); } return extraPacketSize; }
Return the size required by all the extra packets on same page as setup header, usually there are no packets immediately after the setup packet. @return extra data size required for additional packets on same page
OggVorbisHeaderSizes::getExtraPacketDataSize
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public long getCommentHeaderStartPosition() { return commentHeaderStartPosition; }
@return the start position in the file of the ogg header which contains the start of the Vorbis Comment
OggVorbisHeaderSizes::getCommentHeaderStartPosition
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
public long getSetupHeaderStartPosition() { return setupHeaderStartPosition; }
@return the start position in the file of the ogg header which contains the start of the Setup Header
OggVorbisHeaderSizes::getSetupHeaderStartPosition
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagReader.java
Apache-2.0
private void calculateChecksumOverPage(ByteBuffer page) { //CRC should be zero before calculating it page.putInt(OggPageHeader.FIELD_PAGE_CHECKSUM_POS, 0); //Compute CRC over the page //TODO shouldnt really use array(); byte[] crc = OggCRCFactory.computeCRC(page.array()); for (int i = 0; i < crc.length; i++) { page.put(OggPageHeader.FIELD_PAGE_CHECKSUM_POS + i, crc[i]); } //Rewind to start of Page page.rewind(); }
Calculate checkSum over the Page @param page
OggVorbisTagWriter::calculateChecksumOverPage
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
Apache-2.0
private ByteBuffer startCreateBasicSecondPage( OggVorbisTagReader.OggVorbisHeaderSizes vorbisHeaderSizes, int newCommentLength, int newSecondPageLength, OggPageHeader secondPageHeader, ByteBuffer newComment) throws IOException { logger.fine("WriteOgg Type 1"); byte[] segmentTable = createSegmentTable(newCommentLength, vorbisHeaderSizes.getSetupHeaderSize(), vorbisHeaderSizes.getExtraPacketList()); int newSecondPageHeaderLength = OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + segmentTable.length; logger.fine("New second page header length:" + newSecondPageHeaderLength); logger.fine("No of segments:" + segmentTable.length); ByteBuffer secondPageBuffer = ByteBuffer.allocate(newSecondPageLength + newSecondPageHeaderLength); secondPageBuffer.order(ByteOrder.LITTLE_ENDIAN); //Build the new 2nd page header, can mostly be taken from the original upto the segment length OggS capture secondPageBuffer.put(secondPageHeader.getRawHeaderData(), 0, OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH - 1); //Number of Page Segments secondPageBuffer.put((byte) segmentTable.length); //Page segment table for (byte aSegmentTable : segmentTable) { secondPageBuffer.put(aSegmentTable); } //Add New VorbisComment secondPageBuffer.put(newComment); return secondPageBuffer; }
Create a second Page, and add comment header to it, but page is incomplete may want to add addition header and need to calculate CRC @param vorbisHeaderSizes @param newCommentLength @param newSecondPageLength @param secondPageHeader @param newComment @return @throws IOException
OggVorbisTagWriter::startCreateBasicSecondPage
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
Apache-2.0
private void replaceSecondPageOnly( OggVorbisTagReader.OggVorbisHeaderSizes vorbisHeaderSizes, int newCommentLength, int newSecondPageLength, OggPageHeader secondPageHeader, ByteBuffer newComment, long secondPageHeaderEndPos, RandomAccessFile raf, RandomAccessFile rafTemp) throws IOException { logger.fine("WriteOgg Type 1"); ByteBuffer secondPageBuffer = startCreateBasicSecondPage(vorbisHeaderSizes, newCommentLength, newSecondPageLength, secondPageHeader, newComment); raf.seek(secondPageHeaderEndPos); //Skip comment header raf.skipBytes(vorbisHeaderSizes.getCommentHeaderSize()); //Read in setup header and extra packets raf.getChannel().read(secondPageBuffer); calculateChecksumOverPage(secondPageBuffer); rafTemp.getChannel().write(secondPageBuffer); rafTemp.getChannel().transferFrom(raf.getChannel(), rafTemp.getFilePointer(), raf.length() - raf.getFilePointer()); }
Usually can use this method, previously comment and setup header all fit on page 2 and they still do, so just replace this page. And copy further pages as is. @param vorbisHeaderSizes @param newCommentLength @param newSecondPageLength @param secondPageHeader @param newComment @param secondPageHeaderEndPos @param raf @param rafTemp @throws IOException
OggVorbisTagWriter::replaceSecondPageOnly
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
Apache-2.0
private void replaceSecondPageAndRenumberPageSeqs(OggVorbisTagReader.OggVorbisHeaderSizes originalHeaderSizes, int newCommentLength, int newSecondPageLength, OggPageHeader secondPageHeader, ByteBuffer newComment, RandomAccessFile raf, RandomAccessFile rafTemp) throws IOException, CannotReadException, CannotWriteException { logger.fine("WriteOgg Type 2"); ByteBuffer secondPageBuffer = startCreateBasicSecondPage(originalHeaderSizes, newCommentLength, newSecondPageLength, secondPageHeader, newComment); //Add setup header and packets int pageSequence = secondPageHeader.getPageSequence(); byte[] setupHeaderData = reader.convertToVorbisSetupHeaderPacketAndAdditionalPackets(originalHeaderSizes.getSetupHeaderStartPosition(), raf); logger.finest(setupHeaderData.length + ":" + secondPageBuffer.position() + ":" + secondPageBuffer.capacity()); secondPageBuffer.put(setupHeaderData); calculateChecksumOverPage(secondPageBuffer); rafTemp.getChannel().write(secondPageBuffer); writeRemainingPages(pageSequence, raf, rafTemp); }
Previously comment and/or setup header was on a number of pages now can just replace this page fitting all on 2nd page, and renumber subsequent sequence pages @param originalHeaderSizes @param newCommentLength @param newSecondPageLength @param secondPageHeader @param newComment @param raf @param rafTemp @throws IOException @throws org.jaudiotagger.audio.exceptions.CannotReadException @throws org.jaudiotagger.audio.exceptions.CannotWriteException
OggVorbisTagWriter::replaceSecondPageAndRenumberPageSeqs
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
Apache-2.0
private void replacePagesAndRenumberPageSeqs(OggVorbisTagReader.OggVorbisHeaderSizes originalHeaderSizes, int newCommentLength, OggPageHeader secondPageHeader, ByteBuffer newComment, RandomAccessFile raf, RandomAccessFile rafTemp) throws IOException, CannotReadException, CannotWriteException { int pageSequence = secondPageHeader.getPageSequence(); //We need to work out how to split the newcommentlength over the pages int noOfCompletePagesNeededForComment = newCommentLength / OggPageHeader.MAXIMUM_PAGE_DATA_SIZE; logger.config("Comment requires:" + noOfCompletePagesNeededForComment + " complete pages"); //Create the Pages int newCommentOffset = 0; if (noOfCompletePagesNeededForComment > 0) { for (int i = 0; i < noOfCompletePagesNeededForComment; i++) { //Create ByteBuffer for the New page byte[] segmentTable = this.createSegments(OggPageHeader.MAXIMUM_PAGE_DATA_SIZE, false); int pageHeaderLength = OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + segmentTable.length; ByteBuffer pageBuffer = ByteBuffer.allocate(pageHeaderLength + OggPageHeader.MAXIMUM_PAGE_DATA_SIZE); pageBuffer.order(ByteOrder.LITTLE_ENDIAN); //Now create the page basing it on the existing 2ndpageheader pageBuffer.put(secondPageHeader.getRawHeaderData(), 0, OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH - 1); //Number of Page Segments pageBuffer.put((byte) segmentTable.length); //Page segment table for (byte aSegmentTable : segmentTable) { pageBuffer.put(aSegmentTable); } //Get next bit of Comment ByteBuffer nextPartOfComment = newComment.slice(); nextPartOfComment.limit(OggPageHeader.MAXIMUM_PAGE_DATA_SIZE); pageBuffer.put(nextPartOfComment); //Recalculate Page Sequence Number pageBuffer.putInt(OggPageHeader.FIELD_PAGE_SEQUENCE_NO_POS, pageSequence); pageSequence++; //Set Header Flag to indicate continuous (except for first flag) if (i != 0) { pageBuffer.put(OggPageHeader.FIELD_HEADER_TYPE_FLAG_POS, OggPageHeader.HeaderTypeFlag.CONTINUED_PACKET.getFileValue()); } calculateChecksumOverPage(pageBuffer); rafTemp.getChannel().write(pageBuffer); newCommentOffset += OggPageHeader.MAXIMUM_PAGE_DATA_SIZE; newComment.position(newCommentOffset); } } int lastPageCommentPacketSize = newCommentLength % OggPageHeader.MAXIMUM_PAGE_DATA_SIZE; logger.fine("Last comment packet size:" + lastPageCommentPacketSize); //End of comment and setup header cannot fit on the last page if (!isCommentAndSetupHeaderFitsOnASinglePage(lastPageCommentPacketSize, originalHeaderSizes.getSetupHeaderSize(), originalHeaderSizes.getExtraPacketList())) { logger.fine("WriteOgg Type 3"); //Write the last part of comment only (its possible it might be the only comment) { byte[] segmentTable = createSegments(lastPageCommentPacketSize, true); int pageHeaderLength = OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + segmentTable.length; ByteBuffer pageBuffer = ByteBuffer.allocate(lastPageCommentPacketSize + pageHeaderLength); pageBuffer.order(ByteOrder.LITTLE_ENDIAN); pageBuffer.put(secondPageHeader.getRawHeaderData(), 0, OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH - 1); pageBuffer.put((byte) segmentTable.length); for (byte aSegmentTable : segmentTable) { pageBuffer.put(aSegmentTable); } newComment.position(newCommentOffset); pageBuffer.put(newComment.slice()); pageBuffer.putInt(OggPageHeader.FIELD_PAGE_SEQUENCE_NO_POS, pageSequence); if(noOfCompletePagesNeededForComment>0) { pageBuffer.put(OggPageHeader.FIELD_HEADER_TYPE_FLAG_POS, OggPageHeader.HeaderTypeFlag.CONTINUED_PACKET.getFileValue()); } logger.fine("Writing Last Comment Page "+pageSequence +" to file"); pageSequence++; calculateChecksumOverPage(pageBuffer); rafTemp.getChannel().write(pageBuffer); } //Now write header and extra packets onto next page { byte[] segmentTable = this.createSegmentTable(originalHeaderSizes.getSetupHeaderSize(),originalHeaderSizes.getExtraPacketList()); int pageHeaderLength = OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + segmentTable.length; byte[] setupHeaderData = reader.convertToVorbisSetupHeaderPacketAndAdditionalPackets(originalHeaderSizes.getSetupHeaderStartPosition(), raf); ByteBuffer pageBuffer = ByteBuffer.allocate(setupHeaderData.length + pageHeaderLength); pageBuffer.order(ByteOrder.LITTLE_ENDIAN); pageBuffer.put(secondPageHeader.getRawHeaderData(), 0, OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH - 1); pageBuffer.put((byte) segmentTable.length); for (byte aSegmentTable : segmentTable) { pageBuffer.put(aSegmentTable); } pageBuffer.put(setupHeaderData); pageBuffer.putInt(OggPageHeader.FIELD_PAGE_SEQUENCE_NO_POS, pageSequence); //pageBuffer.put(OggPageHeader.FIELD_HEADER_TYPE_FLAG_POS, OggPageHeader.HeaderTypeFlag.CONTINUED_PACKET.getFileValue()); logger.fine("Writing Setup Header and packets Page "+pageSequence +" to file"); calculateChecksumOverPage(pageBuffer); rafTemp.getChannel().write(pageBuffer); } } else { //End of Comment and SetupHeader and extra packets can fit on one page logger.fine("WriteOgg Type 4"); //Create last header page int newSecondPageDataLength = originalHeaderSizes.getSetupHeaderSize() + lastPageCommentPacketSize + originalHeaderSizes.getExtraPacketDataSize(); newComment.position(newCommentOffset); ByteBuffer lastComment = newComment.slice(); ByteBuffer lastHeaderBuffer = startCreateBasicSecondPage( originalHeaderSizes, lastPageCommentPacketSize, newSecondPageDataLength, secondPageHeader, lastComment); //Now find the setupheader which is on a different page raf.seek(originalHeaderSizes.getSetupHeaderStartPosition()); //Add setup Header and Extra Packets (although it will fit in this page, it may be over multiple pages in its original form //so need to use this function to convert to raw data byte[] setupHeaderData = reader.convertToVorbisSetupHeaderPacketAndAdditionalPackets(originalHeaderSizes.getSetupHeaderStartPosition(), raf); lastHeaderBuffer.put(setupHeaderData); //Page Sequence No lastHeaderBuffer.putInt(OggPageHeader.FIELD_PAGE_SEQUENCE_NO_POS, pageSequence); //Set Header Flag to indicate continuous (contains end of comment) lastHeaderBuffer.put(OggPageHeader.FIELD_HEADER_TYPE_FLAG_POS, OggPageHeader.HeaderTypeFlag.CONTINUED_PACKET.getFileValue()); calculateChecksumOverPage(lastHeaderBuffer); rafTemp.getChannel().write(lastHeaderBuffer); } //Write the rest of the original file writeRemainingPages(pageSequence, raf, rafTemp); }
CommentHeader extends over multiple pages OR Comment Header doesnt but it's got larger causing some extra packets to be shifted onto another page. @param originalHeaderSizes @param newCommentLength @param secondPageHeader @param newComment @param raf @param rafTemp @throws IOException @throws CannotReadException @throws CannotWriteException
OggVorbisTagWriter::replacePagesAndRenumberPageSeqs
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
Apache-2.0
public void writeRemainingPages(int pageSequence, RandomAccessFile raf, RandomAccessFile rafTemp) throws IOException, CannotReadException, CannotWriteException { long startAudio = raf.getFilePointer(); long startAudioWritten = rafTemp.getFilePointer(); //TODO there is a risk we wont have enough memory to create these buffers ByteBuffer bb = ByteBuffer.allocate((int) (raf.length() - raf.getFilePointer())); ByteBuffer bbTemp = ByteBuffer.allocate((int)(raf.length() - raf.getFilePointer())); //Read in the rest of the data into bytebuffer and rewind it to start raf.getChannel().read(bb); bb.rewind(); long bytesToDiscard = 0; while(bb.hasRemaining()) { OggPageHeader nextPage=null; try { nextPage = OggPageHeader.read(bb); } catch(CannotReadException cre) { //Go back to where were bb.position(bb.position() - OggPageHeader.CAPTURE_PATTERN.length); //#117:Ogg file with invalid ID3v1 tag at end remove and save if(Utils.readThreeBytesAsChars(bb).equals(AbstractID3v1Tag.TAG)) { bytesToDiscard = bb.remaining() + AbstractID3v1Tag.TAG.length(); break; } else { throw cre; } } //Create buffer large enough for next page (header and data) and set byte order to LE so we can use //putInt method ByteBuffer nextPageHeaderBuffer = ByteBuffer.allocate(nextPage.getRawHeaderData().length + nextPage.getPageLength()); nextPageHeaderBuffer.order(ByteOrder.LITTLE_ENDIAN); nextPageHeaderBuffer.put(nextPage.getRawHeaderData()); ByteBuffer data = bb.slice(); data.limit(nextPage.getPageLength()); nextPageHeaderBuffer.put(data); nextPageHeaderBuffer.putInt(OggPageHeader.FIELD_PAGE_SEQUENCE_NO_POS, ++pageSequence); calculateChecksumOverPage(nextPageHeaderBuffer); bb.position(bb.position() + nextPage.getPageLength()); nextPageHeaderBuffer.rewind(); bbTemp.put(nextPageHeaderBuffer); } //Now just write as a single IO operation bbTemp.flip(); rafTemp.getChannel().write(bbTemp); //Check we have written all the data (minus any invalid Tag at end) if ((raf.length() - startAudio) != ((rafTemp.length() + bytesToDiscard) - startAudioWritten)) { throw new CannotWriteException("File written counts don't match, file not written:" +"origAudioLength:"+(raf.length() - startAudio) +":newAudioLength:"+((rafTemp.length() + bytesToDiscard) - startAudioWritten) +":bytesDiscarded:"+bytesToDiscard); } }
Write all the remaining pages as they are except that the page sequence needs to be modified. @param pageSequence @param raf @param rafTemp @throws IOException @throws CannotReadException @throws CannotWriteException
OggVorbisTagWriter::writeRemainingPages
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
Apache-2.0
private byte[] createSegmentTable(int newCommentLength, int setupHeaderLength, List<OggPageHeader.PacketStartAndLength> extraPackets) { logger.finest("Create SegmentTable CommentLength:" + newCommentLength + ":SetupHeaderLength:" + setupHeaderLength); ByteArrayOutputStream resultBaos = new ByteArrayOutputStream(); byte[] newStart; byte[] restShouldBe; byte[] nextPacket; //Vorbis Comment if (setupHeaderLength == 0) { //Comment Stream continues onto next page so last lacing value can be 255 newStart = createSegments(newCommentLength, false); return newStart; } else { //Comment Stream finishes on this page so if is a multiple of 255 //have to add an extra entry. newStart = createSegments(newCommentLength, true); } //Setup Header, should be closed if (extraPackets.size() > 0) { restShouldBe = createSegments(setupHeaderLength, true); } //.. continue sonto next page else { restShouldBe = createSegments(setupHeaderLength, false); } logger.finest("Created " + newStart.length + " segments for header"); logger.finest("Created " + restShouldBe.length + " segments for setup"); try { resultBaos.write(newStart); resultBaos.write(restShouldBe); if (extraPackets.size() > 0) { //Packets are being copied literally not converted from a length, so always pass //false parameter, TODO is this statement correct logger.finer("Creating segments for " + extraPackets.size() + " packets"); for (OggPageHeader.PacketStartAndLength packet : extraPackets) { nextPacket = createSegments(packet.getLength(), false); resultBaos.write(nextPacket); } } } catch (IOException ioe) { throw new RuntimeException("Unable to create segment table:" + ioe.getMessage()); } return resultBaos.toByteArray(); }
This method creates a new segment table for the second page (header). @param newCommentLength The length of the Vorbis Comment @param setupHeaderLength The length of Setup Header, zero if comment String extends over multiple pages and this is not the last page. @param extraPackets If there are packets immediately after setup header in same page, they need including in the segment table @return new segment table.
OggVorbisTagWriter::createSegmentTable
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
Apache-2.0
private byte[] createSegmentTable(int setupHeaderLength, List<OggPageHeader.PacketStartAndLength> extraPackets) { ByteArrayOutputStream resultBaos = new ByteArrayOutputStream(); byte[] restShouldBe; byte[] nextPacket; //Setup Header restShouldBe = createSegments(setupHeaderLength, true); try { resultBaos.write(restShouldBe); if (extraPackets.size() > 0) { //Packets are being copied literally not converted from a length, so always pass //false parameter, TODO is this statement correct for (OggPageHeader.PacketStartAndLength packet : extraPackets) { nextPacket = createSegments(packet.getLength(), false); resultBaos.write(nextPacket); } } } catch (IOException ioe) { throw new RuntimeException("Unable to create segment table:" + ioe.getMessage()); } return resultBaos.toByteArray(); }
This method creates a new segment table for the second half of setup header @param setupHeaderLength The length of Setup Header, zero if comment String extends over multiple pages and this is not the last page. @param extraPackets If there are packets immediately after setup header in same page, they need including in the segment table @return new segment table.
OggVorbisTagWriter::createSegmentTable
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
Apache-2.0
private boolean isCommentAndSetupHeaderFitsOnASinglePage(int commentLength, int setupHeaderLength, List<OggPageHeader.PacketStartAndLength> extraPacketList) { int totalDataSize = 0; if (commentLength == 0) { totalDataSize++; } else { totalDataSize = (commentLength / OggPageHeader.MAXIMUM_SEGMENT_SIZE) + 1; if (commentLength % OggPageHeader.MAXIMUM_SEGMENT_SIZE == 0) { totalDataSize++; } } logger.finest("Require:" + totalDataSize + " segments for comment"); if (setupHeaderLength == 0) { totalDataSize++; } else { totalDataSize += (setupHeaderLength / OggPageHeader.MAXIMUM_SEGMENT_SIZE) + 1; if (setupHeaderLength % OggPageHeader.MAXIMUM_SEGMENT_SIZE == 0) { totalDataSize++; } } logger.finest("Require:" + totalDataSize + " segments for comment plus setup"); for (OggPageHeader.PacketStartAndLength extraPacket : extraPacketList) { if (extraPacket.getLength() == 0) { totalDataSize++; } else { totalDataSize += (extraPacket.getLength() / OggPageHeader.MAXIMUM_SEGMENT_SIZE) + 1; if (extraPacket.getLength() % OggPageHeader.MAXIMUM_SEGMENT_SIZE == 0) { totalDataSize++; } } } logger.finest("Total No Of Segment If New Comment And Header Put On One Page:" + totalDataSize); return totalDataSize <= OggPageHeader.MAXIMUM_NO_OF_SEGMENT_SIZE; }
@param commentLength @param setupHeaderLength @param extraPacketList @return true if there is enough room to fit the comment and the setup headers on one page taking into account the maximum no of segments allowed per page and zero lacing values.
OggVorbisTagWriter::isCommentAndSetupHeaderFitsOnASinglePage
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/OggVorbisTagWriter.java
Apache-2.0
public static OggPageHeader read(ByteBuffer byteBuffer) throws IOException, CannotReadException { //byteBuffer int start = byteBuffer.position(); logger.fine("Trying to read OggPage at:" + start); byte[] b = new byte[OggPageHeader.CAPTURE_PATTERN.length]; byteBuffer.get(b); if (!(Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) { throw new CannotReadException(ErrorMessage.OGG_HEADER_CANNOT_BE_FOUND.getMsg(new String(b))); } byteBuffer.position(start + OggPageHeader.FIELD_PAGE_SEGMENTS_POS); int pageSegments = byteBuffer.get() & 0xFF; //unsigned byteBuffer.position(start); b = new byte[OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageSegments]; byteBuffer.get(b); OggPageHeader pageHeader = new OggPageHeader(b); //Now just after PageHeader, ready for Packet Data return pageHeader; }
Read next PageHeader from Buffer @param byteBuffer @return @throws IOException @throws CannotReadException
OggPageHeader::read
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
Apache-2.0
public static OggPageHeader read(RandomAccessFile raf) throws IOException, CannotReadException { long start = raf.getFilePointer(); logger.fine("Trying to read OggPage at:" + start); byte[] b = new byte[OggPageHeader.CAPTURE_PATTERN.length]; raf.read(b); if (!(Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) { raf.seek(start); if(AbstractID3v2Tag.isId3Tag(raf)) { logger.warning(ErrorMessage.OGG_CONTAINS_ID3TAG.getMsg(raf.getFilePointer() - start)); raf.read(b); if ((Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) { //Go to the end of the ID3 header start=raf.getFilePointer() - OggPageHeader.CAPTURE_PATTERN.length; } } else { throw new CannotReadException(ErrorMessage.OGG_HEADER_CANNOT_BE_FOUND.getMsg(new String(b))); } } raf.seek(start + OggPageHeader.FIELD_PAGE_SEGMENTS_POS); int pageSegments = raf.readByte() & 0xFF; //unsigned raf.seek(start); b = new byte[OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageSegments]; raf.read(b); OggPageHeader pageHeader = new OggPageHeader(b); pageHeader.setStartByte(start); //Now just after PageHeader, ready for Packet Data return pageHeader; }
Read next PageHeader from file @param raf @return @throws IOException @throws CannotReadException
OggPageHeader::read
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
Apache-2.0
public boolean isLastPacketIncomplete() { return lastPacketIncomplete; }
@return true if the last packet on this page extends to the next page
OggPageHeader::isLastPacketIncomplete
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
Apache-2.0
public List<PacketStartAndLength> getPacketList() { return packetList; }
@return a list of packet start position and size within this page.
OggPageHeader::getPacketList
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
Apache-2.0
public byte[] getRawHeaderData() { return rawHeaderData; }
@return the raw header data that this pageheader is derived from
OggPageHeader::getRawHeaderData
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
Apache-2.0
public long getStartByte() { return startByte; }
Startbyte of this pageHeader in the file This is useful for Ogg files that contain unsupported additional data at the start of the file such as ID3 data
OggPageHeader::getStartByte
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
Apache-2.0
public byte getFileValue() { return fileValue; }
@return the value that should be written to file to enable this flag
HeaderTypeFlag::getFileValue
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/ogg/util/OggPageHeader.java
Apache-2.0
public ExtDouble(byte[] rawData) { _rawData = rawData; }
Constructor. @param rawData A 10-byte array representing the number in the sequence in which it was stored.
ExtDouble::ExtDouble
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/ExtDouble.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/ExtDouble.java
Apache-2.0
public double toDouble() { int sign; int exponent; long mantissa = 0; // Extract the sign bit. sign = _rawData[0] >> 7; // Extract the exponent. It's stored with a // bias of 16383, so subtract that off. // Also, the mantissa is between 1 and 2 (i.e., // all but 1 digits are to the right of the binary point, so // we take 62 (not 63: see below) off the exponent for that. exponent = (_rawData[0] << 8) | _rawData[1]; exponent &= 0X7FFF; // strip off sign bit exponent -= (16383 + 62); // 1 is added to the "real" exponent // Extract the mantissa. It's 64 bits of unsigned // data, but a long is a signed number, so we have to // discard the LSB. We'll lose more than that converting // to double anyway. This division by 2 is the reason for // adding an extra 1 to the exponent above. int shifter = 55; for (int i = 2; i < 9; i++) { mantissa |= ((long) _rawData[i] & 0XFFL) << shifter; shifter -= 8; } mantissa |= _rawData[9] >>> 1; // Now put it together in a floating point number. double val = Math.pow(2, exponent); val *= mantissa; if (sign != 0) { val = -val; } return val; }
Convert the value to a Java double. This results in loss of precision. If the number is out of range, results aren't guaranteed.
ExtDouble::toDouble
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/ExtDouble.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/ExtDouble.java
Apache-2.0
public AiffTag read(Path file) throws CannotReadException, IOException { try(FileChannel fc = FileChannel.open(file)) { AiffAudioHeader aiffAudioHeader = new AiffAudioHeader(); AiffTag aiffTag = new AiffTag(); final AiffFileHeader fileHeader = new AiffFileHeader(); fileHeader.readHeader(fc, aiffAudioHeader, file.toString()); while (fc.position() < fc.size()) { if (!readChunk(fc, aiffTag, file.toString())) { logger.severe(file + " UnableToReadProcessChunk"); break; } } if (aiffTag.getID3Tag() == null) { aiffTag.setID3Tag(AiffTag.createDefaultID3Tag()); } return aiffTag; } }
Read editable Metadata @param file @return @throws CannotReadException @throws IOException
AiffTagReader::read
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagReader.java
Apache-2.0
private boolean readChunk(FileChannel fc, AiffTag aiffTag, String fileName) throws IOException { logger.config(fileName + " Reading Tag Chunk"); ChunkHeader chunkHeader = new ChunkHeader(ByteOrder.BIG_ENDIAN); if (!chunkHeader.readHeader(fc)) { return false; } logger.config(fileName + " Reading Chunk:" + chunkHeader.getID() + ":starting at:" + Hex.asDecAndHex(chunkHeader.getStartLocationInFile()) + ":sizeIncHeader:" + (chunkHeader.getSize() + ChunkHeader.CHUNK_HEADER_SIZE)); long startLocationOfId3TagInFile = fc.position(); AiffChunkType chunkType = AiffChunkType.get(chunkHeader.getID()); if (chunkType!=null && chunkType== AiffChunkType.TAG && chunkHeader.getSize() > 0) { ByteBuffer chunkData = readChunkDataIntoBuffer(fc, chunkHeader); aiffTag.addChunkSummary(new ChunkSummary(chunkHeader.getID(), chunkHeader.getStartLocationInFile(), chunkHeader.getSize())); //If we havent already for an ID3 Tag if(aiffTag.getID3Tag()==null) { Chunk chunk = new ID3Chunk(chunkHeader,chunkData, aiffTag); chunk.readChunk(); aiffTag.setExistingId3Tag(true); aiffTag.getID3Tag().setStartLocationInFile(startLocationOfId3TagInFile); aiffTag.getID3Tag().setEndLocationInFile(fc.position()); } //else otherwise we discard because the first one found is the one that will be used by other apps { logger.warning(fileName + " Ignoring ID3Tag because already have one:" + chunkHeader.getID() + ":" + chunkHeader.getStartLocationInFile() + Hex.asDecAndHex(chunkHeader.getStartLocationInFile() - 1) + ":sizeIncHeader:" + (chunkHeader.getSize() + ChunkHeader.CHUNK_HEADER_SIZE)); } } //Special handling to recognise ID3Tags written on odd boundary because original preceding chunk odd length but //didn't write padding byte else if(chunkType!=null && chunkType== AiffChunkType.CORRUPT_TAG_LATE) { logger.warning(fileName + "Found Corrupt ID3 Chunk, starting at Odd Location:" + chunkHeader.getID() + ":" + Hex.asDecAndHex(chunkHeader.getStartLocationInFile() - 1) + ":sizeIncHeader:"+ (chunkHeader.getSize() + ChunkHeader.CHUNK_HEADER_SIZE)); //We only want to know if first metadata tag is misaligned if(aiffTag.getID3Tag()==null) { aiffTag.setIncorrectlyAlignedTag(true); } fc.position(fc.position() - (ChunkHeader.CHUNK_HEADER_SIZE + 1)); return true; } //Other Special handling for ID3Tags else if(chunkType!=null && chunkType== AiffChunkType.CORRUPT_TAG_EARLY) { logger.warning(fileName + " Found Corrupt ID3 Chunk, starting at Odd Location:" + chunkHeader.getID() + ":" + Hex.asDecAndHex(chunkHeader.getStartLocationInFile()) + ":sizeIncHeader:"+ (chunkHeader.getSize() + ChunkHeader.CHUNK_HEADER_SIZE)); //We only want to know if first metadata tag is misaligned if(aiffTag.getID3Tag()==null) { aiffTag.setIncorrectlyAlignedTag(true); } fc.position(fc.position() - (ChunkHeader.CHUNK_HEADER_SIZE - 1)); return true; } else { logger.config(fileName + "Skipping Chunk:" + chunkHeader.getID() + ":" + chunkHeader.getSize()); aiffTag.addChunkSummary(new ChunkSummary(chunkHeader.getID(), chunkHeader.getStartLocationInFile(), chunkHeader.getSize())); fc.position(fc.position() + chunkHeader.getSize()); } IffHeaderChunk.ensureOnEqualBoundary(fc, chunkHeader); return true; }
Reads an AIFF ID3 Chunk. @return {@code false}, if we were not able to read a valid chunk id
AiffTagReader::readChunk
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagReader.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagReader.java
Apache-2.0
private AiffTag getExistingMetadata(Path file) throws IOException, CannotWriteException { try { //Find AiffTag (if any) AiffTagReader im = new AiffTagReader(); return im.read(file); } catch (CannotReadException ex) { throw new CannotWriteException(file + " Failed to read file"); } }
Read existing metadata @param file @return tags within Tag wrapper @throws IOException @throws CannotWriteException
AiffTagWriter::getExistingMetadata
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0
private ChunkHeader seekToStartOfMetadata(FileChannel fc, AiffTag existingTag, String fileName) throws IOException, CannotWriteException { fc.position(existingTag.getStartLocationInFileOfId3Chunk()); final ChunkHeader chunkHeader = new ChunkHeader(ByteOrder.BIG_ENDIAN); chunkHeader.readHeader(fc); fc.position(fc.position() - ChunkHeader.CHUNK_HEADER_SIZE); if(!AiffChunkType.TAG.getCode().equals(chunkHeader.getID())) { throw new CannotWriteException(fileName + " Unable to find ID3 chunk at expected location:"+existingTag.getStartLocationInFileOfId3Chunk()); } return chunkHeader; }
Seek in file to start of LIST Metadata chunk @param fc @param existingTag @throws IOException @throws CannotWriteException
AiffTagWriter::seekToStartOfMetadata
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0
private boolean isAtEndOfFileAllowingForPaddingByte(AiffTag existingTag, FileChannel fc) throws IOException { return ( ( existingTag.getID3Tag().getEndLocationInFile() == fc.size() ) || ( Utils.isOddLength(existingTag.getID3Tag().getEndLocationInFile()) && existingTag.getID3Tag().getEndLocationInFile() + 1 == fc.size() ) ); }
@param existingTag @param fc @return true if at end of file (also take into account padding byte) @throws IOException
AiffTagWriter::isAtEndOfFileAllowingForPaddingByte
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0
public void delete(final Tag tag, Path file) throws CannotWriteException { try(FileChannel fc = FileChannel.open(file, StandardOpenOption.WRITE, StandardOpenOption.READ)) { logger.severe(file +" Deleting tag from file"); final AiffTag existingTag = getExistingMetadata(file); if (existingTag.isExistingId3Tag() && existingTag.getID3Tag().getStartLocationInFile() != null) { ChunkHeader chunkHeader = seekToStartOfMetadata(fc, existingTag, file.toString()); if (isAtEndOfFileAllowingForPaddingByte(existingTag, fc)) { logger.severe(file + " Setting new length to:" + (existingTag.getStartLocationInFileOfId3Chunk())); fc.truncate(existingTag.getStartLocationInFileOfId3Chunk()); } else { logger.severe(file + " Deleting tag chunk"); deleteTagChunk(fc, existingTag, chunkHeader,file.toString()); } rewriteRiffHeaderSize(fc); } logger.severe(file + " Deleted tag from file"); } catch(IOException ioe) { throw new CannotWriteException(file + ":" + ioe.getMessage()); } }
Delete given {@link Tag} from file. @param tag tag, must be instance of {@link AiffTag} @param file @throws java.io.IOException @throws org.jaudiotagger.audio.exceptions.CannotWriteException
AiffTagWriter::delete
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0
private void deleteTagChunk(FileChannel fc, final AiffTag existingTag, final ChunkHeader tagChunkHeader, String fileName) throws IOException { int lengthTagChunk = (int) tagChunkHeader.getSize() + ChunkHeader.CHUNK_HEADER_SIZE; if(Utils.isOddLength(lengthTagChunk)) { if(existingTag.getStartLocationInFileOfId3Chunk() + lengthTagChunk <fc.size()) { lengthTagChunk++; } } final long newLength = fc.size() - lengthTagChunk; logger.severe(fileName + " Size of id3 chunk to delete is:"+lengthTagChunk+":Location:"+existingTag.getStartLocationInFileOfId3Chunk()); // position for reading after the id3 tag fc.position(existingTag.getStartLocationInFileOfId3Chunk() + lengthTagChunk); deleteTagChunkUsingSmallByteBufferSegments(existingTag, fc, newLength, lengthTagChunk); // truncate the file after the last chunk logger.severe(fileName + " Setting new length to:" + newLength); fc.truncate(newLength); }
<p>Deletes the given ID3-{@link Tag}/{@link Chunk} from the file by moving all following chunks up.</p> <pre> [chunk][-id3-][chunk][chunk] [chunk] &lt;&lt;--- [chunk][chunk] [chunk][chunk][chunk] </pre> @param fc, filechannel @param existingTag existing tag @param tagChunkHeader existing chunk header for the tag @throws IOException if something goes wrong
AiffTagWriter::deleteTagChunk
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0
private void deleteRemainderOfFile(FileChannel fc, final AiffTag existingTag, String fileName) throws IOException { ChunkSummary precedingChunk = AiffChunkSummary.getChunkBeforeStartingMetadataTag(existingTag); if(!Utils.isOddLength(precedingChunk.getEndLocation())) { logger.severe(fileName + " Truncating corrupted ID3 tags from:" + (existingTag.getStartLocationInFileOfId3Chunk() - 1)); fc.truncate(existingTag.getStartLocationInFileOfId3Chunk() - 1); } else { logger.severe(fileName + " Truncating corrupted ID3 tags from:" + (existingTag.getStartLocationInFileOfId3Chunk())); fc.truncate(existingTag.getStartLocationInFileOfId3Chunk()); } }
If Metadata tags are corrupted and no other tags later in the file then just truncate ID3 tags and start again @param fc @param existingTag @throws IOException
AiffTagWriter::deleteRemainderOfFile
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0
private void deleteTagChunkUsingChannelTransfer(final AiffTag existingTag, final FileChannel channel, final long newLength) throws IOException { long read; //Read from just after the ID3Chunk into the channel at where the ID3 chunk started, should usually only require one transfer //but put into loop in case multiple calls are required for (long position = existingTag.getStartLocationInFileOfId3Chunk(); (read = channel.transferFrom(channel, position, newLength - position)) < newLength-position; position += read);//is this problem if loop called more than once do we need to update position of channel to modify //where write to ? }
The following seems to work on Windows but hangs on OSX! Bug is filed <a href="https://bugs.openjdk.java.net/browse/JDK-8140241">here</a>. @param existingTag existing tag @param channel channel @param newLength new length @throws IOException if something goes wrong
AiffTagWriter::deleteTagChunkUsingChannelTransfer
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0
public void write(final Tag tag, Path file) throws CannotWriteException { logger.severe(file + " Writing Aiff tag to file"); AiffTag existingTag = null; try { existingTag = getExistingMetadata(file); } catch(IOException ioe) { throw new CannotWriteException(file + ":" + ioe.getMessage()); } try(FileChannel fc = FileChannel.open(file, StandardOpenOption.WRITE, StandardOpenOption.READ)) { long existingFileLength = fc.size(); final AiffTag aiffTag = (AiffTag) tag; final ByteBuffer bb = convert(aiffTag, existingTag); //Replacing ID3 tag if (existingTag.isExistingId3Tag() && existingTag.getID3Tag().getStartLocationInFile() != null) { //Usual case if (!existingTag.isIncorrectlyAlignedTag()) { final ChunkHeader chunkHeader = seekToStartOfMetadata(fc, existingTag, file.toString()); logger.info(file + "Current Space allocated:" + existingTag.getSizeOfID3TagOnly() + ":NewTagRequires:" + bb.limit()); //Usual case ID3 is last chunk if (isAtEndOfFileAllowingForPaddingByte(existingTag, fc)) { writeDataToFile(fc, bb); } //Unusual Case where ID3 is not last chunk else { deleteTagChunk(fc, existingTag, chunkHeader, file.toString()); fc.position(fc.size()); writeExtraByteIfChunkOddSize(fc, fc.size()); writeDataToFile(fc, bb); } } //Existing ID3 tag is incorrectly aligned so if we can lets delete it and any subsequentially added //ID3 tags as we only want one ID3 tag. else if (AiffChunkSummary.isOnlyMetadataTagsAfterStartingMetadataTag(existingTag)) { deleteRemainderOfFile(fc, existingTag, file.toString()); fc.position(fc.size()); writeExtraByteIfChunkOddSize(fc, fc.size()); writeDataToFile(fc, bb); } else { throw new CannotWriteException(file + " Metadata tags are corrupted and not at end of file so cannot be fixed"); } } //New Tag else { fc.position(fc.size()); if (Utils.isOddLength(fc.size())) { fc.write(ByteBuffer.allocateDirect(1)); } writeDataToFile(fc, bb); } if (existingFileLength != fc.size()) { rewriteRiffHeaderSize(fc); } } catch(AccessDeniedException ade) { throw new NoWritePermissionsException(file + ":" + ade.getMessage()); } catch(IOException ioe) { throw new CannotWriteException(file + ":" + ioe.getMessage()); } }
@param tag @param file @throws CannotWriteException @throws IOException
AiffTagWriter::write
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0
private void rewriteRiffHeaderSize(FileChannel fc) throws IOException { fc.position(IffHeaderChunk.SIGNATURE_LENGTH); ByteBuffer bb = ByteBuffer.allocateDirect(IffHeaderChunk.SIZE_LENGTH); bb.order(ByteOrder.BIG_ENDIAN); int size = ((int) fc.size()) - SIGNATURE_LENGTH - SIZE_LENGTH; bb.putInt(size); bb.flip(); fc.write(bb); }
Rewrite RAF header to reflect new file length @param fc @throws IOException
AiffTagWriter::rewriteRiffHeaderSize
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0
private void writeDataToFile(FileChannel fc, final ByteBuffer bb) throws IOException { final ChunkHeader ch = new ChunkHeader(ByteOrder.BIG_ENDIAN); ch.setID(AiffChunkType.TAG.getCode()); ch.setSize(bb.limit()); fc.write(ch.writeHeader()); fc.write(bb); writeExtraByteIfChunkOddSize(fc, bb.limit() ); }
Writes data as a {@link org.jaudiotagger.audio.aiff.chunk.AiffChunkType#TAG} chunk to the file. @param fc filechannel @param bb data to write @throws IOException
AiffTagWriter::writeDataToFile
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0
private void writeExtraByteIfChunkOddSize(FileChannel fc, long size ) throws IOException { if(Utils.isOddLength(size)) { fc.write(ByteBuffer.allocateDirect(1)); } }
Chunk must also start on an even byte so if our chunksize is odd we need to write another byte. This should never happen as ID3Tag is now amended to ensure always write padding byte if needed to stop it being odd sized but we keep check in just incase. @param fc @param size @throws IOException
AiffTagWriter::writeExtraByteIfChunkOddSize
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0
public ByteBuffer convert(final AiffTag tag, AiffTag existingTag) throws UnsupportedEncodingException { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); long existingTagSize = existingTag.getSizeOfID3TagOnly(); //If existingTag is uneven size lets make it even if( existingTagSize > 0) { if((existingTagSize & 1)!=0) { existingTagSize++; } } //Write Tag to buffer tag.getID3Tag().write(baos, (int)existingTagSize); //If the tag is now odd because we needed to increase size and the data made it odd sized //we redo adding a padding byte to make it even if((baos.toByteArray().length & 1)!=0) { int newSize = baos.toByteArray().length + 1; baos = new ByteArrayOutputStream(); tag.getID3Tag().write(baos, newSize); } final ByteBuffer buf = ByteBuffer.wrap(baos.toByteArray()); buf.rewind(); return buf; } catch (IOException ioe) { //Should never happen as not writing to file at this point throw new RuntimeException(ioe); } }
Converts tag to {@link ByteBuffer}. @param tag tag @param existingTag @return byte buffer containing the tag data @throws UnsupportedEncodingException
AiffTagWriter::convert
java
ZTFtrue/MonsterMusic
app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/aiff/AiffTagWriter.java
Apache-2.0