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 |
---|---|---|---|---|---|---|---|
protected boolean readChunk(FileChannel fc, WavTag tag)throws IOException, CannotReadException
{
Chunk chunk;
ChunkHeader chunkHeader = new ChunkHeader(ByteOrder.LITTLE_ENDIAN);
if (!chunkHeader.readHeader(fc))
{
return false;
}
String id = chunkHeader.getID();
logger.config(loggingName + " Next Id is:" + id + ":FileLocation:" + fc.position() + ":Size:" + chunkHeader.getSize());
final WavChunkType chunkType = WavChunkType.get(id);
if (chunkType != null)
{
switch (chunkType)
{
case LIST:
tag.addChunkSummary(new ChunkSummary(chunkHeader.getID(), chunkHeader.getStartLocationInFile(), chunkHeader.getSize()));
if(tag.getInfoTag()==null)
{
chunk = new WavListChunk(loggingName, Utils.readFileDataIntoBufferLE(fc, (int) chunkHeader.getSize()), chunkHeader, tag);
if (!chunk.readChunk())
{
return false;
}
}
else
{
logger.warning(loggingName + " Ignoring LIST chunk because already have one:" + chunkHeader.getID()
+ ":" + Hex.asDecAndHex(chunkHeader.getStartLocationInFile() - 1)
+ ":sizeIncHeader:"+ (chunkHeader.getSize() + ChunkHeader.CHUNK_HEADER_SIZE));
}
break;
case CORRUPT_LIST:
logger.severe(loggingName + " Found Corrupt LIST Chunk, starting at Odd Location:"+chunkHeader.getID()+":"+chunkHeader.getSize());
if(tag.getInfoTag()==null && tag.getID3Tag() == null)
{
tag.setIncorrectlyAlignedTag(true);
}
fc.position(fc.position() - (ChunkHeader.CHUNK_HEADER_SIZE - 1));
return true;
case ID3:
tag.addChunkSummary(new ChunkSummary(chunkHeader.getID(), chunkHeader.getStartLocationInFile(), chunkHeader.getSize()));
if(tag.getID3Tag()==null)
{
chunk = new WavId3Chunk(Utils.readFileDataIntoBufferLE(fc, (int) chunkHeader.getSize()), chunkHeader, tag);
if (!chunk.readChunk())
{
return false;
}
}
else
{
logger.warning(loggingName + " Ignoring id3 chunk because already have one:" + chunkHeader.getID() + ":"
+ Hex.asDecAndHex(chunkHeader.getStartLocationInFile())
+ ":sizeIncHeader:"+ (chunkHeader.getSize() + ChunkHeader.CHUNK_HEADER_SIZE));
}
break;
case CORRUPT_ID3_EARLY:
logger.severe(loggingName + " Found Corrupt id3 chunk, starting at Odd Location:"+chunkHeader.getID()+":"+chunkHeader.getSize());
if(tag.getInfoTag()==null && tag.getID3Tag() == null)
{
tag.setIncorrectlyAlignedTag(true);
}
fc.position(fc.position() - (ChunkHeader.CHUNK_HEADER_SIZE - 1));
return true;
case CORRUPT_ID3_LATE:
logger.severe(loggingName + " Found Corrupt id3 chunk, starting at Odd Location:"+chunkHeader.getID()+":"+chunkHeader.getSize());
if(tag.getInfoTag()==null && tag.getID3Tag() == null)
{
tag.setIncorrectlyAlignedTag(true);
}
fc.position(fc.position() - (ChunkHeader.CHUNK_HEADER_SIZE - 1));
return true;
default:
tag.addChunkSummary(new ChunkSummary(chunkHeader.getID(), chunkHeader.getStartLocationInFile(), chunkHeader.getSize()));
fc.position(fc.position() + chunkHeader.getSize());
}
}
//Unknown chunk type just skip
else
{
if(chunkHeader.getSize() < 0)
{
String msg = loggingName + " Not a valid header, unable to read a sensible size:Header"
+ chunkHeader.getID()+"Size:"+chunkHeader.getSize();
logger.severe(msg);
throw new CannotReadException(msg);
}
logger.config(loggingName + " Skipping chunk bytes:" + chunkHeader.getSize() +"for"+chunkHeader.getID());
fc.position(fc.position() + chunkHeader.getSize());
if(fc.position()>fc.size())
{
String msg = loggingName + " Failed to move to invalid position to " + fc.position() + " because file length is only " + fc.size()
+ " indicates invalid chunk";
logger.severe(msg);
throw new CannotReadException(msg);
}
}
IffHeaderChunk.ensureOnEqualBoundary(fc, chunkHeader);
return true;
} |
Reads Wavs Chunk that contain tag metadata
If the same chunk exists more than once in the file we would just use the last occurence
@param tag
@return
@throws IOException
| WavTagReader::readChunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagReader.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagReader.java | Apache-2.0 |
private WavTag getExistingMetadata(Path path) throws IOException, CannotWriteException
{
try
{
//Find WavTag (if any)
WavTagReader im = new WavTagReader(loggingName);
return im.read(path);
}
catch (CannotReadException ex)
{
throw new CannotWriteException("Failed to read file "+path);
}
} |
Read existing metadata
@param path
@return tags within Tag wrapper
@throws IOException
@throws CannotWriteException
| WavTagWriter::getExistingMetadata | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private ChunkHeader seekToStartOfListInfoMetadata(FileChannel fc, WavTag existingTag) throws IOException, CannotWriteException
{
fc.position(existingTag.getInfoTag().getStartLocationInFile());
final ChunkHeader chunkHeader = new ChunkHeader(ByteOrder.LITTLE_ENDIAN);
chunkHeader.readHeader(fc);
fc.position(fc.position() - ChunkHeader.CHUNK_HEADER_SIZE);
if (!WavChunkType.LIST.getCode().equals(chunkHeader.getID()))
{
throw new CannotWriteException(loggingName +" Unable to find List chunk at original location has file been modified externally");
}
return chunkHeader;
} |
Seek in file to start of LIST Metadata chunk
@param fc
@param existingTag
@throws IOException
@throws CannotWriteException
| WavTagWriter::seekToStartOfListInfoMetadata | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private ChunkHeader seekToStartOfId3Metadata(FileChannel fc, WavTag existingTag) throws IOException, CannotWriteException
{
fc.position(existingTag.getStartLocationInFileOfId3Chunk());
final ChunkHeader chunkHeader = new ChunkHeader(ByteOrder.LITTLE_ENDIAN);
chunkHeader.readHeader(fc);
fc.position(fc.position() - ChunkHeader.CHUNK_HEADER_SIZE);
if (!WavChunkType.ID3.getCode().equals(chunkHeader.getID()))
{
throw new CannotWriteException(loggingName + " Unable to find ID3 chunk at original location has file been modified externally");
}
return chunkHeader;
} |
Seek in file to start of Id3 Metadata chunk
@param fc
@param existingTag
@throws IOException
@throws CannotWriteException
| WavTagWriter::seekToStartOfId3Metadata | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
public void delete (Tag tag, Path file) throws CannotWriteException
{
logger.info(loggingName + " Deleting metadata from file");
try(FileChannel fc = FileChannel.open(file, StandardOpenOption.WRITE, StandardOpenOption.READ))
{
WavTag existingTag = getExistingMetadata(file);
//have both tags
if (existingTag.isExistingId3Tag() && existingTag.isExistingInfoTag())
{
BothTagsFileStructure fs = checkExistingLocations(existingTag, fc);
//We can delete both chunks in one go
if (fs.isContiguous)
{
//Quick method
if (fs.isAtEnd)
{
if (fs.isInfoTagFirst)
{
logger.info(loggingName + ":Setting new length to:" + existingTag.getInfoTag().getStartLocationInFile());
fc.truncate(existingTag.getInfoTag().getStartLocationInFile());
}
else
{
logger.info(loggingName + ":Setting new length to:" + existingTag.getStartLocationInFileOfId3Chunk());
fc.truncate(existingTag.getStartLocationInFileOfId3Chunk());
}
}
//Slower
else
{
if (fs.isInfoTagFirst)
{
final int lengthTagChunk = (int) (existingTag.getEndLocationInFileOfId3Chunk() - existingTag.getInfoTag().getStartLocationInFile());
deleteTagChunk(fc, (int) existingTag.getEndLocationInFileOfId3Chunk(), lengthTagChunk);
}
else
{
final int lengthTagChunk = (int) (existingTag.getInfoTag().getEndLocationInFile().intValue() - existingTag.getStartLocationInFileOfId3Chunk());
deleteTagChunk(fc, existingTag.getInfoTag().getEndLocationInFile().intValue(), lengthTagChunk);
}
}
}
//Tricky to delete both because once one is deleted affects the location of the other
else
{
WavInfoTag existingInfoTag = existingTag.getInfoTag();
ChunkHeader infoChunkHeader = seekToStartOfListInfoMetadata(fc, existingTag);
ChunkHeader id3ChunkHeader = seekToStartOfId3Metadata(fc, existingTag);
//If one of these two at end of file delete first then remove the other as a chunk
if (isInfoTagAtEndOfFileAllowingForPaddingByte(existingTag, fc))
{
fc.truncate(existingInfoTag.getStartLocationInFile());
deleteId3TagChunk(fc, existingTag, id3ChunkHeader);
}
else if (isID3TagAtEndOfFileAllowingForPaddingByte(existingTag, fc))
{
fc.truncate(existingTag.getStartLocationInFileOfId3Chunk());
deleteInfoTagChunk(fc, existingTag, infoChunkHeader);
}
else
{
deleteId3TagChunk(fc, existingTag, id3ChunkHeader);
//Reread then delete other tag
existingTag = getExistingMetadata(file);
deleteInfoTagChunk(fc, existingTag, infoChunkHeader);
}
}
}
//Delete Info if exists
else if (existingTag.isExistingInfoTag())
{
WavInfoTag existingInfoTag = existingTag.getInfoTag();
ChunkHeader chunkHeader = seekToStartOfListInfoMetadata(fc, existingTag);
//and it is at end of the file
if (existingInfoTag.getEndLocationInFile() == fc.size())
{
logger.info(loggingName + ":Setting new length to:" + existingInfoTag.getStartLocationInFile());
fc.truncate(existingInfoTag.getStartLocationInFile());
}
else
{
deleteInfoTagChunk(fc, existingTag, chunkHeader);
}
}
else if (existingTag.isExistingId3Tag())
{
ChunkHeader chunkHeader = seekToStartOfId3Metadata(fc, existingTag);
//and it is at end of the file
if (isID3TagAtEndOfFileAllowingForPaddingByte(existingTag, fc))
{
logger.info(loggingName + ":Setting new length to:" + existingTag.getStartLocationInFileOfId3Chunk());
fc.truncate(existingTag.getStartLocationInFileOfId3Chunk());
}
else
{
deleteId3TagChunk(fc, existingTag, chunkHeader);
}
}
else
{
//Nothing to delete
}
rewriteRiffHeaderSize(fc);
}
catch(IOException ioe)
{
throw new CannotWriteException(file + ":" + ioe.getMessage());
}
} |
Delete any existing metadata tags from files
@param tag
@param file
@throws IOException
@throws CannotWriteException
| WavTagWriter::delete | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void deleteInfoTagChunk(final FileChannel fc, final WavTag existingTag, final ChunkHeader chunkHeader) throws IOException
{
final WavInfoTag existingInfoTag = existingTag.getInfoTag();
final int lengthTagChunk = (int) chunkHeader.getSize() + ChunkHeader.CHUNK_HEADER_SIZE;
deleteTagChunk(fc, existingInfoTag.getEndLocationInFile().intValue(), lengthTagChunk);
} |
Delete existing Info Tag
@param fc
@param existingTag
@param chunkHeader
@throws IOException
| WavTagWriter::deleteInfoTagChunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void deleteId3TagChunk(FileChannel fc, final WavTag existingTag, final ChunkHeader chunkHeader) throws IOException
{
final int lengthTagChunk = (int) chunkHeader.getSize() + ChunkHeader.CHUNK_HEADER_SIZE;
deleteTagChunk(fc, (int) existingTag.getEndLocationInFileOfId3Chunk(), lengthTagChunk);
} |
Delete existing Id3 Tag
@param fc
@param existingTag
@param chunkHeader
@throws IOException
| WavTagWriter::deleteId3TagChunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void deleteTagChunk(final FileChannel fc, int endOfExistingChunk, final int lengthTagChunk) throws IOException
{
//Position for reading after the tag
fc.position(endOfExistingChunk);
final ByteBuffer buffer = ByteBuffer.allocate((int) TagOptionSingleton.getInstance().getWriteChunkSize());
while (fc.read(buffer) >= 0 || buffer.position() != 0)
{
buffer.flip();
final long readPosition = fc.position();
fc.position(readPosition - lengthTagChunk - buffer.limit());
fc.write(buffer);
fc.position(readPosition);
buffer.compact();
}
//Truncate the file after the last chunk
final long newLength = fc.size() - lengthTagChunk;
logger.config(loggingName + " Setting new length to:" + newLength);
fc.truncate(newLength);
} |
Delete Tag Chunk
<p/>
Can be used when chunk is not the last chunk
<p/>
Continually copy a 4mb chunk, write the chunk and repeat until the rest of the file after the tag
is rewritten
@param fc
@param endOfExistingChunk
@param lengthTagChunk
@throws IOException
| WavTagWriter::deleteTagChunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
public void write(final Tag tag, Path file) throws CannotWriteException
{
logger.config(loggingName + " Writing tag to file:start");
WavSaveOptions wso = TagOptionSingleton.getInstance().getWavSaveOptions();
WavTag 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))
{
final WavTag wavTag = (WavTag) tag;
if (wso == WavSaveOptions.SAVE_BOTH)
{
saveBoth(wavTag, fc, existingTag);
}
else if (wso == WavSaveOptions.SAVE_ACTIVE)
{
saveActive(wavTag, fc, existingTag);
}
else if (wso == WavSaveOptions.SAVE_EXISTING_AND_ACTIVE)
{
saveActiveExisting(wavTag, fc, existingTag);
}
else if (wso == WavSaveOptions.SAVE_BOTH_AND_SYNC)
{
wavTag.syncTagBeforeWrite();
saveBoth(wavTag, fc, existingTag);
}
else if (wso == WavSaveOptions.SAVE_EXISTING_AND_ACTIVE_AND_SYNC)
{
wavTag.syncTagBeforeWrite();
saveActiveExisting(wavTag, fc, existingTag);
}
//Invalid Option, should never happen
else
{
throw new RuntimeException(loggingName + " No setting for:WavSaveOptions");
}
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
| WavTagWriter::write | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.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.LITTLE_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 size
@param fc
@throws IOException
| WavTagWriter::rewriteRiffHeaderSize | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void writeInfoDataToFile(FileChannel fc, final ByteBuffer bb, final long chunkSize) throws IOException
{
if (Utils.isOddLength(fc.position()))
{
writePaddingToFile(fc, 1);
}
//Write LIST header
final ByteBuffer listHeaderBuffer = ByteBuffer.allocate(ChunkHeader.CHUNK_HEADER_SIZE);
listHeaderBuffer.order(ByteOrder.LITTLE_ENDIAN);
listHeaderBuffer.put(WavChunkType.LIST.getCode().getBytes(StandardCharsets.US_ASCII));
listHeaderBuffer.putInt((int) chunkSize);
listHeaderBuffer.flip();
fc.write(listHeaderBuffer);
//Now write actual data
fc.write(bb);
writeExtraByteIfChunkOddSize(fc, chunkSize);
} |
Write LISTINFOChunk of specified size to current file location
ensuring it is on even file boundary
@param fc random access file
@param bb data to write
@param chunkSize chunk size
@throws java.io.IOException
| WavTagWriter::writeInfoDataToFile | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void writeInfoDataToFile(final FileChannel fc, final ByteBuffer bb) throws IOException
{
writeInfoDataToFile(fc, bb, bb.limit());
} |
Write new Info chunk and dont worry about the size of existing chunk just use size of new chunk
@param fc
@param bb
@throws IOException
| WavTagWriter::writeInfoDataToFile | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void writeID3DataToFile(final FileChannel fc, final ByteBuffer bb) throws IOException
{
if(Utils.isOddLength(fc.position()))
{
writePaddingToFile(fc, 1);
}
//Write ID3Data header
final ByteBuffer listBuffer = ByteBuffer.allocate(ChunkHeader.CHUNK_HEADER_SIZE);
listBuffer.order(ByteOrder.LITTLE_ENDIAN);
listBuffer.put(WavChunkType.ID3.getCode().getBytes(StandardCharsets.US_ASCII));
listBuffer.putInt(bb.limit());
listBuffer.flip();
fc.write(listBuffer);
//Now write actual data
fc.write(bb);
} |
Write Id3Chunk of specified size to current file location
ensuring it is on even file boundary
@param fc random access file
@param bb data to write
@throws java.io.IOException
| WavTagWriter::writeID3DataToFile | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void writePaddingToFile(final FileChannel fc, final int paddingSize) throws IOException
{
fc.write(ByteBuffer.allocateDirect(paddingSize));
} |
Write Padding bytes
@param fc
@param paddingSize
@throws IOException
| WavTagWriter::writePaddingToFile | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
public ByteBuffer convertInfoChunk(final WavTag tag) throws UnsupportedEncodingException
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
WavInfoTag wif = tag.getInfoTag();
//Write the Info chunks
List<TagField> fields = wif.getAll();
Collections.sort(fields, new InfoFieldWriterOrderComparator());
for(TagField nextField:fields)
{
TagTextField next = (TagTextField) nextField;
WavInfoIdentifier wii = WavInfoIdentifier.getByByFieldKey(FieldKey.valueOf(next.getId()));
baos.write(wii.getCode().getBytes(StandardCharsets.US_ASCII));
logger.config(loggingName + " Writing:" + wii.getCode() + ":" + next.getContent());
//TODO Is UTF8 allowed format
byte[] contentConvertedToBytes = next.getContent().getBytes(StandardCharsets.UTF_8);
baos.write(Utils.getSizeLEInt32(contentConvertedToBytes.length));
baos.write(contentConvertedToBytes);
//Write extra byte if data length not equal
if (Utils.isOddLength(contentConvertedToBytes.length))
{
baos.write(0);
}
//Add a duplicated record for Twonky
if(wii==WavInfoIdentifier.TRACKNO)
{
if(TagOptionSingleton.getInstance().isWriteWavForTwonky())
{
baos.write(WavInfoIdentifier.TWONKY_TRACKNO.getCode().getBytes(StandardCharsets.US_ASCII));
logger.config(loggingName + " Writing:" + WavInfoIdentifier.TWONKY_TRACKNO.getCode() + ":" + next.getContent());
baos.write(Utils.getSizeLEInt32(contentConvertedToBytes.length));
baos.write(contentConvertedToBytes);
//Write extra byte if data length not equal
if (Utils.isOddLength(contentConvertedToBytes.length))
{
baos.write(0);
}
}
}
}
//Write any existing unrecognized tuples
Iterator<TagTextField> ti = wif.getUnrecognisedFields().iterator();
while(ti.hasNext())
{
TagTextField next = ti.next();
baos.write(next.getId().getBytes(StandardCharsets.US_ASCII));
logger.config(loggingName + " Writing:" +next.getId() + ":" + next.getContent());
byte[] contentConvertedToBytes = next.getContent().getBytes(StandardCharsets.UTF_8);
baos.write(Utils.getSizeLEInt32(contentConvertedToBytes.length));
baos.write(contentConvertedToBytes);
//Write extra byte if data length not equal
if (Utils.isOddLength(contentConvertedToBytes.length))
{
baos.write(0);
}
}
final ByteBuffer infoBuffer = ByteBuffer.wrap(baos.toByteArray());
infoBuffer.rewind();
//Now Write INFO header
final ByteBuffer infoHeaderBuffer = ByteBuffer.allocate(SIGNATURE_LENGTH);
infoHeaderBuffer.put(WavChunkType.INFO.getCode().getBytes(StandardCharsets.US_ASCII));
infoHeaderBuffer.flip();
//Construct a single ByteBuffer from both
ByteBuffer listInfoBuffer = ByteBuffer.allocateDirect(infoHeaderBuffer.limit() + infoBuffer.limit());
listInfoBuffer.put(infoHeaderBuffer);
listInfoBuffer.put(infoBuffer);
listInfoBuffer.flip();
return listInfoBuffer;
}
catch (IOException ioe)
{
//Should never happen as not writing to file at this point
throw new RuntimeException(ioe);
}
} |
Converts INfO tag to {@link java.nio.ByteBuffer}.
@param tag tag
@return byte buffer containing the tag data
@throws java.io.UnsupportedEncodingException
| InfoFieldWriterOrderComparator::convertInfoChunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
public ByteBuffer convertID3Chunk(final WavTag tag, WavTag 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 ID3tag to {@link ByteBuffer}.
@param tag tag containing ID3tag
@return byte buffer containing the tag data
@throws UnsupportedEncodingException
| InfoFieldWriterOrderComparator::convertID3Chunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private BothTagsFileStructure checkExistingLocations(WavTag wavTag, FileChannel fc) throws IOException
{
BothTagsFileStructure fs = new BothTagsFileStructure();
if(wavTag.getInfoTag().getStartLocationInFile() < wavTag.getID3Tag().getStartLocationInFile())
{
fs.isInfoTagFirst = true;
//Must allow for odd size chunks
if(Math.abs(wavTag.getInfoTag().getEndLocationInFile() - wavTag.getStartLocationInFileOfId3Chunk()) <=1)
{
fs.isContiguous = true;
if(isID3TagAtEndOfFileAllowingForPaddingByte(wavTag, fc))
{
fs.isAtEnd = true;
}
}
}
else
{
//Must allow for odd size chunks
if(Math.abs(wavTag.getID3Tag().getEndLocationInFile() - wavTag.getInfoTag().getStartLocationInFile()) <=1)
{
fs.isContiguous = true;
if(isInfoTagAtEndOfFileAllowingForPaddingByte(wavTag, fc))
{
fs.isAtEnd = true;
}
}
}
return fs;
} |
Identify where both metadata chunks are in relation to each other and other chunks
@param wavTag
@param fc
@return
@throws IOException
| BothTagsFileStructure::checkExistingLocations | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void writeInfoChunk(FileChannel fc, final WavInfoTag existingInfoTag, ByteBuffer newTagBuffer)
throws CannotWriteException, IOException
{
long newInfoTagSize = newTagBuffer.limit();
//We have enough existing space in chunk so just keep existing chunk size
if (existingInfoTag.getSizeOfTag() >= newInfoTagSize)
{
writeInfoDataToFile(fc, newTagBuffer, existingInfoTag.getSizeOfTag());
//To ensure old data from previous tag are erased
if (existingInfoTag.getSizeOfTag() > newInfoTagSize)
{
writePaddingToFile(fc, (int) (existingInfoTag.getSizeOfTag() - newInfoTagSize));
}
}
//New tag is larger so set chunk size to accommodate it
else
{
writeInfoDataToFile(fc, newTagBuffer, newInfoTagSize);
}
} |
Write Info chunk to current location which is last chunk of file
@param fc
@param existingInfoTag
@param newTagBuffer
@throws CannotWriteException
@throws IOException
| BothTagsFileStructure::writeInfoChunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void writeExtraByteIfChunkOddSize(FileChannel fc, long size )
throws IOException
{
if (Utils.isOddLength(size))
{
writePaddingToFile(fc, 1);
}
} |
Chunk must also start on an even byte so if our chinksize is odd we need
to write another byte
@param fc
@param size
@throws IOException
| BothTagsFileStructure::writeExtraByteIfChunkOddSize | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private boolean isID3TagAtEndOfFileAllowingForPaddingByte(WavTag existingTag, FileChannel fc) throws IOException
{
return ((existingTag.getID3Tag().getEndLocationInFile() == fc.size())||
(((existingTag.getID3Tag().getEndLocationInFile() & 1) != 0) && existingTag.getID3Tag().getEndLocationInFile() + 1 == fc.size()));
} |
@param existingTag
@param fc
@return trueif ID3Tag at end of the file
@throws IOException
| BothTagsFileStructure::isID3TagAtEndOfFileAllowingForPaddingByte | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private boolean isInfoTagAtEndOfFileAllowingForPaddingByte(WavTag existingTag, FileChannel fc) throws IOException
{
return ((existingTag.getInfoTag().getEndLocationInFile() == fc.size())||
(((existingTag.getInfoTag().getEndLocationInFile() & 1) != 0) && existingTag.getInfoTag().getEndLocationInFile() + 1 == fc.size()));
} |
@param existingTag
@param fc
@return
@throws IOException
| BothTagsFileStructure::isInfoTagAtEndOfFileAllowingForPaddingByte | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void saveBoth(WavTag wavTag, FileChannel fc, final WavTag existingTag )
throws CannotWriteException, IOException
{
//Convert both tags and get existing ones
final ByteBuffer infoTagBuffer = convertInfoChunk(wavTag);
final ByteBuffer id3TagBuffer = convertID3Chunk(wavTag, existingTag);
//If both tags already exist in file
if(existingTag.isExistingInfoTag() && existingTag.isExistingId3Tag())
{
if(!existingTag.isIncorrectlyAlignedTag())
{
BothTagsFileStructure fs = checkExistingLocations(existingTag, fc);
//We can write both chunks without affecting anything else
if (fs.isContiguous && fs.isAtEnd)
{
if (fs.isInfoTagFirst)
{
seekToStartOfListInfoMetadata(fc, existingTag);
writeBothTags(fc, infoTagBuffer, id3TagBuffer);
fc.truncate(fc.position());
}
else
{
seekToStartOfId3Metadata(fc, existingTag);
writeBothTags(fc, infoTagBuffer, id3TagBuffer);
fc.truncate(fc.position());
}
}
//Both chunks are together but there is another chunk after them
else
{
ChunkHeader infoChunkHeader = seekToStartOfListInfoMetadata(fc, existingTag);
ChunkHeader id3ChunkHeader = seekToStartOfId3Metadata(fc, existingTag);
deleteInfoTagChunk(fc, existingTag, infoChunkHeader);
deleteId3TagChunk(fc, existingTag, id3ChunkHeader);
fc.position(fc.size());
writeBothTags(fc, infoTagBuffer, id3TagBuffer);
}
}
//Existing metadata tag is incorrectly aligned so if we can lets delete it and any subsequentially added
//tags and start again
else if(WavChunkSummary.isOnlyMetadataTagsAfterStartingMetadataTag(existingTag))
{
deleteExistingMetadataTagsToEndOfFile(fc, existingTag);
fc.position(fc.size());
writeBothTags(fc, infoTagBuffer, id3TagBuffer);
}
else
{
throw new CannotWriteException(loggingName + " Metadata tags are corrupted and not at end of file so cannot be fixed");
}
}
//If only INFO chunk exists
else if(existingTag.isExistingInfoTag() && !existingTag.isExistingId3Tag())
{
if(!existingTag.isIncorrectlyAlignedTag())
{
ChunkHeader infoChunkHeader = seekToStartOfListInfoMetadata(fc, existingTag);
if (isInfoTagAtEndOfFileAllowingForPaddingByte(existingTag, fc))
{
writeBothTags(fc, infoTagBuffer, id3TagBuffer);
fc.truncate(fc.position());
}
else
{
deleteInfoTagChunk(fc, existingTag, infoChunkHeader);
fc.position(fc.size());
writeBothTags(fc, infoTagBuffer, id3TagBuffer);
}
}
//Existing metadata tag is incorrectly aligned so if we can lets delete it and any subsequentially added
//tags and start again
else if(WavChunkSummary.isOnlyMetadataTagsAfterStartingMetadataTag(existingTag))
{
deleteExistingMetadataTagsToEndOfFile(fc, existingTag);
fc.position(fc.size());
writeBothTags(fc, infoTagBuffer, id3TagBuffer);
}
else
{
throw new CannotWriteException(loggingName + " Metadata tags are corrupted and not at end of file so cannot be fixed");
}
}
//If only ID3 chunk exists
else if(existingTag.isExistingId3Tag() && !existingTag.isExistingInfoTag())
{
if(!existingTag.isIncorrectlyAlignedTag())
{
ChunkHeader id3ChunkHeader = seekToStartOfId3Metadata(fc, existingTag);
if (isID3TagAtEndOfFileAllowingForPaddingByte(existingTag, fc))
{
writeBothTags(fc, infoTagBuffer, id3TagBuffer);
fc.truncate(fc.position());
}
else
{
deleteId3TagChunk(fc, existingTag, id3ChunkHeader);
fc.position(fc.size());
writeBothTags(fc, infoTagBuffer, id3TagBuffer);
}
}
//Existing metadata tag is incorrectly aligned so if we can lets delete it and any subsequentially added
//tags and start again
else if(WavChunkSummary.isOnlyMetadataTagsAfterStartingMetadataTag(existingTag))
{
deleteExistingMetadataTagsToEndOfFile(fc, existingTag);
fc.position(fc.size());
writeBothTags(fc, infoTagBuffer, id3TagBuffer);
}
else
{
throw new CannotWriteException(loggingName + " Metadata tags are corrupted and not at end of file so cannot be fixed");
}
}
//No existing tags so write both to the end (or existing tag but couldnt not be written)
else
{
//Go to end of file
fc.position(fc.size());
writeBothTags(fc, infoTagBuffer, id3TagBuffer);
}
} |
Save both Info and ID3 chunk
@param wavTag
@param fc
@param existingTag
@throws CannotWriteException
@throws IOException
| BothTagsFileStructure::saveBoth | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void writeBothTags(FileChannel fc, ByteBuffer infoTagBuffer, ByteBuffer id3TagBuffer)
throws IOException
{
if(TagOptionSingleton.getInstance().getWavSaveOrder()==WavSaveOrder.INFO_THEN_ID3)
{
writeInfoDataToFile(fc, infoTagBuffer);
writeID3DataToFile(fc, id3TagBuffer);
}
else
{
writeID3DataToFile(fc, id3TagBuffer);
writeInfoDataToFile(fc, infoTagBuffer);
}
} |
Write both tags in the order preferred by the options
@param fc
@param infoTagBuffer
@param id3TagBuffer
@throws IOException
| BothTagsFileStructure::writeBothTags | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void saveActive(WavTag wavTag, FileChannel fc, final WavTag existingTag )
throws CannotWriteException, IOException
{
//Info is Active Tag
if(wavTag.getActiveTag() instanceof WavInfoTag)
{
final ByteBuffer infoTagBuffer = convertInfoChunk(wavTag);
final long newInfoTagSize = infoTagBuffer.limit();
//Usual Case
if(!existingTag.isIncorrectlyAlignedTag())
{
//We have an ID3 tag which we do not want
if (existingTag.isExistingId3Tag())
{
if (isID3TagAtEndOfFileAllowingForPaddingByte(existingTag, fc))
{
fc.truncate(existingTag.getStartLocationInFileOfId3Chunk());
}
else
{
ChunkHeader id3ChunkHeader = seekToStartOfId3Metadata(fc, existingTag);
deleteId3TagChunk(fc, existingTag, id3ChunkHeader);
}
}
//We already have such a tag
if (existingTag.isExistingInfoTag())
{
ChunkHeader infoChunkHeader = seekToStartOfListInfoMetadata(fc, existingTag);
if(isInfoTagAtEndOfFileAllowingForPaddingByte(existingTag, fc))
{
writeInfoChunk(fc, existingTag.getInfoTag(), infoTagBuffer);
}
else
{
deleteInfoTagChunk(fc, existingTag, infoChunkHeader);
fc.position(fc.size());
writeInfoDataToFile(fc, infoTagBuffer, infoTagBuffer.limit());
}
}
//Don't have tag so have to create new
else
{
fc.position(fc.size());
writeInfoDataToFile(fc, infoTagBuffer, newInfoTagSize);
}
}
//Existing one is in wrong place
//Existing Info tag is incorrectly aligned so if we can lets delete it and any subsequentially added
//tags and start again
else if(WavChunkSummary.isOnlyMetadataTagsAfterStartingMetadataTag(existingTag))
{
deleteExistingMetadataTagsToEndOfFile(fc, existingTag);
fc.position(fc.size());
writeInfoDataToFile(fc, infoTagBuffer, newInfoTagSize);
}
else
{
throw new CannotWriteException(loggingName + " Metadata tags are corrupted and not at end of file so cannot be fixed");
}
}
//ID3 is Active Tag
else
{
final ByteBuffer id3TagBuffer = convertID3Chunk(wavTag, existingTag);
if(!existingTag.isIncorrectlyAlignedTag())
{
if (existingTag.isExistingInfoTag())
{
ChunkHeader infoChunkHeader = seekToStartOfListInfoMetadata(fc, existingTag);
if (isInfoTagAtEndOfFileAllowingForPaddingByte(existingTag, fc))
{
fc.truncate(existingTag.getInfoTag().getStartLocationInFile());
}
else
{
deleteInfoTagChunk(fc, existingTag, infoChunkHeader);
}
}
if (existingTag.isExistingId3Tag())
{
ChunkHeader id3ChunkHeader = seekToStartOfId3Metadata(fc, existingTag);
if (isID3TagAtEndOfFileAllowingForPaddingByte(existingTag, fc))
{
writeID3DataToFile(fc, id3TagBuffer);
}
else
{
deleteId3TagChunk(fc, existingTag, id3ChunkHeader);
fc.position(fc.size());
writeID3DataToFile(fc, id3TagBuffer);
}
}
else
{
fc.position(fc.size());
writeID3DataToFile(fc, id3TagBuffer);
}
}
else if(WavChunkSummary.isOnlyMetadataTagsAfterStartingMetadataTag(existingTag))
{
deleteExistingMetadataTagsToEndOfFile(fc, existingTag);
fc.position(fc.size());
writeID3DataToFile(fc, id3TagBuffer);
}
else
{
throw new CannotWriteException(loggingName + " Metadata tags are corrupted and not at end of file so cannot be fixed");
}
}
} |
Save Active chunk only, if a non-active metadata chunk exists will be removed
@param wavTag
@param fc
@param existingTag
@throws CannotWriteException
@throws IOException
| BothTagsFileStructure::saveActive | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void saveActiveExisting(WavTag wavTag, FileChannel fc, final WavTag existingTag )
throws CannotWriteException, IOException
{
if(wavTag.getActiveTag() instanceof WavInfoTag)
{
if(existingTag.isExistingId3Tag())
{
saveBoth(wavTag, fc, existingTag);
}
else
{
saveActive(wavTag, fc, existingTag );
}
}
else
{
if(existingTag.isExistingInfoTag())
{
saveBoth(wavTag, fc, existingTag );
}
else
{
saveActive(wavTag, fc, existingTag );
}
}
} |
Save Active chunk and existing chunks even if not the active chunk
@param wavTag
@param fc
@param existingTag
@throws CannotWriteException
@throws IOException
| BothTagsFileStructure::saveActiveExisting | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
private void deleteExistingMetadataTagsToEndOfFile(final FileChannel fc, final WavTag existingTag) throws IOException
{
ChunkSummary precedingChunk = WavChunkSummary.getChunkBeforeFirstMetadataTag(existingTag);
//Preceding chunk ends on odd boundary
if(!Utils.isOddLength(precedingChunk.getEndLocation()))
{
logger.severe(loggingName + " Truncating corrupted metadata tags from:" + (existingTag.getInfoTag().getStartLocationInFile() - 1));
fc.truncate(existingTag.getInfoTag().getStartLocationInFile() - 1);
}
//Preceding chunk ends on even boundary
else
{
logger.severe(loggingName + " Truncating corrupted metadata tags from:" + (existingTag.getInfoTag().getStartLocationInFile()));
fc.truncate(existingTag.getInfoTag().getStartLocationInFile());
}
} | If Info/ID3 Metadata tags are corrupted and only metadata tags later in the file then just truncate metadata tags and start again
@param fc
@param existingTag
@throws IOException
| BothTagsFileStructure::deleteExistingMetadataTagsToEndOfFile | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavTagWriter.java | Apache-2.0 |
public void clean() throws Exception
{
System.out.println("EndOfDataChunk:" + Hex.asHex(findEndOfDataChunk()));
} |
Delete all data after data chunk and print new length
@throws Exception
| WavCleaner::clean | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavCleaner.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavCleaner.java | Apache-2.0 |
private int findEndOfDataChunk() throws Exception
{
try(FileChannel fc = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.READ))
{
if(WavRIFFHeader.isValidHeader(fc))
{
while (fc.position() < fc.size())
{
int endOfChunk = readChunk(fc);
if(endOfChunk>0)
{
fc.truncate(fc.position());
return endOfChunk;
}
}
}
}
return 0;
} |
If find data chunk delete al data after it
@return
@throws Exception
| WavCleaner::findEndOfDataChunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavCleaner.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavCleaner.java | Apache-2.0 |
private int readChunk(FileChannel fc) throws IOException, CannotReadException
{
Chunk chunk;
ChunkHeader chunkHeader = new ChunkHeader(ByteOrder.LITTLE_ENDIAN);
if (!chunkHeader.readHeader(fc))
{
return 0;
}
String id = chunkHeader.getID();
logger.config(loggingName + " Reading Chunk:" + id
+ ":starting at:" +Hex.asDecAndHex(chunkHeader.getStartLocationInFile())
+ ":sizeIncHeader:" + (chunkHeader.getSize() + ChunkHeader.CHUNK_HEADER_SIZE));
final WavChunkType chunkType = WavChunkType.get(id);
//If known chunkType
if (chunkType != null)
{
switch (chunkType)
{
case DATA:
{
fc.position(fc.position() + chunkHeader.getSize());
return (int)fc.position();
}
//Dont need to do anything with these just skip
default:
logger.config(loggingName + " Skipping chunk bytes:" + chunkHeader.getSize());
fc.position(fc.position() + chunkHeader.getSize());
}
}
//Unknown chunk type just skip
else
{
if(chunkHeader.getSize() < 0)
{
String msg = loggingName + " Not a valid header, unable to read a sensible size:Header"
+ chunkHeader.getID()+"Size:"+chunkHeader.getSize();
logger.severe(msg);
throw new CannotReadException(msg);
}
logger.severe(loggingName + " Skipping chunk bytes:" + chunkHeader.getSize() + " for" + chunkHeader.getID());
fc.position(fc.position() + chunkHeader.getSize());
}
IffHeaderChunk.ensureOnEqualBoundary(fc, chunkHeader);
return 0;
} |
@param fc
@return location of end of data chunk when chunk found
@throws IOException
@throws CannotReadException
| WavCleaner::readChunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavCleaner.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavCleaner.java | Apache-2.0 |
public synchronized static WavChunkType get(final String code) {
if (CODE_TYPE_MAP.isEmpty()) {
for (final WavChunkType type : values()) {
CODE_TYPE_MAP.put(type.getCode(), type);
}
}
return CODE_TYPE_MAP.get(code);
} |
Get {@link WavChunkType} for code (e.g. "SSND").
@param code chunk id
@return chunk type or {@code null} if not registered
| WavChunkType::get | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavChunkType.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavChunkType.java | Apache-2.0 |
WavChunkType(final String code, String description)
{
this.code=code;
this.description=description;
} |
@param code 4 char string
| WavChunkType::WavChunkType | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavChunkType.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavChunkType.java | Apache-2.0 |
public String getCode()
{
return code;
} |
4 char type code.
@return 4 char type code, e.g. "SSND" for the sound chunk.
| WavChunkType::getCode | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavChunkType.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavChunkType.java | Apache-2.0 |
private void calculateTrackLength(GenericAudioHeader info) throws CannotReadException
{
//If we have fact chunk we can calculate accurately by taking total of samples (per channel) divided by the number
//of samples taken per second (per channel)
if(info.getNoOfSamples()!=null)
{
if(info.getSampleRateAsNumber()>0)
{
info.setPreciseLength((float)info.getNoOfSamples() / info.getSampleRateAsNumber());
}
}
//Otherwise adequate to divide the total number of sampling bytes by the average byte rate
else if(info.getAudioDataLength()> 0)
{
info.setPreciseLength((float)info.getAudioDataLength() / info.getByteRate());
}
else
{
throw new CannotReadException(loggingName + " Wav Data Header Missing");
}
} |
Calculate track length, done it here because requires data from multiple chunks
@param info
@throws CannotReadException
| WavInfoReader::calculateTrackLength | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavInfoReader.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavInfoReader.java | Apache-2.0 |
protected boolean readChunk(FileChannel fc, GenericAudioHeader info) throws IOException, CannotReadException
{
Chunk chunk;
ChunkHeader chunkHeader = new ChunkHeader(ByteOrder.LITTLE_ENDIAN);
if (!chunkHeader.readHeader(fc))
{
return false;
}
String id = chunkHeader.getID();
logger.fine(loggingName + " Reading Chunk:" + id
+ ":starting at:" + Hex.asDecAndHex(chunkHeader.getStartLocationInFile())
+ ":sizeIncHeader:" + (chunkHeader.getSize() + ChunkHeader.CHUNK_HEADER_SIZE));
final WavChunkType chunkType = WavChunkType.get(id);
//If known chunkType
if (chunkType != null)
{
switch (chunkType)
{
case FACT:
{
ByteBuffer fmtChunkData = Utils.readFileDataIntoBufferLE(fc, (int) chunkHeader.getSize());
chunk = new WavFactChunk(fmtChunkData, chunkHeader, info);
if (!chunk.readChunk())
{
return false;
}
break;
}
case DATA:
{
//We just need this value from header dont actually need to read data itself
info.setAudioDataLength(chunkHeader.getSize());
info.setAudioDataStartPosition(fc.position());
info.setAudioDataEndPosition(fc.position() + chunkHeader.getSize());
fc.position(fc.position() + chunkHeader.getSize());
break;
}
case FORMAT:
{
ByteBuffer fmtChunkData = Utils.readFileDataIntoBufferLE(fc, (int) chunkHeader.getSize());
chunk = new WavFormatChunk(fmtChunkData, chunkHeader, info);
if (!chunk.readChunk())
{
return false;
}
break;
}
case CORRUPT_LIST:
logger.severe(loggingName + " Found Corrupt LIST Chunk, starting at Odd Location:"+chunkHeader.getID()+":"+chunkHeader.getSize());
fc.position(fc.position() - (ChunkHeader.CHUNK_HEADER_SIZE - 1));
return true;
//Dont need to do anything with these just skip
default:
logger.config(loggingName + " Skipping chunk bytes:" + chunkHeader.getSize());
fc.position(fc.position() + chunkHeader.getSize());
}
}
//Unknown chunk type just skip
else
{
if(chunkHeader.getSize() < 0)
{
String msg = loggingName + " Not a valid header, unable to read a sensible size:Header"
+ chunkHeader.getID()+"Size:"+chunkHeader.getSize();
logger.severe(msg);
throw new CannotReadException(msg);
}
logger.config(loggingName + " Skipping chunk bytes:" + chunkHeader.getSize() + " for " + chunkHeader.getID());
fc.position(fc.position() + chunkHeader.getSize());
if(fc.position()>fc.size())
{
String msg = loggingName + " Failed to move to invalid position to " + fc.position() + " because file length is only " + fc.size()
+ " indicates invalid chunk";
logger.severe(msg);
throw new CannotReadException(msg);
}
}
IffHeaderChunk.ensureOnEqualBoundary(fc, chunkHeader);
return true;
} |
Reads a Wav Chunk.
| WavInfoReader::readChunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/WavInfoReader.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/WavInfoReader.java | Apache-2.0 |
public boolean readChunk() throws IOException
{
boolean result = false;
String subIdentifier = Utils.readFourBytesAsChars(chunkData);
if(subIdentifier.equals(WavChunkType.INFO.getCode()))
{
WavInfoChunk chunk = new WavInfoChunk(tag, loggingName);
result = chunk.readChunks(chunkData);
//This is the start of the enclosing LIST element
tag.getInfoTag().setStartLocationInFile(chunkHeader.getStartLocationInFile());
tag.getInfoTag().setEndLocationInFile(chunkHeader.getStartLocationInFile() + ChunkHeader.CHUNK_HEADER_SIZE + chunkHeader.getSize());
tag.setExistingInfoTag(true);
}
return result;
} |
@return true if successfully read chunks
@throws IOException
| WavListChunk::readChunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavListChunk.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavListChunk.java | Apache-2.0 |
public WavId3Chunk(final ByteBuffer chunkData, final ChunkHeader chunkHeader, final WavTag tag)
{
super(chunkData, chunkHeader);
wavTag = tag;
} |
Constructor.
@param chunkData The content of this chunk
@param chunkHeader The header for this chunk
@param tag The WavTag into which information is stored
| WavId3Chunk::WavId3Chunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavId3Chunk.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavId3Chunk.java | Apache-2.0 |
private boolean isId3v2Tag(final ByteBuffer headerData) throws IOException
{
for (int i = 0; i < AbstractID3v2Tag.FIELD_TAGID_LENGTH; i++)
{
if (headerData.get() != AbstractID3v2Tag.TAG_ID[i])
{
return false;
}
}
return true;
} |
Reads 3 bytes to determine if the tag really looks like ID3 data.
| WavId3Chunk::isId3v2Tag | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavId3Chunk.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavId3Chunk.java | Apache-2.0 |
public synchronized static WavInfoIdentifier getByCode(final String code)
{
if (CODE_TYPE_MAP.isEmpty())
{
for (final WavInfoIdentifier type : values())
{
CODE_TYPE_MAP.put(type.getCode(), type);
}
}
return CODE_TYPE_MAP.get(code);
} |
Get {@link WavInfoIdentifier} for code (e.g. "SSND").
@param code chunk id
@return chunk type or {@code null} if not registered
| WavInfoIdentifier::getByCode | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavInfoIdentifier.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavInfoIdentifier.java | Apache-2.0 |
public synchronized static WavInfoIdentifier getByByFieldKey(final FieldKey fieldKey)
{
if (FIELDKEY_TYPE_MAP.isEmpty())
{
for (final WavInfoIdentifier type : values())
{
if (type.getFieldKey() != null)
{
FIELDKEY_TYPE_MAP.put(type.getFieldKey(), type);
}
}
}
return FIELDKEY_TYPE_MAP.get(fieldKey);
} |
Get {@link WavInfoIdentifier} for code (e.g. "SSND").
@param fieldKey
@return chunk type or {@code null} if not registered
| WavInfoIdentifier::getByByFieldKey | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavInfoIdentifier.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavInfoIdentifier.java | Apache-2.0 |
public static long getStartLocationOfFirstMetadataChunk(WavTag tag)
{
//Work out the location of the first metadata tag (could be id3 or LIST tag)
long startLocationOfMetadatTag = -1;
if(tag.getInfoTag()!=null)
{
startLocationOfMetadatTag = tag.getInfoTag().getStartLocationInFile();
if(tag.getID3Tag()!=null)
{
if(tag.getStartLocationInFileOfId3Chunk() < startLocationOfMetadatTag)
{
startLocationOfMetadatTag = tag.getStartLocationInFileOfId3Chunk();
}
}
}
else if(tag.getID3Tag()!=null)
{
startLocationOfMetadatTag = tag.getStartLocationInFileOfId3Chunk();
}
return startLocationOfMetadatTag;
} |
Get start location in file of first metadata chunk (could be LIST or ID3)
@param tag
@return
| WavChunkSummary::getStartLocationOfFirstMetadataChunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavChunkSummary.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavChunkSummary.java | Apache-2.0 |
public static boolean isOnlyMetadataTagsAfterStartingMetadataTag(WavTag tag)
{
long startLocationOfMetadatTag = getStartLocationOfFirstMetadataChunk(tag);
if(startLocationOfMetadatTag==-1)
{
logger.severe("Unable to find any metadata tags !");
return false;
}
boolean firstMetadataTag = false;
for(ChunkSummary cs:tag.getChunkSummaryList())
{
if(firstMetadataTag)
{
if(
!cs.getChunkId().equals(WavChunkType.ID3.getCode()) &&
!cs.getChunkId().equals(WavChunkType.LIST.getCode()) &&
!cs.getChunkId().equals(WavChunkType.INFO.getCode())
)
{
return false;
}
}
else
{
if (cs.getFileStartLocation() == startLocationOfMetadatTag)
{
//Found starting point
firstMetadataTag = true;
}
}
}
//Should always be true but this is to protect against something gone wrong
return firstMetadataTag;
} |
Checks that there are only id3 tags after the currently selected id3tag because this means its safe to truncate
the remainder of the file.
@param tag
@return
| WavChunkSummary::isOnlyMetadataTagsAfterStartingMetadataTag | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavChunkSummary.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavChunkSummary.java | Apache-2.0 |
public static ChunkSummary getChunkBeforeFirstMetadataTag(WavTag tag)
{
long startLocationOfMetadatTag = getStartLocationOfFirstMetadataChunk(tag);
for(int i=0;i < tag.getChunkSummaryList().size(); i++)
{
ChunkSummary cs = tag.getChunkSummaryList().get(i);
if (cs.getFileStartLocation() == startLocationOfMetadatTag)
{
return tag.getChunkSummaryList().get(i - 1);
}
}
return null;
} |
Get chunk before the first metadata tag
@param tag
@return
| WavChunkSummary::getChunkBeforeFirstMetadataTag | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavChunkSummary.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavChunkSummary.java | Apache-2.0 |
public boolean readChunks(ByteBuffer chunkData)
{
while(chunkData.remaining()>= IffHeaderChunk.TYPE_LENGTH)
{
String id = Utils.readFourBytesAsChars(chunkData);
//Padding
if(id.trim().isEmpty())
{
return true;
}
int size = chunkData.getInt();
if(
(!Character.isAlphabetic(id.charAt(0)))||
(!Character.isAlphabetic(id.charAt(1)))||
(!Character.isAlphabetic(id.charAt(2)))||
(!Character.isAlphabetic(id.charAt(3)))
)
{
logger.severe(loggingName + "LISTINFO appears corrupt, ignoring:"+id+":"+size);
return false;
}
String value =null;
try
{
value = Utils.getString(chunkData, 0, size, StandardCharsets.UTF_8);
}
catch(BufferUnderflowException bue)
{
logger.log(Level.SEVERE, loggingName + "LISTINFO appears corrupt, ignoring:"+bue.getMessage(), bue);
return false;
}
logger.config(loggingName + "Result:" + id + ":" + size + ":" + value + ":");
WavInfoIdentifier wii = WavInfoIdentifier.getByCode(id);
if(wii!=null && wii.getFieldKey()!=null)
{
try
{
wavInfoTag.setField(wii.getFieldKey(), value);
}
catch(FieldDataInvalidException fdie)
{
logger.log(Level.SEVERE, loggingName + fdie.getMessage(), fdie);
}
}
//Add unless just padding
else if(id!=null && !id.trim().isEmpty())
{
wavInfoTag.addUnRecognizedField(id, value);
}
//Each tuple aligned on even byte boundary
if (Utils.isOddLength(size) && chunkData.hasRemaining())
{
chunkData.get();
}
}
return true;
} |
Read Info chunk
@param chunkData
| WavInfoChunk::readChunks | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavInfoChunk.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/wav/chunk/WavInfoChunk.java | Apache-2.0 |
public CannotWriteException()
{
super();
} |
(overridden)
@see Exception#Exception()
| CannotWriteException::CannotWriteException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/CannotWriteException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/CannotWriteException.java | Apache-2.0 |
public CannotWriteException(String message)
{
super(message);
} |
(overridden)
@param message
@see Exception#Exception(java.lang.String)
| CannotWriteException::CannotWriteException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/CannotWriteException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/CannotWriteException.java | Apache-2.0 |
public CannotWriteException(String message, Throwable cause)
{
super(message, cause);
} |
(overridden)
@param message
@param cause
@see Exception#Exception(java.lang.String,java.lang.Throwable)
| CannotWriteException::CannotWriteException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/CannotWriteException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/CannotWriteException.java | Apache-2.0 |
public CannotWriteException(Throwable cause)
{
super(cause);
} |
(overridden)
@param cause
@see Exception#Exception(java.lang.Throwable)
| CannotWriteException::CannotWriteException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/CannotWriteException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/CannotWriteException.java | Apache-2.0 |
public CannotReadException()
{
super();
} |
Creates an instance.
| CannotReadException::CannotReadException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadException.java | Apache-2.0 |
public CannotReadException(String message)
{
super(message);
} |
Creates an instance.
@param message The message.
| CannotReadException::CannotReadException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadException.java | Apache-2.0 |
public CannotReadException(String message, Throwable cause)
{
super(message, cause);
} |
Creates an instance.
@param message The error message.
@param cause The throwable causing this exception.
| CannotReadException::CannotReadException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadException.java | Apache-2.0 |
public NoReadPermissionsException()
{
super();
} |
Creates an instance.
| NoReadPermissionsException::NoReadPermissionsException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/NoReadPermissionsException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/NoReadPermissionsException.java | Apache-2.0 |
public NoReadPermissionsException(String message)
{
super(message);
} |
Creates an instance.
@param message The message.
| NoReadPermissionsException::NoReadPermissionsException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/NoReadPermissionsException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/NoReadPermissionsException.java | Apache-2.0 |
public NoReadPermissionsException(String message, Throwable cause)
{
super(message, cause);
} |
Creates an instance.
@param message The error message.
@param cause The throwable causing this exception.
| NoReadPermissionsException::NoReadPermissionsException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/NoReadPermissionsException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/NoReadPermissionsException.java | Apache-2.0 |
public ReadOnlyFileException()
{
} |
Creates a new ReadOnlyException datatype.
| ReadOnlyFileException::ReadOnlyFileException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/ReadOnlyFileException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/ReadOnlyFileException.java | Apache-2.0 |
public ReadOnlyFileException(String msg)
{
super(msg);
} |
Creates a new ReadOnlyException datatype.
@param msg the detail message.
| ReadOnlyFileException::ReadOnlyFileException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/ReadOnlyFileException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/ReadOnlyFileException.java | Apache-2.0 |
public CannotReadVideoException()
{
super();
} |
Creates an instance.
| CannotReadVideoException::CannotReadVideoException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadVideoException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadVideoException.java | Apache-2.0 |
public CannotReadVideoException(String message)
{
super(message);
} |
Creates an instance.
@param message The message.
| CannotReadVideoException::CannotReadVideoException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadVideoException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadVideoException.java | Apache-2.0 |
public CannotReadVideoException(String message, Throwable cause)
{
super(message, cause);
} |
Creates an instance.
@param message The error message.
@param cause The throwable causing this exception.
| CannotReadVideoException::CannotReadVideoException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadVideoException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/CannotReadVideoException.java | Apache-2.0 |
public ModifyVetoException()
{
super();
} |
(overridden)
| ModifyVetoException::ModifyVetoException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/ModifyVetoException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/ModifyVetoException.java | Apache-2.0 |
public ModifyVetoException(String message)
{
super(message);
} |
(overridden)
@param message
@see Exception#Exception(java.lang.String)
| ModifyVetoException::ModifyVetoException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/ModifyVetoException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/ModifyVetoException.java | Apache-2.0 |
public ModifyVetoException(String message, Throwable cause)
{
super(message, cause);
} |
(overridden)
@param message
@param cause
@see Exception#Exception(java.lang.String,java.lang.Throwable)
| ModifyVetoException::ModifyVetoException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/ModifyVetoException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/ModifyVetoException.java | Apache-2.0 |
public ModifyVetoException(Throwable cause)
{
super(cause);
} |
(overridden)
@param cause
@see Exception#Exception(java.lang.Throwable)
| ModifyVetoException::ModifyVetoException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/ModifyVetoException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/ModifyVetoException.java | Apache-2.0 |
public NoWritePermissionsException()
{
super();
} |
Creates an instance.
| NoWritePermissionsException::NoWritePermissionsException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/NoWritePermissionsException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/NoWritePermissionsException.java | Apache-2.0 |
public NoWritePermissionsException(String message)
{
super(message);
} |
Creates an instance.
@param message The message.
| NoWritePermissionsException::NoWritePermissionsException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/NoWritePermissionsException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/NoWritePermissionsException.java | Apache-2.0 |
public NoWritePermissionsException(String message, Throwable cause)
{
super(message, cause);
} |
Creates an instance.
@param message The error message.
@param cause The throwable causing this exception.
| NoWritePermissionsException::NoWritePermissionsException | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/exceptions/NoWritePermissionsException.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/exceptions/NoWritePermissionsException.java | Apache-2.0 |
public Chunk(ByteBuffer chunkData, ChunkHeader chunkHeader)
{
this.chunkData = chunkData;
this.chunkHeader = chunkHeader;
} |
Constructor used by Wav
@param chunkData
@param chunkHeader
| Chunk::Chunk | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/iff/Chunk.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/iff/Chunk.java | Apache-2.0 |
public static void ensureOnEqualBoundary(final RandomAccessFile raf,ChunkHeader chunkHeader) throws IOException
{
if (Utils.isOddLength(chunkHeader.getSize()))
{
// Must come out to an even byte boundary unless at end of file
if(raf.getFilePointer()<raf.length())
{
logger.config("Skipping Byte because on odd boundary");
raf.skipBytes(1);
}
}
} |
If Size is not even then we skip a byte, because chunks have to be aligned
@param raf
@param chunkHeader
@throws java.io.IOException
| IffHeaderChunk::ensureOnEqualBoundary | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/iff/IffHeaderChunk.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/iff/IffHeaderChunk.java | Apache-2.0 |
public boolean readHeader(final FileChannel fc) throws IOException
{
ByteBuffer header = ByteBuffer.allocate(CHUNK_HEADER_SIZE);
startLocationInFile = fc.position();
fc.read(header);
header.order(byteOrder);
header.position(0);
this.chunkId = Utils.readFourBytesAsChars(header);
this.size = header.getInt();
return true;
} |
Reads the header of a chunk.
@return {@code true}, if we were able to read a chunk header and believe we found a valid chunk id.
| ChunkHeader::readHeader | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | Apache-2.0 |
public boolean readHeader(final RandomAccessFile raf) throws IOException
{
ByteBuffer header = ByteBuffer.allocate(CHUNK_HEADER_SIZE);
startLocationInFile = raf.getFilePointer();
raf.getChannel().read(header);
header.order(byteOrder);
header.position(0);
this.chunkId = Utils.readFourBytesAsChars(header);
this.size = header.getInt();
return true;
} |
Reads the header of a chunk.
@return {@code true}, if we were able to read a chunk header and believe we found a valid chunk id.
| ChunkHeader::readHeader | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | Apache-2.0 |
public ByteBuffer writeHeader()
{
final ByteBuffer bb = ByteBuffer.allocate(CHUNK_HEADER_SIZE);
bb.order(byteOrder);
bb.put(chunkId.getBytes(StandardCharsets.US_ASCII));
bb.putInt((int)size);
bb.flip();
return bb;
} |
Writes this chunk header to a {@link ByteBuffer}.
@return the byte buffer containing the
| ChunkHeader::writeHeader | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | Apache-2.0 |
public void setID(final String id)
{
this.chunkId = id;
} |
Sets the chunk type, which is a 4-character code, directly.
@param id 4-char id
| ChunkHeader::setID | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | Apache-2.0 |
public String getID()
{
return this.chunkId;
} |
Returns the chunk type, which is a 4-character code.
@return id
| ChunkHeader::getID | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | Apache-2.0 |
public long getSize()
{
return size;
} |
Returns the chunk size (excluding the first 8 bytes).
@see #setSize(long)
| ChunkHeader::getSize | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | Apache-2.0 |
public void setSize(final long size)
{
this.size=size;
} |
Set chunk size.
@param size chunk size without header
@see #getSize()
| ChunkHeader::setSize | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | Apache-2.0 |
public long getStartLocationInFile()
{
return startLocationInFile;
} |
Set chunk size.
@param size chunk size without header
@see #getSize()
public void setSize(final long size)
{
this.size=size;
}
/** The start of this chunk(header) in the file | ChunkHeader::getStartLocationInFile | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/iff/ChunkHeader.java | Apache-2.0 |
public void addAudioFileModificationListener(AudioFileModificationListener l)
{
if (!this.listeners.contains(l))
{
this.listeners.add(l);
}
} |
This method adds an {@link AudioFileModificationListener}
@param l Listener to add.
| ModificationHandler::addAudioFileModificationListener | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | Apache-2.0 |
public void fileModified(AudioFile original, File temporary) throws ModifyVetoException
{
for (AudioFileModificationListener listener : this.listeners)
{
AudioFileModificationListener current = listener;
try
{
current.fileModified(original, temporary);
}
catch (ModifyVetoException e)
{
vetoThrown(current, original, e);
throw e;
}
}
} |
(overridden)
@see org.jaudiotagger.audio.generic.AudioFileModificationListener#fileModified(org.jaudiotagger.audio.AudioFile,
File)
| ModificationHandler::fileModified | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | Apache-2.0 |
public void fileOperationFinished(File result)
{
for (AudioFileModificationListener listener : this.listeners)
{
AudioFileModificationListener current = listener;
current.fileOperationFinished(result);
}
} |
(overridden)
@see org.jaudiotagger.audio.generic.AudioFileModificationListener#fileOperationFinished(File)
| ModificationHandler::fileOperationFinished | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | Apache-2.0 |
public void fileWillBeModified(AudioFile file, boolean delete) throws ModifyVetoException
{
for (AudioFileModificationListener listener : this.listeners)
{
AudioFileModificationListener current = listener;
try
{
current.fileWillBeModified(file, delete);
}
catch (ModifyVetoException e)
{
vetoThrown(current, file, e);
throw e;
}
}
} |
(overridden)
@see org.jaudiotagger.audio.generic.AudioFileModificationListener#fileWillBeModified(org.jaudiotagger.audio.AudioFile,
boolean)
| ModificationHandler::fileWillBeModified | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | Apache-2.0 |
public void removeAudioFileModificationListener(AudioFileModificationListener l)
{
this.listeners.remove(l);
} |
This method removes an {@link AudioFileModificationListener}
@param l Listener to remove.
| ModificationHandler::removeAudioFileModificationListener | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | Apache-2.0 |
public void vetoThrown(AudioFileModificationListener cause, AudioFile original, ModifyVetoException veto)
{
for (AudioFileModificationListener listener : this.listeners)
{
AudioFileModificationListener current = listener;
current.vetoThrown(cause, original, veto);
}
} |
(overridden)
@see org.jaudiotagger.audio.generic.AudioFileModificationListener#vetoThrown(org.jaudiotagger.audio.generic.AudioFileModificationListener,
org.jaudiotagger.audio.AudioFile,
org.jaudiotagger.audio.exceptions.ModifyVetoException)
| ModificationHandler::vetoThrown | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/ModificationHandler.java | Apache-2.0 |
public AudioFile read(File f) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException
{
Path path = f.toPath();
if(logger.isLoggable(Level.CONFIG))
{
logger.config(ErrorMessage.GENERAL_READ.getMsg(path));
}
if (!Files.isReadable(path))
{
logger.warning(Permissions.displayPermissions(path));
throw new NoReadPermissionsException(ErrorMessage.GENERAL_READ_FAILED_DO_NOT_HAVE_PERMISSION_TO_READ_FILE.getMsg(path));
}
if (f.length() <= MINIMUM_SIZE_FOR_VALID_AUDIO_FILE)
{
throw new CannotReadException(ErrorMessage.GENERAL_READ_FAILED_FILE_TOO_SMALL.getMsg(path));
}
GenericAudioHeader info = getEncodingInfo(path);
Tag tag = getTag(path);
return new AudioFile(f, info, tag);
} |
Replacement for AudioFileReader class
public abstract class AudioFileReader2 extends AudioFileReader
{
/*
Reads the given file, and return an AudioFile object containing the Tag
and the encoding infos present in the file. If the file has no tag, an
empty one is returned. If the encodinginfo is not valid , an exception is thrown.
@param f The file to read
@exception NoReadPermissionsException if permissions prevent reading of file
@exception CannotReadException If anything went bad during the read of this file
| AudioFileReader2::read | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/AudioFileReader2.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/AudioFileReader2.java | Apache-2.0 |
public void delete(AudioFile af) throws CannotReadException, CannotWriteException
{
Path file = af.getFile().toPath();
if (TagOptionSingleton.getInstance().isCheckIsWritable() && !Files.isWritable(file))
{
logger.severe(Permissions.displayPermissions(file));
throw new CannotWriteException(ErrorMessage.GENERAL_DELETE_FAILED.getMsg(file));
}
if (af.getFile().length() <= MINIMUM_FILESIZE)
{
throw new CannotWriteException(ErrorMessage.GENERAL_DELETE_FAILED_BECAUSE_FILE_IS_TOO_SMALL.getMsg(file));
}
RandomAccessFile raf = null;
RandomAccessFile rafTemp = null;
File tempF = null;
// Will be set to true on VetoException, causing the finally block to
// discard the tempfile.
boolean revert = false;
try
{
tempF = File.createTempFile(af.getFile().getName().replace('.', '_'), TEMP_FILENAME_SUFFIX, af.getFile().getParentFile());
rafTemp = new RandomAccessFile(tempF, WRITE_MODE);
raf = new RandomAccessFile(af.getFile(), WRITE_MODE);
raf.seek(0);
rafTemp.seek(0);
try
{
if (this.modificationListener != null)
{
this.modificationListener.fileWillBeModified(af, true);
}
deleteTag(af.getTag(), raf, rafTemp);
if (this.modificationListener != null)
{
this.modificationListener.fileModified(af, tempF);
}
}
catch (ModifyVetoException veto)
{
throw new CannotWriteException(veto);
}
}
catch (Exception e)
{
revert = true;
throw new CannotWriteException("\"" + af.getFile().getAbsolutePath() + "\" :" + e, e);
}
finally
{
// will be set to the remaining file.
File result = af.getFile();
try
{
if (raf != null)
{
raf.close();
}
if (rafTemp != null)
{
rafTemp.close();
}
if (tempF.length() > 0 && !revert)
{
boolean deleteResult = af.getFile().delete();
if (!deleteResult)
{
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
}
boolean renameResult = tempF.renameTo(af.getFile());
if (!renameResult)
{
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
}
result = tempF;
// If still exists we can now delete
if (tempF.exists())
{
if (!tempF.delete())
{
// Non critical failed deletion
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(tempF.getPath()));
}
}
}
else
{
// It was created but never used
if (!tempF.delete())
{
// Non critical failed deletion
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(tempF.getPath()));
}
}
}
catch (Exception ex)
{
logger.severe("AudioFileWriter exception cleaning up delete:" + af.getFile().getPath() + " or" + tempF.getAbsolutePath() + ":" + ex);
}
// Notify listener
if (this.modificationListener != null)
{
this.modificationListener.fileOperationFinished(result);
}
}
} |
Delete the tag (if any) present in the given file
@param af The file to process
@throws CannotWriteException if anything went wrong
@throws org.jaudiotagger.audio.exceptions.CannotReadException
| AudioFileWriter::delete | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | Apache-2.0 |
public void delete(Tag tag, RandomAccessFile raf, RandomAccessFile tempRaf) throws CannotReadException, CannotWriteException, IOException
{
raf.seek(0);
tempRaf.seek(0);
deleteTag(tag, raf, tempRaf);
} |
Delete the tag (if any) present in the given randomaccessfile, and do not
close it at the end.
@param tag
@param raf The source file, already opened in r-write mode
@param tempRaf The temporary file opened in r-write mode
@throws CannotWriteException if anything went wrong
@throws org.jaudiotagger.audio.exceptions.CannotReadException
@throws java.io.IOException
| AudioFileWriter::delete | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | Apache-2.0 |
public void setAudioFileModificationListener(AudioFileModificationListener listener)
{
this.modificationListener = listener;
} |
This method sets the {@link AudioFileModificationListener}.<br>
There is only one listener allowed, if you want more instances to be
supported, use the {@link ModificationHandler} to broadcast those events.<br>
@param listener The listener. <code>null</code> allowed to deregister.
| AudioFileWriter::setAudioFileModificationListener | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | Apache-2.0 |
private void precheckWrite(AudioFile af) throws CannotWriteException
{
// Preliminary checks
try
{
if (af.getTag().isEmpty())
{
delete(af);
return;
}
}
catch (CannotReadException re)
{
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED.getMsg(af.getFile().getPath()));
}
Path file = af.getFile().toPath();
if (TagOptionSingleton.getInstance().isCheckIsWritable() && !Files.isWritable(file))
{
logger.severe(Permissions.displayPermissions(file));
logger.severe(ErrorMessage.GENERAL_WRITE_FAILED.getMsg(af.getFile().getPath()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_OPEN_FILE_FOR_EDITING.getMsg(file));
}
if (af.getFile().length() <= MINIMUM_FILESIZE)
{
logger.severe(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE_FILE_IS_TOO_SMALL.getMsg(file));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE_FILE_IS_TOO_SMALL.getMsg(file));
}
} |
Prechecks before normal write
<p/>
<ul>
<li>If the tag is actually empty, remove the tag</li>
<li>if the file is not writable, throw exception
<li>
<li>If the file is too small to be a valid file, throw exception
<li>
</ul>
@param af
@throws CannotWriteException
| AudioFileWriter::precheckWrite | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | Apache-2.0 |
private void transferNewFileToOriginalFile(final File newFile, final File originalFile, final boolean reuseExistingOriginalFile) throws CannotWriteException
{
if (reuseExistingOriginalFile)
{
transferNewFileContentToOriginalFile(newFile, originalFile);
}
else
{
transferNewFileToNewOriginalFile(newFile, originalFile);
}
} |
<p>
Transfers the content from {@code newFile} to a file named {@code originalFile}.
With regards to file identity (inode/<a href="https://msdn.microsoft.com/en-us/library/aa363788(v=vs.85).aspx">fileIndex</a>),
after execution, {@code originalFile} may be a completely new file or the same file as before execution, depending
on {@code reuseExistingOriginalFile}.
</p>
<p>
Reusing the existing file may be slower, if both the temp file and the original file are located
in the same filesystem, because an actual copy is created instead of just a file rename.
If both files are on different filesystems, a copy is always needed — regardless of which method is used.
</p>
@param newFile new file
@param originalFile original file
@param reuseExistingOriginalFile {@code true} or {@code false}
@throws CannotWriteException If the file cannot be written
| AudioFileWriter::transferNewFileToOriginalFile | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | Apache-2.0 |
private void transferNewFileContentToOriginalFile(final File newFile, final File originalFile) throws CannotWriteException
{
// try to obtain exclusive lock on the file
try (final RandomAccessFile raf = new RandomAccessFile(originalFile, "rw"))
{
final FileChannel outChannel = raf.getChannel();
try (final FileLock lock = outChannel.tryLock())
{
if (lock != null)
{
transferNewFileContentToOriginalFile(newFile, originalFile, raf, outChannel);
}
else
{
// we didn't get a lock
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_FILE_LOCKED.getMsg(originalFile.getPath()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_FILE_LOCKED.getMsg(originalFile.getPath()));
}
}
catch (IOException e)
{
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_FILE_LOCKED.getMsg(originalFile.getPath()));
// we didn't get a lock, this may be, because locking is not supported by the OS/JRE
// this can happen on OS X with network shares (samba, afp)
// for details see https://stackoverflow.com/questions/33220148/samba-share-gradle-java-io-exception
// coarse check that works on OS X:
if ("Operation not supported".equals(e.getMessage()))
{
// transfer without lock
transferNewFileContentToOriginalFile(newFile, originalFile, raf, outChannel);
}
else
{
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_FILE_LOCKED.getMsg(originalFile.getPath()), e);
}
}
catch (Exception e)
{
// tryLock failed for some reason other than an IOException — we're definitely doomed
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_FILE_LOCKED.getMsg(originalFile.getPath()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_FILE_LOCKED.getMsg(originalFile.getPath()), e);
}
}
catch (FileNotFoundException e)
{
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE_FILE_NOT_FOUND.getMsg(originalFile.getAbsolutePath()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_BECAUSE_FILE_NOT_FOUND.getMsg(originalFile.getPath()), e);
}
catch (Exception e)
{
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED.getMsg(originalFile.getAbsolutePath()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED.getMsg(originalFile.getPath()), e);
}
} |
<p>
Writes the contents of the given {@code newFile} to the given {@code originalFile},
overwriting the already existing content in {@code originalFile}.
This ensures that the file denoted by the abstract pathname {@code originalFile}
keeps the same Unix inode or Windows
<a href="https://msdn.microsoft.com/en-us/library/aa363788(v=vs.85).aspx">fileIndex</a>.
</p>
<p>
If no errors occur, the method follows this approach:
</p>
<ol>
<li>Rename <code>originalFile</code> to <code>originalFile.old</code></li>
<li>Rename <code>newFile</code> to <code>originalFile</code> (this implies a file identity change for <code>originalFile</code>)</li>
<li>Delete <code>originalFile.old</code></li>
<li>Delete <code>newFile</code></li>
</ol>
@param newFile File containing the data we want in the {@code originalFile}
@param originalFile Before execution this denotes the original, unmodified file.
After execution it denotes the name of the file with the modified content and new inode/fileIndex.
@throws CannotWriteException if the file cannot be written
| AudioFileWriter::transferNewFileContentToOriginalFile | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | Apache-2.0 |
private void transferNewFileToNewOriginalFile(final File newFile, final File originalFile) throws CannotWriteException
{
// get original creation date
final FileTime creationTime = getCreationTime(originalFile);
// Rename Original File
// Can fail on Vista if have Special Permission 'Delete' set Deny
File originalFileBackup = new File(originalFile.getAbsoluteFile().getParentFile().getPath(), AudioFile.getBaseFilename(originalFile) + ".old");
//If already exists modify the suffix
int count = 1;
while (originalFileBackup.exists())
{
originalFileBackup = new File(originalFile.getAbsoluteFile().getParentFile().getPath(), AudioFile.getBaseFilename(originalFile) + ".old" + count);
count++;
}
boolean renameResult = Utils.rename(originalFile, originalFileBackup);
if (!renameResult)
{
logger.log(Level.SEVERE, ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_FILE_TO_BACKUP.getMsg(originalFile.getAbsolutePath(), originalFileBackup.getName()));
//Delete the temp file because write has failed
// TODO: Simplify: newFile is always != null, otherwise we would not have entered this block (-> if (newFile.length() > 0) {})
if (newFile != null)
{
newFile.delete();
}
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_FILE_TO_BACKUP.getMsg(originalFile.getPath(), originalFileBackup.getName()));
}
// Rename Temp File to Original File
renameResult = Utils.rename(newFile, originalFile);
if (!renameResult)
{
// Renamed failed so lets do some checks rename the backup back to the original file
// New File doesnt exist
if (!newFile.exists())
{
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_NEW_FILE_DOESNT_EXIST.getMsg(newFile.getAbsolutePath()));
}
// Rename the backup back to the original
if (!originalFileBackup.renameTo(originalFile))
{
// TODO now if this happens we are left with testfile.old
// instead of testfile.mp4
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_ORIGINAL_BACKUP_TO_ORIGINAL.getMsg(originalFileBackup.getAbsolutePath(), originalFile.getName()));
}
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(originalFile.getAbsolutePath(), newFile.getName()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(originalFile.getAbsolutePath(), newFile.getName()));
}
else
{
// Rename was okay so we can now delete the backup of the
// original
boolean deleteResult = originalFileBackup.delete();
if (!deleteResult)
{
// Not a disaster but can't delete the backup so make a
// warning
logger.warning(ErrorMessage.GENERAL_WRITE_WARNING_UNABLE_TO_DELETE_BACKUP_FILE.getMsg(originalFileBackup.getAbsolutePath()));
}
// now also set the creation date to the creation date of the original file
if (creationTime != null)
{
// this may fail silently on OS X, because of a JDK bug
setCreationTime(originalFile, creationTime);
}
}
// Delete the temporary file if still exists
if (newFile.exists())
{
if (!newFile.delete())
{
// Non critical failed deletion
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(newFile.getPath()));
}
}
} |
<p>
Replaces the original file with the new file in a way that changes the file identity.
In other words, the Unix inode or the Windows
<a href="https://msdn.microsoft.com/en-us/library/aa363788(v=vs.85).aspx">fileIndex</a>
of the resulting file with the name {@code originalFile} is not identical to the inode/fileIndex
of the file named {@code originalFile} before this method was called.
</p>
<p>
If no errors occur, the method follows this approach:
</p>
<ol>
<li>Rename <code>originalFile</code> to <code>originalFile.old</code></li>
<li>Rename <code>newFile</code> to <code>originalFile</code> (this implies a file identity change for <code>originalFile</code>)</li>
<li>Delete <code>originalFile.old</code></li>
<li>Delete <code>newFile</code></li>
</ol>
@param newFile File containing the data we want in the {@code originalFile}
@param originalFile Before execution this denotes the original, unmodified file.
After execution it denotes the name of the file with the modified content and new inode/fileIndex.
@throws CannotWriteException if the file cannot be written
| AudioFileWriter::transferNewFileToNewOriginalFile | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | Apache-2.0 |
private void setCreationTime(final File file, final FileTime creationTime)
{
try
{
Files.setAttribute(file.toPath(), "creationTime", creationTime);
}
catch (Exception e)
{
logger.log(Level.WARNING, ErrorMessage.GENERAL_SET_CREATION_TIME_FAILED.getMsg(file.getAbsolutePath(), e.getMessage()), e);
}
} |
Sets the creation time for a given file.
Fails silently with a log message.
@param file file
@param creationTime creation time
| AudioFileWriter::setCreationTime | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/AudioFileWriter.java | Apache-2.0 |
public static String getExtension(final File f)
{
final String name = f.getName().toLowerCase();
final int i = name.lastIndexOf(".");
if (i == -1)
{
return "";
}
return name.substring(i + 1);
} |
Returns the extension of the given file.
The extension is empty if there is no extension
The extension is the string after the last "."
@param f The file whose extension is requested
@return The extension of the given file
| Utils::getExtension | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | Apache-2.0 |
public static String getMagicExtension(final File f) throws IOException{
final String fileType = FileTypeUtil.getMagicFileType(f);
return FileTypeUtil.getMagicExt(fileType);
} |
Returns the extension of the given file based on the file signature.
The extension is empty if the file signature is not recognized.
@param f The file whose extension is requested
@return The extension of the given file
| Utils::getMagicExtension | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | Apache-2.0 |
public static long getLongLE(final ByteBuffer b, final int start, final int end)
{
long number = 0;
for (int i = 0; i < (end - start + 1); i++)
{
number += ((long) (b.get(start + i) & 0xFF) << i * 8);
}
return number;
} |
Computes a number whereby the 1st byte is the least signifcant and the last
byte is the most significant.
So if storing a number which only requires one byte it will be stored in the first
byte.
@param b The byte array @param start The starting offset in b
(b[offset]). The less significant byte @param end The end index
(included) in b (b[end]). The most significant byte
@return a long number represented by the byte sequence.
| Utils::getLongLE | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | Apache-2.0 |
public static long getLongBE(final ByteBuffer b, final int start, final int end)
{
long number = 0;
for (int i = 0; i < (end - start + 1); i++)
{
number += ((long)((b.get(end - i) & 0xFF)) << i * 8);
}
return number;
} |
Computes a number whereby the 1st byte is the most significant and the last
byte is the least significant.
So if storing a number which only requires one byte it will be stored in the last
byte.
Will fail if end - start >= 8, due to the limitations of the long type.
| Utils::getLongBE | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | Apache-2.0 |
public static int getIntLE(final byte[] b)
{
return (int) getLongLE(ByteBuffer.wrap(b), 0, b.length - 1);
} |
Computes a number whereby the 1st byte is the least significant and the last
byte is the most significant. This version doesn't take a length,
and it returns an int rather than a long.
@param b The byte array. Maximum length for valid results is 4 bytes.
| Utils::getIntLE | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | Apache-2.0 |
public static int getIntLE(final byte[] b, final int start, final int end)
{
return (int) getLongLE(ByteBuffer.wrap(b), start, end);
} |
Computes a number whereby the 1st byte is the least significant and the last
byte is the most significant. end - start must be no greater than 4.
@param b The byte array
@param start The starting offset in b (b[offset]). The less
significant byte
@param end The end index (included) in b (b[end])
@return a int number represented by the byte sequence.
| Utils::getIntLE | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | Apache-2.0 |
public static int getIntBE(final ByteBuffer b, final int start, final int end)
{
return (int) getLongBE(b, start, end);
} |
Computes a number whereby the 1st byte is the most significant and the last
byte is the least significant.
@param b The ByteBuffer
@param start The starting offset in b. The less
significant byte
@param end The end index (included) in b
@return an int number represented by the byte sequence.
| Utils::getIntBE | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | Apache-2.0 |
public static short getShortBE(final ByteBuffer b, final int start, final int end)
{
return (short) getIntBE(b, start, end);
} |
Computes a number whereby the 1st byte is the most significant and the last
byte is the least significant.
@param b The ByteBuffer
@param start The starting offset in b. The less
significant byte
@param end The end index (included) in b
@return a short number represented by the byte sequence.
| Utils::getShortBE | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | Apache-2.0 |
public static byte[] getSizeBEInt32(final int size)
{
final byte[] b = new byte[4];
b[0] = (byte) ((size >> 24) & 0xFF);
b[1] = (byte) ((size >> 16) & 0xFF);
b[2] = (byte) ((size >> 8) & 0xFF);
b[3] = (byte) (size & 0xFF);
return b;
} |
Convert int to byte representation - Big Endian (as used by mp4).
@param size
@return byte representation
| Utils::getSizeBEInt32 | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | Apache-2.0 |
public static byte[] getSizeBEInt16(final short size)
{
final byte[] b = new byte[2];
b[0] = (byte) ((size >> 8) & 0xFF);
b[1] = (byte) (size & 0xFF);
return b;
} |
Convert short to byte representation - Big Endian (as used by mp4).
@param size number to convert
@return byte representation
| Utils::getSizeBEInt16 | java | ZTFtrue/MonsterMusic | app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | https://github.com/ZTFtrue/MonsterMusic/blob/master/app/src/main/java/org/jaudiotagger/audio/generic/Utils.java | Apache-2.0 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.