Changeset 68

Show
Ignore:
Timestamp:
05/20/07 01:43:50 (3 years ago)
Author:
chris
Message:

app now makes sure the DbReader? is closing out it's file streams properly

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/org/lastpod/DbReader.java

    r67 r68  
    1919package org.lastpod; 
    2020 
     21import org.lastpod.util.IoUtils; 
     22 
    2123import java.io.BufferedInputStream; 
    2224import java.io.File; 
    2325import java.io.FileInputStream; 
    2426import java.io.IOException; 
     27import java.io.InputStream; 
    2528 
    2629import java.math.BigInteger; 
     
    5356 
    5457    /** 
    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     /** 
    6558     * A list of all the tracks from the iTunes databaes file. 
    6659     */ 
     
    6861 
    6962    /** 
    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>. 
    7164     */ 
    7265    boolean parseVariousArtists; 
     
    9184     * @param itunespath  Directory containing the iTunesDB and the corresponding 
    9285     *                         Play Counts, including trailing "\" or "/". 
     86     * @param parseVariousArtists  If <code>true</code> then parses "Various 
     87     * Artists" 
    9388     */ 
    9489    public DbReader(String itunespath, boolean parseVariousArtists) { 
     
    119114     */ 
    120115    public void parse() throws IOException { 
     116        InputStream itunesFileIn = null; 
     117        InputStream itunesBufferedIn = null; 
     118        InputStream playCountsFileIn = null; 
     119        InputStream playCountsBufferedIn = null; 
     120 
    121121        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); 
    124126        } catch (IOException e) { 
    125127            throw new IOException("Error reading iTunes Database"); 
     128        } finally { 
     129            IoUtils.cleanup(itunesFileIn, null); 
     130            IoUtils.cleanup(itunesBufferedIn, null); 
    126131        } 
    127132 
    128133        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); 
    131138        } catch (IOException e) { 
    132139            String errorMsg = 
     
    136143                + "to automatically run iTunes when an iPod is detected."; 
    137144            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        } 
    145149    } 
    146150 
    147151    /** 
    148152     * 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 { 
    152158        byte[] buf = new byte[1]; 
    153159 
     
    160166 
    161167                if (new String(buf).equals("hit")) { 
    162                     tracklist.add(parsemhit()); 
     168                    tracklist.add(parsemhit(itunesistream)); 
    163169                } else { 
    164170                    itunesistream.reset(); 
     
    172178    /** 
    173179     * Parses an MHIT object from the iTunes Database. 
     180     * @param itunesistream  A stream that reads the iTunes database file. 
    174181     * @return Returns parsed track object. 
    175182     * @throws IOException  Thrown if errors occur. 
    176183     */ 
    177     public TrackItem parsemhit() throws IOException { 
     184    public TrackItem parsemhit(InputStream itunesistream) 
     185            throws IOException { 
    178186        byte[] dword = new byte[4]; 
    179187        TrackItem track = new TrackItem(); 
     
    202210 
    203211        for (long i = 0; i < nummhods; i++) { 
    204             parsemhod(track); 
     212            parsemhod(track, itunesistream); 
    205213        } 
    206214 
     
    211219     * Parses an MHOD object and sets proper fields in the track item object. 
    212220     * @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 { 
    216226        byte[] dword = new byte[4]; 
    217227 
     
    265275    /** 
    266276     * 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 { 
    270282        byte[] dword = new byte[4]; 
    271283 
     
    345357     * @throws IOException  Thrown if errors occur. 
    346358     */ 
    347     public static void skipFully(BufferedInputStream stream, long bytes) 
     359    public static void skipFully(InputStream stream, long bytes) 
    348360            throws IOException { 
    349361        for (long i = stream.skip(bytes); i < bytes; i += stream.skip(bytes - i)) {