FileDocCategorySizeDatePackage
MyID3v1.javaAPI DocMyID3 for Android5918Wed Oct 08 18:25:52 BST 2008org.cmc.music.myid3

MyID3v1

public class MyID3v1 extends Object implements org.cmc.music.metadata.MusicMetadataConstants

Fields Summary
private static final String
DEFAULT_CHAR_ENCODING
Constructors Summary
Methods Summary
private java.lang.StringgetField(MyID3Listener listener, byte[] bytes, int start, int length)

		for (int i = start; i < start + length; i++)
		{
			if (bytes[i] == 0)
			{
				length = i - start;
				break;
			}
		}
//		if (null != listener)
//			listener
//					.log("isValidIso8859", isValidIso8859(bytes, start, length));

		if (length > 0)
		{
			try
			{
				String result = new String(bytes, start, length,
						DEFAULT_CHAR_ENCODING);
				result = result.trim();
				if (result.length() < 1)
					return null;
				return result;
			} catch (Throwable e)
			{

			}
		}

		return null;
	
public org.cmc.music.metadata.MusicMetadataparseTags(byte[] bytes)

		return parseTags(null, bytes);
	
public org.cmc.music.metadata.MusicMetadataparseTags(MyID3Listener listener, byte[] bytes)

		MusicMetadata tags = new MusicMetadata("id3v1");

		int counter = 3;
		String title = getField(listener, bytes, counter, 30);
		counter += 30;
		tags.put(KEY_TITLE, title);
		if (null != listener)
			listener.logWithLength("id3v1 title", title);

		String artist = getField(listener, bytes, counter, 30);
		counter += 30;
		tags.put(KEY_ARTIST, artist);
		if (null != listener)
			listener.logWithLength("id3v1 artist", artist);

		String album = getField(listener, bytes, counter, 30);
		counter += 30;
		tags.put(KEY_ALBUM, album);
		if (null != listener)
			listener.logWithLength("id3v1 album", album);

		String year = getField(listener, bytes, counter, 4);
		counter += 4;
		tags.put(KEY_YEAR, year);
		if (null != listener)
			listener.logWithLength("id3v1 year", year);

		String comment = getField(listener, bytes, counter, 30);
		counter += 30;
		tags.put(KEY_COMMENT, comment);
		if (null != listener)
			listener.logWithLength("id3v1 comment", comment);

		if (bytes[counter - 2] == 0 && bytes[counter - 1] != 0)
		{
			int trackNumber = 0xff & bytes[counter - 1];
			tags.put(KEY_TRACK_NUMBER, new Integer(trackNumber));

			if (null != listener)
				listener.log("id3v1 trackNumber: " + trackNumber);
		}

		int genre = 0xff & bytes[counter];
		if (genre < 80 && genre > 0)
		{
			tags.put(KEY_GENRE_ID, new Integer(genre));

			if (null != listener)
				listener.log("id3v1 genre: " + genre);
		}

		if (null != listener)
			listener.log();

		return tags;
	
public byte[]toTag(org.cmc.music.metadata.MusicMetadata values)

	
	   
			 
	
		byte result[] = new byte[128];

		int index = 0;
		result[index++] = 0x54; // T
		result[index++] = 0x41; // A
		result[index++] = 0x47; // G

		writeField(result, index, 30, (String) values.get(KEY_TITLE));
		index += 30;

		writeField(result, index, 30, (String) values.get(KEY_ARTIST));
		index += 30;

		writeField(result, index, 30, (String) values.get(KEY_ALBUM));
		index += 30;

		{
			Object o = values.get(KEY_YEAR);
			Number value = null;
			if (o == null)
				;
			else if (o instanceof Number)
			{
				value = (Number) values.get(KEY_YEAR);
			} else if (o instanceof String)
			{
				String s = (String) o;
				try
				{
					value = Integer.valueOf(s);
				} catch (NumberFormatException e)
				{


				}
			} else


			writeField(result, index, 4, value == null ? null : "" + value);
			index += 4;
		}

		Number track_number = null;
		{
			Number value = (Number) values.get(KEY_TRACK_NUMBER);



			if (value != null && value.intValue() >= 0
					&& value.intValue() < 256)
				track_number = value;
		}

		if (track_number == null)
		{
			writeField(result, index, 30, (String) values.get(KEY_COMMENT));
			index += 30;
		} else
		{
			writeField(result, index, 28, (String) values.get(KEY_COMMENT));
			index += 28;

			result[index++] = 0;
			result[index++] = (byte) track_number.intValue();
		}

		{
			Object o = (Object) values.get(KEY_GENRE_ID);
			if (o == null)
				o = (Object) values.get(KEY_GENRE);

			if (o != null && (o instanceof String))
			{
				String genre_name = (String) o;
				Number genre_id = ID3v1Genre.get(genre_name);
				if (genre_id != null)
				{
					o = genre_id;



				}
			}

			if (o != null && !(o instanceof Number))
			{


			} else
			{
				Number value = (Number) o;

				if (value != null && value.intValue() >= 0
						&& value.intValue() < 80)
					result[index++] = (byte) value.intValue();
				else
					result[index++] = 0;
			}
		}



		return result;
	
private voidwriteField(byte[] bytes, int start, int max_length, java.lang.String s)

		if (s == null)
		{
			for (int i = 0; i < max_length; i++)
				bytes[i + start] = 0;
			return;
		}

		byte value[] = s.getBytes(DEFAULT_CHAR_ENCODING);
		int count = Math.min(value.length, max_length);
		for (int i = 0; i < count; i++)
			bytes[i + start] = value[i];
		for (int i = count; i < max_length; i++)
			bytes[i + start] = 0;