| 1 |
/* |
|---|
| 2 |
* LastPod is an application used to publish one's iPod play counts to Last.fm. |
|---|
| 3 |
* Copyright (C) 2007 Chris Tilden |
|---|
| 4 |
* |
|---|
| 5 |
* This program is free software; you can redistribute it and/or |
|---|
| 6 |
* modify it under the terms of the GNU General Public License |
|---|
| 7 |
* as published by the Free Software Foundation; either version 2 |
|---|
| 8 |
* of the License, or (at your option) any later version. |
|---|
| 9 |
* |
|---|
| 10 |
* This program is distributed in the hope that it will be useful, |
|---|
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 |
* GNU General Public License for more details. |
|---|
| 14 |
* |
|---|
| 15 |
* You should have received a copy of the GNU General Public License |
|---|
| 16 |
* along with this program; if not, write to the Free Software |
|---|
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 18 |
*/ |
|---|
| 19 |
package org.lastpod; |
|---|
| 20 |
|
|---|
| 21 |
import org.lastpod.parser.TrackItemParser; |
|---|
| 22 |
|
|---|
| 23 |
import java.io.IOException; |
|---|
| 24 |
import java.io.InputStream; |
|---|
| 25 |
|
|---|
| 26 |
import java.math.BigInteger; |
|---|
| 27 |
|
|---|
| 28 |
import java.util.ArrayList; |
|---|
| 29 |
import java.util.List; |
|---|
| 30 |
|
|---|
| 31 |
/** |
|---|
| 32 |
* Reads the iTunes database directly from the iPod. |
|---|
| 33 |
* @author muti |
|---|
| 34 |
* @author Chris Tilden |
|---|
| 35 |
* @version $Id$ |
|---|
| 36 |
*/ |
|---|
| 37 |
public class DbReader { |
|---|
| 38 |
/** |
|---|
| 39 |
* Parses the itunesDatabase. |
|---|
| 40 |
*/ |
|---|
| 41 |
private TrackItemParser itunesDbParser; |
|---|
| 42 |
|
|---|
| 43 |
/** |
|---|
| 44 |
* Parses the Play Counts file. |
|---|
| 45 |
*/ |
|---|
| 46 |
private TrackItemParser playCountsParser; |
|---|
| 47 |
|
|---|
| 48 |
/** |
|---|
| 49 |
* A list of the recently played tracks from the iPod play counts file. |
|---|
| 50 |
* (Sorted by play time. This is important because otherwise Last.fm will |
|---|
| 51 |
* reject them.) |
|---|
| 52 |
*/ |
|---|
| 53 |
private List recentplays; |
|---|
| 54 |
|
|---|
| 55 |
/** |
|---|
| 56 |
* Default constructor should not be used. |
|---|
| 57 |
*/ |
|---|
| 58 |
private DbReader() { |
|---|
| 59 |
/* Default constructor. */ |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
/** |
|---|
| 63 |
* Initializes the class with the locations of the iPod DB files. |
|---|
| 64 |
* |
|---|
| 65 |
*/ |
|---|
| 66 |
public DbReader(TrackItemParser itunesDbParser, TrackItemParser playCountsParser) { |
|---|
| 67 |
this.itunesDbParser = itunesDbParser; |
|---|
| 68 |
this.recentplays = new ArrayList(); |
|---|
| 69 |
this.playCountsParser = playCountsParser; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
/** |
|---|
| 73 |
* Gets the recent plays. |
|---|
| 74 |
* @return Returns recent plays. |
|---|
| 75 |
*/ |
|---|
| 76 |
public List getRecentplays() { |
|---|
| 77 |
return recentplays; |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
/** |
|---|
| 81 |
* Attempts to open and parse the DB & Play Counts files, creating |
|---|
| 82 |
* the appropriate data structures. |
|---|
| 83 |
*/ |
|---|
| 84 |
public void parse() { |
|---|
| 85 |
List trackList = itunesDbParser.parse(); |
|---|
| 86 |
|
|---|
| 87 |
playCountsParser.setTrackList(trackList); |
|---|
| 88 |
recentplays = playCountsParser.parse(); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
/** |
|---|
| 92 |
* This converts any size byte array to a BigInteger. |
|---|
| 93 |
* @param num Little-Endian byte array. |
|---|
| 94 |
* @return A BigInt. |
|---|
| 95 |
*/ |
|---|
| 96 |
public static BigInteger littleEndianToBigInt(byte[] num) { |
|---|
| 97 |
byte temp; |
|---|
| 98 |
|
|---|
| 99 |
int upperBound = num.length - 1; |
|---|
| 100 |
int lowerBound = 0; |
|---|
| 101 |
|
|---|
| 102 |
while (lowerBound < upperBound) { |
|---|
| 103 |
temp = num[lowerBound]; |
|---|
| 104 |
num[lowerBound] = num[upperBound]; |
|---|
| 105 |
num[upperBound] = temp; |
|---|
| 106 |
lowerBound++; |
|---|
| 107 |
upperBound--; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
return new BigInteger(1, num); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
/** |
|---|
| 114 |
* Guarantees that the specified number of bytes will be skipped. |
|---|
| 115 |
* @param stream Input Stream. |
|---|
| 116 |
* @param bytes Number of bytes to skip. |
|---|
| 117 |
* @throws IOException Thrown if errors occur. |
|---|
| 118 |
*/ |
|---|
| 119 |
public static void skipFully(InputStream stream, long bytes) |
|---|
| 120 |
throws IOException { |
|---|
| 121 |
for (long i = stream.skip(bytes); i < bytes; i += stream.skip(bytes - i)) { |
|---|
| 122 |
/* The loop itself performs all the logic needed to skip. */ |
|---|
| 123 |
} |
|---|
| 124 |
} |
|---|
| 125 |
} |
|---|