summaryrefslogtreecommitdiffstats
path: root/source/FieldPair.java
blob: d3804c66f79c219b0ffaa82511b817b543625af1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Created on May 6, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
Copyright 2003 Joseph Barnett

This File is part of "one 2 oh my god"

"one 2 oh my god" is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Free Software Foundation; either version 2 of the License, or
your option) any later version.

"one 2 oh my god" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with "one 2 oh my god"; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */
package itunes;
import java.io.*;
import java.util.ArrayList;

import itunes.client.request.Request;
import itunes.client.Song;
/**
 * @author jbarnett
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class FieldPair {
	public String name;
	public byte[] value;
	public byte[] transmission;
	
	public FieldPair(String name, int value) {
		byte[] bytes = new byte[4];
		enterInt(bytes, value, 0);
		init(name,bytes,0,bytes.length);
	}
	
	public FieldPair(String name, Song s, String meta) {
		String[] extraTags = meta.split(",");
		FieldPair miid = new FieldPair("miid", s.id);
		FieldPair minm = new FieldPair("minm", s.getName());
		ArrayList songPairs = new ArrayList();
		songPairs.add(miid); songPairs.add(minm);
		for (int i = 0; i < extraTags.length; i++) {
			// add FieldPairs for each meta tag
			if (!extraTags[i].equals("dmap.itemid") && !extraTags[i].equals("dmap.itemname")) { //already do these	
				FieldPair newPair=new FieldPair(lookup(extraTags[i]), lookup(extraTags[i], s));
				if (!newPair.name.equals("null"))
					songPairs.add(newPair);
			}
		}
		FieldPair[] array = new FieldPair[songPairs.size()];
		System.arraycopy(songPairs.toArray(), 0, array, 0, array.length);
		byte[] bytes = embed(array);
		init(name,bytes,0,bytes.length);
	}
	
	public FieldPair(String name, FieldPair[] fields) {
		byte[] bytes = embed(fields);	
		init(name,bytes,0,bytes.length);
	}
	
	public FieldPair(String name, byte[] value) {
		init(name,value,0,value.length);	
	}
	
	public FieldPair(String name, byte value) {
		byte[] bytes = {value};
		init(name,bytes,0,bytes.length);
	}
	public FieldPair(String name, byte[] value,int offset, int size) {
		init(name,value,offset,size);
	}
	public FieldPair (String name, String value) {
		init(name, value.getBytes(), 0, value.getBytes().length);	
	}
	
	public void init(String name, byte[] value, int offset ,int size) {
		this.name = name;
		this.value = new byte[size];
		try {
			ByteArrayInputStream b = new ByteArrayInputStream(value,offset,size);
			DataInputStream d = new DataInputStream(b);
			d.readFully(this.value);
		} catch (IOException e) {
			e.printStackTrace();
		}
		transmission = getDaapBytes();
	}

	public String toString() {
		String ret = name + " ";
		for (int i = 0; i < value.length;i++) {
			ret+= Integer.toHexString(0xFF&value[i]) + " ";
		}
		ret += "("+ Request.readString(value,0,value.length)+")";

		return ret;
	}
	
	private byte[] getDaapBytes() {
		byte[] bytes = new byte[4+value.length+name.length()];
		System.arraycopy(name.getBytes(), 0, bytes, 0, name.length());
		int length = value.length;
		byte[] lengthBytes = new byte[4];
		lengthBytes[3] = (byte)(length % 256);
		length = length / 256;
		lengthBytes[2] = (byte)(length % 256);
		length = length / 256;
		lengthBytes[1] = (byte)(length % 256);
		length = length / 256;
		lengthBytes[0] = (byte)(length % 256);
		System.arraycopy(lengthBytes,0,bytes, name.length(), 4);
		System.arraycopy(value, 0, bytes, name.length() + 4, value.length);
		return bytes;	
	}
	
	public boolean equals(Object o) {
		if (FieldPair.class.isInstance(o)) {
			return (((FieldPair)o).name.equals(this.name));
		} else if (String.class.isInstance(o)) {
			return ((String)o).equals(this.name);
		} else return false;
	}
	
	public static void enterInt(byte[] data, int value, int offset) {
		int temp = value;
		for (int i = offset; i < offset + 4; i++) {
			data[offset + 3 - i] = (byte)( temp % 256);
			temp = temp / 256;
		}
	}
	
	private static byte[] embed(FieldPair[] fields) {
		int offset = 0;
		int length = 0;
		for (int i = 0; i < fields.length; i++) {
			length += fields[i].transmission.length;	
		}
		byte[] data = new byte[length];
		for (int i = 0; i < fields.length; i++) {
			System.arraycopy(fields[i].transmission, 0, data, offset, fields[i].transmission.length);
			offset += fields[i].transmission.length;
		}
		return data;
	}
	
	private static String lookup(String meta) {
		if (meta.equals("daap.songalbum")) {
			return "asal";	
		} else if (meta.equals("daap.songartist")) {
			return "asar";	
		} else if (meta.equals("daap.songtracknumber")) {
			return "astn";	
		} else if (meta.equals("daap.songuserrating")) {
			return "asur";	
		} else if (meta.equals("daap.songgenre")) {
			return "asgn";	
		} else if (meta.equals("daap.songformat")) {
			return "asfm";	
		} else if (meta.equals("daap.songtime")) {
			return "astm";	
		}
		return "null";
	}
	
	private static byte[] lookup(String meta, Song s) {
		if (meta.equals("daap.songalbum")) {
			return s.getAlbum().getBytes();	
		} else if (meta.equals("daap.songartist")) {
			return s.getArtist().getBytes();	
		} else if (meta.equals("daap.songtracknumber")) {
			byte[] intBytes = new byte[2];
			intBytes[0] = (byte)(s.getTrack() / 256);
			intBytes[1] = (byte)(s.getTrack() % 256);
			return intBytes;	
		} else if (meta.equals("daap.songuserrating")) {
			byte[] bytes = {(byte)0};
			return bytes;	
		}else if (meta.equals("daap.songgenre")) {
			return s.getGenre().getBytes();	
		} else if (meta.equals("daap.songformat")) {
			return "mp3".getBytes();	
		} else if (meta.equals("daap.songtime")) {
			byte[] intBytes = new byte[4];
			enterInt(intBytes, s.getTime(), 0);
			return intBytes;	
		}
		return new byte[0];	
	}
}