org.at4j.archive
Class ArchiveEntryCollaborator<T extends ArchiveEntry<T,U>,U extends ArchiveDirectoryEntry<T,U>>

java.lang.Object
  extended by org.at4j.archive.ArchiveEntryCollaborator<T,U>
Type Parameters:
T - The type of entries in the archive.
U - The type of directory entries in the archive.
All Implemented Interfaces:
RandomAccessCloseObserver
Direct Known Subclasses:
ZipEntryCollaborator

public abstract class ArchiveEntryCollaborator<T extends ArchiveEntry<T,U>,U extends ArchiveDirectoryEntry<T,U>>
extends Object
implements RandomAccessCloseObserver

This is a collaborator object that gives archive entries access to the archive.

This object should be safe to use concurrently from several threads. If the archive file is in a locking FileSystem, clients should obtain read locks on the archive file manually for other threads than the one creating the Archive object.

Note: This object is part of the archive implementation. Clients don't have to bother with this.

Since:
1.0
Author:
Karl Gustafsson

Constructor Summary
protected ArchiveEntryCollaborator(RandomlyAccessibleFile archiveFile, Map<AbsoluteLocation,T> entryMap)
          Create a new collaborator.
 
Method Summary
 void close()
          Close the collaborator.
protected  void finalize()
           
 T getEntry(AbsoluteLocation loc)
          Get the archive entry stored at the specified absolute location in the archive.
 void notifyClosed(RandomAccess ra)
          This callback method is called when a stream is closed on a file entry.
 RandomAccess openRandomAccess(long pos, long upperBound)
          Open a read only RandomAccess object that reads data for a file entry.
 InputStream openStream(long pos, long upperBound)
          Open an InputStream for reading the data for a file entry.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArchiveEntryCollaborator

protected ArchiveEntryCollaborator(RandomlyAccessibleFile archiveFile,
                                   Map<AbsoluteLocation,T> entryMap)
Create a new collaborator.

Parameters:
archiveFile - The archive file. This file has to be locked by reading while this object is alive. If several threads access the archive file, all threads must own a read lock on the archive file (if it is in a locking file system).
entryMap - A map containing all of the entries in the archive. This object will use the Map instance that is supplied in the argument (i.e. not make a defensive copy of it). The map does not have to contain all archive entries when this object is created, but it must do so before any client starts to use the archive object.
Method Detail

openStream

public InputStream openStream(long pos,
                              long upperBound)
Open an InputStream for reading the data for a file entry.

Parameters:
pos - The file's data's start position in the archive.
upperBound - The file's data's last position in the archive.
Returns:
An InputStream for reading data between the boundaries.

openRandomAccess

public RandomAccess openRandomAccess(long pos,
                                     long upperBound)
Open a read only RandomAccess object that reads data for a file entry.

Parameters:
pos - The start position of the file data.
upperBound - The last position of the file data in the archive.
Returns:
A RandomAccess for reading data between the boundaries.

getEntry

public T getEntry(AbsoluteLocation loc)
Get the archive entry stored at the specified absolute location in the archive.

Parameters:
loc - The entry's position in the archive.
Returns:
An archive entry or null if there is no entry at the specified location.

notifyClosed

public void notifyClosed(RandomAccess ra)
This callback method is called when a stream is closed on a file entry.

Specified by:
notifyClosed in interface RandomAccessCloseObserver
Parameters:
ra - The closed RandomAccess.

close

public void close()
Close the collaborator. This method closes all open streams on file entries in the archive.


finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable