package org.codehaus.plexus.archiver.tar;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FilterInputStream;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import org.codehaus.plexus.archiver.ArchiveFile;
implements ArchiveFile
{
private final java.io.File file;
private TarInputStream inputStream;
private TarEntry currentEntry;
{
this.file = file;
}
throws IOException
{
if ( inputStream != null )
{
close();
}
open();
return new Enumeration()
{
boolean currentEntryValid;
{
if ( !currentEntryValid )
{
try
{
currentEntry = inputStream.getNextEntry();
}
catch ( IOException e )
{
throw new UndeclaredThrowableException( e );
}
}
return currentEntry != null;
}
{
if ( currentEntry == null )
{
throw new NoSuchElementException();
}
currentEntryValid = false;
return currentEntry;
}
};
}
throws IOException
{
if ( inputStream != null )
{
inputStream.close();
inputStream = null;
}
}
throws IOException
{
return getInputStream( (TarEntry) entry );
}
throws IOException
{
if ( entry.equals( (Object) currentEntry ) && inputStream != null )
{
return new FilterInputStream( inputStream )
{
throws IOException
{
}
};
}
return getInputStream( entry, currentEntry );
}
throws IOException
{
return new FileInputStream( file );
}
private InputStream
getInputStream( TarEntry entry, TarEntry currentEntry )
throws IOException
{
if ( currentEntry == null || inputStream == null )
{
if ( inputStream != null )
{
close();
}
open();
if ( !findEntry( entry, null ) )
{
throw new IOException( "Unknown entry: " + entry.getName() );
}
}
else
{
if ( findEntry( entry, null ) )
{
return getInputStream( entry );
}
close();
open();
if ( !findEntry( entry, currentEntry ) )
{
throw new IOException( "No such entry: " + entry.getName() );
}
}
return getInputStream( entry );
}
throws IOException
{
inputStream = new TarInputStream( getInputStream( file ) );
}
private boolean findEntry( TarEntry entry, TarEntry currentEntry)
throws IOException
{
for (;;)
{
this.currentEntry = inputStream.getNextEntry();
if ( this.currentEntry == null
|| (currentEntry != null && this.currentEntry.equals( currentEntry ) ) )
{
return false;
}
if ( this.currentEntry.equals( entry ) )
{
return true;
}
}
}
}