001/*
002** Tim feel free to integrate this code here.
003**
004** This code has been placed into the Public Domain.
005** This code was written by David M. Gaskin in 1999.
006**
007*/
008
009package com.ice.tar;
010
011import java.io.IOException;
012import java.util.Enumeration;
013import java.util.NoSuchElementException;
014
015/**
016 * Enumerate the contents of a "tar" file.
017 *
018 * Last updated 26th Mar 1999.
019 *
020 * @author  David. M. Gaskin.
021 * @version Version 1.0 Mar 1999
022 * @since    Version 1.0
023 */
024
025public
026class           TarEntryEnumerator
027implements      Enumeration
028        {
029        /**
030         * The instance on which the enumeration works.
031         */
032        private TarInputStream  tis = null;
033
034        /**
035         * Has EndOfFile been reached?
036         */
037        private boolean                 eof = false;
038
039        /**
040         * The read ahead entry (or <B><I>null</I></B> if no read ahead exists)
041         */
042        private TarEntry                readAhead = null;
043
044        /**
045         * Construct an instance given a TarInputStream. This method is package
046         * private because it is not initially forseen that an instance of this class
047         * should be constructed from outside the package. Should it become necessary
048         * to construct an instance of this class from outside the package in which it
049         * exists then the constructor should be made <B>protected</B> and an empty
050         * subclass should be written in the other package.
051         *
052         * @param <B>tis</B> the <B>TarInputStream</B> on which this enumeration has
053         *  to be based.
054         */
055        public
056        TarEntryEnumerator( TarInputStream tis )
057                {
058                this.tis      = tis;
059                eof           = false;
060                }
061
062        /**
063         * Return the next element in the enumeration. This is a required method
064         * for implementing <B>java.util.Enumeration</B>.
065         *
066         * @return the next Object in the enumeration
067         * @exception <B>NoSuchElementException</B> should an attempt be made to
068         *  read beyond EOF
069         */
070        public Object
071        nextElement()
072                throws NoSuchElementException
073                {
074                if ( eof && ( readAhead == null ) )
075                        throw new NoSuchElementException();
076
077                TarEntry rc = null;
078                if ( readAhead != null )
079                        {
080                        rc        = readAhead;
081                        readAhead = null;
082                        }
083                else
084                        {
085                        rc = getNext();
086                        }
087
088                return rc;
089                }
090
091        /**
092         * Return <B>true</B> if there are more elements in the enumeration.
093         *
094         * @return <B>true</B> if there are more elements in the enumeration.
095         */
096        public boolean
097        hasMoreElements()
098                {
099                if (eof)
100                        return false;
101
102                boolean rc = false;
103                readAhead = getNext();
104                if ( readAhead != null )
105                        rc = true;
106
107                return rc;
108                }
109
110        /**
111         * Return the next element of <B>null</B> if there is no next element or
112         * if an error occured.
113         *
114         * @return the next element of <B>null</B> if there is no next element or
115         * if an error occured.
116         */
117        private TarEntry
118        getNext()
119                {
120                TarEntry rc = null;
121                try {
122                        rc = tis.getNextEntry();
123                        }
124                catch ( IOException ex )
125                        {
126                        // null will be returned but should not occur
127                        ex.printStackTrace();
128                        }
129
130                if ( rc == null )
131                        eof = true;
132
133                return rc;
134                }
135        }