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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
/*
TreeSet for runtimedatabase with entry hash used in compareto
fix commandline interface.
*/
public class TagDatabase {
protected static TagDatabase instance=null;
protected TreeMap songs;
protected TreeMap files;
protected TreeMap filehashes;
protected TreeMap albums;
protected TreeMap artists;
protected int artiststart,albumstart,songstart,filestart;
protected int artistcount,albumcount,songcount,filecount;
public int artistlen,albumlen,songlen,genrelen,filelen,songarraylen,albumarraylen;
public String strip,add;
public boolean haveOldDatabase,dirisalbum,dirisalbumname,showduplicates;
protected Vector sortedsongs,sortedfiles,sortedalbums,sortedartists;
protected TagDatabase() {
songs=new TreeMap();
files=new TreeMap();
filehashes=new TreeMap();
albums=new TreeMap();
artists=new TreeMap();
strip=null;
add=null;
haveOldDatabase=false;
dirisalbum=false;
dirisalbumname=true;
showduplicates=true;
}
public static TagDatabase getInstance() {
if(instance==null)
instance=new TagDatabase();
return instance;
}
public void removeFileEntry(File file) {
String key = file.getAbsolutePath();
files.remove(key);
}
public FileEntry getFileEntry(File file) throws FileNotFoundException, IOException {
String key = file.getAbsolutePath();
if(!files.containsKey(key)) {
FileEntry f = new FileEntry(file);
files.put(key,f);
return f;
}
else
return (FileEntry)files.get(key);
}
public ArtistEntry getArtistEntry(String name) {
String key = name.toLowerCase();
if(!artists.containsKey(key)) {
ArtistEntry a = new ArtistEntry(name);
artists.put(key,a);
return a;
}
else
return (ArtistEntry)artists.get(key);
}
public String getAlbumKey(String name, String directory) {
if(dirisalbum)
return directory;
else
return name.toLowerCase()+"___"+directory;
}
public AlbumEntry getAlbumEntry(String name,String directory) {
String key = getAlbumKey(name,directory);
if(!albums.containsKey(key)) {
AlbumEntry a = new AlbumEntry(name);
albums.put(key,a);
return a;
}
else
return (AlbumEntry)albums.get(key);
}
public void removeSongEntry(FileEntry file) {
String key = file.getFilename();
songs.remove(key);
file.setSongEntry(null);
}
public SongEntry getSongEntry(FileEntry file) {
String key = file.getFilename();
if(!songs.containsKey(key)) {
SongEntry s = new SongEntry(file);
songs.put(key,s);
return s;
}
else
return (SongEntry)songs.get(key);
}
private class SongFilter implements FileFilter {
public boolean accept(File f) {
if(f.isDirectory()) // always accept directories.
return true;
String name=f.getName();
return name.endsWith(".mp3")||name.endsWith(".ogg");
}
}
public void add(File f) {
if(!f.isDirectory()) {
if(f.isFile()) {
addSong(f);
}
}
else {
File[] files = f.listFiles(new SongFilter());
int length=Array.getLength(files);
System.out.println(FileEntry.convertPath(f.getAbsolutePath()));
for(int i=0;i<length;i++) {
add(files[i]);
}
}
}
protected FileEntry addSong(File f) {
FileEntry file = null;
try {
file = getFileEntry(f);
}
catch(Exception e) {
return null;
}
SongEntry song = getSongEntry(file);
if(!song.gotTagInfo()) {
removeSongEntry(file);
return null;
}
ArtistEntry artist = getArtistEntry(song.getArtistTag());
AlbumEntry album = getAlbumEntry(song.getAlbumTag(),f.getParent());
album.setArtist(artist);
album.addSong(song);
return file;
}
protected int align(int len) {
while((len&3)!=0) len++;
return len;
}
protected void calcLimits() {
ArtistEntry longartist=null,longalbumarray=null;
AlbumEntry longalbum=null, longsongarray=null;
SongEntry longsong=null,longgenre=null;
FileEntry longfile=null;
Iterator i;
artistlen=0;
albumarraylen=0;
i=sortedartists.iterator();
while(i.hasNext()) {
ArtistEntry artist = (ArtistEntry) i.next();
int length=artist.getName().length();
int albumcount=artist.size();
if(length > artistlen) {
artistlen=align(length);
longartist=artist;
}
if(albumcount> albumarraylen) {
albumarraylen=albumcount;
longalbumarray=artist;
}
}
artistcount=sortedartists.size();
if(longartist!=null)
System.out.println("Artist with longest name ("+artistlen+") :"+longartist.getName());
if(longalbumarray!=null)
System.out.println("Artist with most albums ("+albumarraylen+") :"+longalbumarray.getName());
albumlen=0;
songarraylen=0;
i=sortedalbums.iterator();
while(i.hasNext()) {
AlbumEntry album = (AlbumEntry) i.next();
int length=album.getName().length();
int songcount=album.size();
if(length > albumlen) {
albumlen=align(length);
longalbum=album;
}
if(songcount> songarraylen) {
songarraylen=songcount;
longsongarray=album;
}
}
albumcount=sortedalbums.size();
if(longalbum!=null)
System.out.println("Album with longest name ("+albumlen+") :"+longalbum.getName());
if(longsongarray!=null)
System.out.println("Album with most songs ("+songarraylen+") :"+longsongarray.getName());
filelen=0;
i=sortedfiles.iterator();
while(i.hasNext()) {
FileEntry file = (FileEntry) i.next();
int length=file.getFilename().length();
if(length> filelen) {
filelen=align(length);
longfile=file;
}
}
filecount=sortedfiles.size();
if(longfile!=null)
System.out.println("File with longest filename ("+filelen+") :"+longfile.getFilename());
songlen=0;
genrelen=0;
i=sortedsongs.iterator();
while(i.hasNext()) {
SongEntry song = (SongEntry) i.next();
int tlength=song.getName().length();
int glength=song.getGenreTag().length();
if(tlength> songlen) {
songlen=align(tlength);
longsong=song;
}
if(glength> genrelen) {
genrelen=align(glength);
longgenre=song;
}
}
songcount=sortedsongs.size();
if(longsong!=null)
System.out.println("Song with longest name ("+songlen+") :"+longsong.getName());
if(longsong!=null)
System.out.println("Song with longest genre ("+genrelen+") :"+longgenre.getGenreTag());
System.out.println("Artistcount: "+artistcount);
System.out.println("Albumcount : "+albumcount);
System.out.println("Songcount : "+songcount);
System.out.println("Filecount : "+filecount);
artiststart=68;
albumstart=artiststart+artistcount*ArtistEntry.entrySize();
songstart=albumstart+albumcount*AlbumEntry.entrySize();
filestart=songstart+songcount*SongEntry.entrySize();
}
protected void calcOffsets() {
Iterator i;
int offset=artiststart;
i=sortedartists.iterator();
while(i.hasNext()) {
Entry e = (Entry) i.next();
e.setOffset(offset);
offset+=ArtistEntry.entrySize();
}
// assert(offset==albumstart);
i=sortedalbums.iterator();
while(i.hasNext()) {
Entry e = (Entry) i.next();
e.setOffset(offset);
offset+=AlbumEntry.entrySize();
}
// assert(offset==songstart);
i=sortedsongs.iterator();
while(i.hasNext()) {
Entry e = (Entry) i.next();
e.setOffset(offset);
offset+=SongEntry.entrySize();
}
// assert(offset==filestart);
i=sortedfiles.iterator();
while(i.hasNext()) {
Entry e = (Entry) i.next();
e.setOffset(offset);
offset+=FileEntry.entrySize();
}
}
protected void calcHashes() {
Iterator i;
i=sortedfiles.iterator();
while(i.hasNext()) {
FileEntry file = (FileEntry) i.next();
Integer key = new Integer(file.getHash());
if(!filehashes.containsKey(key))
filehashes.put(key,file);
else {
System.out.println("Duplicate hash:");
System.out.println(((FileEntry)filehashes.get(key)).getFilename());
System.out.println(file.getFilename());
}
}
}
protected void writeHeader(DataOutputStream w) throws IOException {
w.write('R');
w.write('D');
w.write('B');
w.write(0x3);
w.writeInt(artiststart);
w.writeInt(albumstart);
w.writeInt(songstart);
w.writeInt(filestart);
w.writeInt(artistcount);
w.writeInt(albumcount);
w.writeInt(songcount);
w.writeInt(filecount);
w.writeInt(artistlen);
w.writeInt(albumlen);
w.writeInt(songlen);
w.writeInt(genrelen);
w.writeInt(filelen);
w.writeInt(songarraylen);
w.writeInt(albumarraylen);
w.writeInt(RuntimeDatabase.getInstance().isDirty());
}
public void prepareWrite() {
System.out.println("Sorting artists..");
sortedartists=new Vector();
sortedartists.addAll(artists.values());
Collections.sort(sortedartists);
System.out.println("Sorting albums..");
sortedalbums=new Vector();
sortedalbums.addAll(albums.values());
Collections.sort(sortedalbums);
System.out.println("Sorting songs..");
sortedsongs=new Vector();
sortedsongs.addAll(songs.values());
Collections.sort(sortedsongs);
System.out.println("Sorting files..");
sortedfiles=new Vector();
sortedfiles.addAll(files.values());
Collections.sort(sortedfiles);
System.out.println("Calculating tag database limits..");
calcLimits();
System.out.println("Calculating tag database offsets..");
calcOffsets();
if(showduplicates) {
System.out.println("Comparing file hashes..");
calcHashes();
}
}
public void writeDatabase(File f) throws IOException {
int x;
Iterator i;
DataOutputStream w = new DataOutputStream(new FileOutputStream(f));
System.out.println("Writing tag database..");
writeHeader(w);
i=sortedartists.iterator();
while(i.hasNext()) {
Entry e = (Entry) i.next();
e.write(w);
}
i=sortedalbums.iterator();
while(i.hasNext()) {
Entry e = (Entry) i.next();
e.write(w);
}
i=sortedsongs.iterator();
while(i.hasNext()) {
Entry e = (Entry) i.next();
e.write(w);
}
i=sortedfiles.iterator();
while(i.hasNext()) {
Entry e = (Entry) i.next();
e.write(w);
}
// done...
w.flush();
w.close();
}
}
|