FileDocCategorySizeDatePackage
CompoundFileReader.javaAPI DocApache Lucene 1.4.37123Mon Jul 12 16:36:04 BST 2004org.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,v 1.7 2004/07/12 14:36:04 otis Exp $

Fields Summary
private Directory
directory
private String
fileName
private boolean
open
private org.apache.lucene.store.InputStream
stream
private HashMap
entries
Constructors Summary
public CompoundFileReader(Directory dir, String name)



        
     
    
        directory = dir;
        fileName = name;

        boolean success = false;

        try {
            stream = dir.openFile(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.OutputStreamcreateFile(java.lang.String name)
Creates a new, empty file in the directory with the given name. Returns a stream writing this file.

        throw new UnsupportedOperationException();
    
public voiddeleteFile(java.lang.String name)
Removes an existing file in the directory.

        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.

        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 named 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)
Construct a {@link Lock}.

param
name the name of the lock file

        throw new UnsupportedOperationException();
    
public synchronized org.apache.lucene.store.InputStreamopenFile(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 CSInputStream(stream, entry.offset, entry.length);
    
public voidrenameFile(java.lang.String from, java.lang.String to)
Renames an existing file in the directory. If a file already exists with the new name, then it is replaced. This replacement should be atomic.

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

        directory.touchFile(fileName);