FileDocCategorySizeDatePackage
CompoundFileReader.javaAPI DocApache Lucene 2.1.07231Wed Feb 14 10:46:40 GMT 2007org.apache.lucene.index

CompoundFileReader

public class CompoundFileReader extends Directory
Class for accessing a compound stream. This class implements a directory, but is limited to only read operations. Directory methods that would normally modify data throw an exception.
author
Dmitry Serebrennikov
version
$Id: CompoundFileReader.java 472959 2006-11-09 16:21:50Z yonik $

Fields Summary
private Directory
directory
private String
fileName
private IndexInput
stream
private HashMap
entries
Constructors Summary
public CompoundFileReader(Directory dir, String name)



        
     
    
        directory = dir;
        fileName = name;

        boolean success = false;

        try {
            stream = dir.openInput(name);

            // read the directory and init files
            int count = stream.readVInt();
            FileEntry entry = null;
            for (int i=0; i<count; i++) {
                long offset = stream.readLong();
                String id = stream.readString();

                if (entry != null) {
                    // set length of the previous entry
                    entry.length = offset - entry.offset;
                }

                entry = new FileEntry();
                entry.offset = offset;
                entries.put(id, entry);
            }

            // set the length of the final entry
            if (entry != null) {
                entry.length = stream.length() - entry.offset;
            }

            success = true;

        } finally {
            if (! success && (stream != null)) {
                try {
                    stream.close();
                } catch (IOException e) { }
            }
        }
    
Methods Summary
public synchronized voidclose()

        if (stream == null)
            throw new IOException("Already closed");

        entries.clear();
        stream.close();
        stream = null;
    
public org.apache.lucene.store.IndexOutputcreateOutput(java.lang.String name)
Not implemented

throws
UnsupportedOperationException

        throw new UnsupportedOperationException();
    
public voiddeleteFile(java.lang.String name)
Not implemented

throws
UnsupportedOperationException

        throw new UnsupportedOperationException();
    
public booleanfileExists(java.lang.String name)
Returns true iff a file with the given name exists.

        return entries.containsKey(name);
    
public longfileLength(java.lang.String name)
Returns the length of a file in the directory.

throws
IOException if the file does not exist

        FileEntry e = (FileEntry) entries.get(name);
        if (e == null)
            throw new IOException("File " + name + " does not exist");
        return e.length;
    
public longfileModified(java.lang.String name)
Returns the time the compound file was last modified.

        return directory.fileModified(fileName);
    
public org.apache.lucene.store.DirectorygetDirectory()

        return directory;
    
public java.lang.StringgetName()

        return fileName;
    
public java.lang.String[]list()
Returns an array of strings, one for each file in the directory.

        String res[] = new String[entries.size()];
        return (String[]) entries.keySet().toArray(res);
    
public org.apache.lucene.store.LockmakeLock(java.lang.String name)
Not implemented

throws
UnsupportedOperationException

        throw new UnsupportedOperationException();
    
public synchronized org.apache.lucene.store.IndexInputopenInput(java.lang.String id)

        if (stream == null)
            throw new IOException("Stream closed");

        FileEntry entry = (FileEntry) entries.get(id);
        if (entry == null)
            throw new IOException("No sub-file with id " + id + " found");

        return new CSIndexInput(stream, entry.offset, entry.length);
    
public voidrenameFile(java.lang.String from, java.lang.String to)
Not implemented

throws
UnsupportedOperationException

        throw new UnsupportedOperationException();
    
public voidtouchFile(java.lang.String name)
Set the modified time of the compound file to now.

        directory.touchFile(fileName);