All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mpatric.mp3agic.ID3v2CommentFrameData Maven / Gradle / Ivy

Go to download

A java library for reading mp3 files and manipulating mp3 file ID3 tags (ID3v1 and ID3v2.2 to ID3v2.4).

There is a newer version: 0.9.1
Show newest version
package com.mpatric.mp3agic;

import java.io.UnsupportedEncodingException;

public class ID3v2CommentFrameData extends AbstractID3v2FrameData {

	private static final String DEFAULT_LANGUAGE = "eng";
	
	private String language;
	private EncodedText description;
	private EncodedText comment;

	public ID3v2CommentFrameData(boolean unsynchronisation) {
		super(unsynchronisation);
	}
	
	public ID3v2CommentFrameData(boolean unsynchronisation, String language, EncodedText description, EncodedText comment) {
		super(unsynchronisation);
		this.language = language;
		this.description = description;
		this.comment = comment;
	}

	public ID3v2CommentFrameData(boolean unsynchronisation, byte[] bytes) throws InvalidDataException {
		super(unsynchronisation);
		synchroniseAndUnpackFrameData(bytes);
	}
	
	protected void unpackFrameData(byte[] bytes) throws InvalidDataException {
		try {
			language = BufferTools.byteBufferToString(bytes, 1, 3);
		} catch (UnsupportedEncodingException e) {
			language = "";
		}
		int marker = BufferTools.indexOfTerminatorForEncoding(bytes, 4, bytes[0]);
		if (marker >= 4) {
			description = new EncodedText(bytes[0], BufferTools.copyBuffer(bytes, 4, marker - 4));
			marker += description.getTerminator().length;
		} else {
			description = new EncodedText(bytes[0], "");
			marker = 4;
		}
		comment = new EncodedText(bytes[0], BufferTools.copyBuffer(bytes, marker, bytes.length - marker));
	}

	protected byte[] packFrameData() {
		byte[] bytes = new byte[getLength()];
		if (comment != null) bytes[0] = comment.getTextEncoding();
		else bytes[0] = 0;
		String langPadded;
		if (language == null) {
			langPadded = DEFAULT_LANGUAGE;
		} else if (language.length() > 3) {
			langPadded = language.substring(0, 3);
		} else {
			langPadded = BufferTools.padStringRight(language, 3, '\00');
		}
		try {
			BufferTools.stringIntoByteBuffer(langPadded, 0, 3, bytes, 1);
		} catch (UnsupportedEncodingException e) {
		}
		int marker = 4;
		if (description != null) {
			byte[] descriptionBytes = description.toBytes(true, true);
			BufferTools.copyIntoByteBuffer(descriptionBytes, 0, descriptionBytes.length, bytes, marker);
			marker += descriptionBytes.length;
		} else {
			bytes[marker++] = 0;
		}
		if (comment != null) {
			byte[] commentBytes = comment.toBytes(true, false);
			BufferTools.copyIntoByteBuffer(commentBytes, 0, commentBytes.length, bytes, marker);
		}
		return bytes;
	}

	protected int getLength() {
		int length = 4;
		if (description != null) length += description.toBytes(true, true).length;
		else length++;
		if (comment != null) length += comment.toBytes(true, false).length;
		return length;
	}
	
	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}
	
	public EncodedText getComment() {
		return comment;
	}

	public void setComment(EncodedText comment) {
		this.comment = comment;
	}

	public EncodedText getDescription() {
		return description;
	}

	public void setDescription(EncodedText description) {
		this.description = description;
	}
	
	public boolean equals(Object obj) {
		if (! (obj instanceof ID3v2CommentFrameData)) return false;
		if (! super.equals(obj)) return false;
		ID3v2CommentFrameData other = (ID3v2CommentFrameData) obj;
		if (language == null) {
			if (other.language != null) return false;
		} else if (other.language == null) return false;
		else if (! language.equals(other.language)) return false;
		if (description == null) {
			if (other.description != null) return false;
		} else if (other.description == null) return false;
		else if (! description.equals(other.description)) return false;
		if (comment == null) {
			if (other.comment != null) return false;
		} else if (other.comment == null) return false;
		else if (! comment.equals(other.comment)) return false;
		return true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy