Changeset 68
- Timestamp:
- 05/20/07 01:43:50 (3 years ago)
- Files:
-
- trunk/src/main/org/lastpod/DbReader.java (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/org/lastpod/DbReader.java
r67 r68 19 19 package org.lastpod; 20 20 21 import org.lastpod.util.IoUtils; 22 21 23 import java.io.BufferedInputStream; 22 24 import java.io.File; 23 25 import java.io.FileInputStream; 24 26 import java.io.IOException; 27 import java.io.InputStream; 25 28 26 29 import java.math.BigInteger; … … 53 56 54 57 /** 55 * A buffered stream that reads the iTunes database file.56 */57 private BufferedInputStream itunesistream;58 59 /**60 * A buffered stream that reads the iPod play counts file.61 */62 private BufferedInputStream playcountsistream;63 64 /**65 58 * A list of all the tracks from the iTunes databaes file. 66 59 */ … … 68 61 69 62 /** 70 * Stores a boolean value that will be passed into <code>TrackItem</code> 63 * Stores a boolean value that will be passed into <code>TrackItem</code>. 71 64 */ 72 65 boolean parseVariousArtists; … … 91 84 * @param itunespath Directory containing the iTunesDB and the corresponding 92 85 * Play Counts, including trailing "\" or "/". 86 * @param parseVariousArtists If <code>true</code> then parses "Various 87 * Artists" 93 88 */ 94 89 public DbReader(String itunespath, boolean parseVariousArtists) { … … 119 114 */ 120 115 public void parse() throws IOException { 116 InputStream itunesFileIn = null; 117 InputStream itunesBufferedIn = null; 118 InputStream playCountsFileIn = null; 119 InputStream playCountsBufferedIn = null; 120 121 121 try { 122 FileInputStream itstream = new FileInputStream(itunesfile); 123 itunesistream = new BufferedInputStream(itstream, 65535); 122 itunesFileIn = new FileInputStream(itunesfile); 123 itunesBufferedIn = new BufferedInputStream(itunesFileIn, 65535); 124 125 parseitunesdb(itunesBufferedIn); 124 126 } catch (IOException e) { 125 127 throw new IOException("Error reading iTunes Database"); 128 } finally { 129 IoUtils.cleanup(itunesFileIn, null); 130 IoUtils.cleanup(itunesBufferedIn, null); 126 131 } 127 132 128 133 try { 129 FileInputStream pcstream = new FileInputStream(playcountsfile); 130 playcountsistream = new BufferedInputStream(pcstream, 65535); 134 playCountsFileIn = new FileInputStream(playcountsfile); 135 playCountsBufferedIn = new BufferedInputStream(playCountsFileIn, 65535); 136 137 parseplaycounts(playCountsBufferedIn); 131 138 } catch (IOException e) { 132 139 String errorMsg = … … 136 143 + "to automatically run iTunes when an iPod is detected."; 137 144 throw new IOException(errorMsg); 138 } 139 140 parseitunesdb(); 141 parseplaycounts(); 142 143 itunesistream.close(); 144 playcountsistream.close(); 145 } finally { 146 IoUtils.cleanup(playCountsFileIn, null); 147 IoUtils.cleanup(playCountsBufferedIn, null); 148 } 145 149 } 146 150 147 151 /** 148 152 * Parses track information from the iTunesDB. 149 * @throws IOException Thrown if errors occur. 150 */ 151 public void parseitunesdb() throws IOException { 153 * @param itunesistream A stream that reads the iTunes database file. 154 * @throws IOException Thrown if errors occur. 155 */ 156 public void parseitunesdb(InputStream itunesistream) 157 throws IOException { 152 158 byte[] buf = new byte[1]; 153 159 … … 160 166 161 167 if (new String(buf).equals("hit")) { 162 tracklist.add(parsemhit( ));168 tracklist.add(parsemhit(itunesistream)); 163 169 } else { 164 170 itunesistream.reset(); … … 172 178 /** 173 179 * Parses an MHIT object from the iTunes Database. 180 * @param itunesistream A stream that reads the iTunes database file. 174 181 * @return Returns parsed track object. 175 182 * @throws IOException Thrown if errors occur. 176 183 */ 177 public TrackItem parsemhit() throws IOException { 184 public TrackItem parsemhit(InputStream itunesistream) 185 throws IOException { 178 186 byte[] dword = new byte[4]; 179 187 TrackItem track = new TrackItem(); … … 202 210 203 211 for (long i = 0; i < nummhods; i++) { 204 parsemhod(track );212 parsemhod(track, itunesistream); 205 213 } 206 214 … … 211 219 * Parses an MHOD object and sets proper fields in the track item object. 212 220 * @param track Track Item. 213 * @throws IOException Thrown if errors occur. 214 */ 215 public void parsemhod(TrackItem track) throws IOException { 221 * @param itunesistream A stream that reads the iTunes database file. 222 * @throws IOException Thrown if errors occur. 223 */ 224 public void parsemhod(TrackItem track, InputStream itunesistream) 225 throws IOException { 216 226 byte[] dword = new byte[4]; 217 227 … … 265 275 /** 266 276 * Parses play counts information from "Play Counts". 267 * @throws IOException Thrown if errors occur. 268 */ 269 public void parseplaycounts() throws IOException { 277 * @param playcountsistream A stream that reads the iPod play counts file. 278 * @throws IOException Thrown if errors occur. 279 */ 280 public void parseplaycounts(InputStream playcountsistream) 281 throws IOException { 270 282 byte[] dword = new byte[4]; 271 283 … … 345 357 * @throws IOException Thrown if errors occur. 346 358 */ 347 public static void skipFully( BufferedInputStream stream, long bytes)359 public static void skipFully(InputStream stream, long bytes) 348 360 throws IOException { 349 361 for (long i = stream.skip(bytes); i < bytes; i += stream.skip(bytes - i)) {
