|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectit.unipi.di.util.Directory
public class Directory
A virtual directory of files identified by unique names and physically
contained into one single file on disk.
Through instances of this class it is possible to add new files to a
brand-new directory (write mode) or to access files in the directory (read
only mode). It is possible to manage only one
file at a time (the current file). This makes this implementation not
thread-safe. Simultaneous read/write accesses are not supported. Once built
on disk, the directory cannot be updated.
WRITE_MODE
constant in the constructor. Then create a new file
by using either the method newFile(String)
and
appending new content with the method write(byte[], int, int)
, or
by getting an OutputStream
through getOutputStream()
and filling sequentially the file.
There is no need to close the current file when you're done, that is automatic
each time the method newFile(String)
is called or this directory is
closed.
Once all files have been added to the new directory, call close()
in
order to flush its content into the file specified in the constructor.
READ_MODE
flag. To get
the list of the available files in this virtual directory, use the
method list()
.
getInputStream(String)
for a sequential access,
or the methods readFileBlock(String, long, byte[])
and
mapFile(String)
for a random access.
Field Summary | |
---|---|
static int |
READ_MODE
|
static int |
WRITE_MODE
|
Constructor Summary | |
---|---|
Directory(String filename,
int mode)
Creates a new virtual directory in read or write mode. |
Method Summary | |
---|---|
void |
cat(String file,
PrintStream out)
Print on a PrintStream the content of a file. |
void |
close()
Closes the directory. |
long |
dirSize()
Returns the total size in bytes of this directory. |
boolean |
exist(String file)
Returns TRUE if the passed virtual file is contained into this directory. |
long |
getFilePointer()
Returns the actual position inside the current file (both in read and write mode). |
InputStream |
getInputStream(String file)
Returns an InputStream to read a virtual file sequentially. |
OutputStream |
getOutputStream()
Returns an OutputStream to fill sequentially the content
of the current file, or null if the current file doesn't exist
(i.e. this directory is empty). |
String[] |
list()
Lists the names of the virtual files contained in this directory. |
void |
ls(PrintStream out)
Prints the file names and their sizes (in byte) for the current directory. |
static void |
main(String[] args)
|
ByteBuffer |
mapFile(String file)
Maps a virtual file into memory. |
void |
newFile(String file)
Creates an empty file. |
protected static void |
printUsage()
|
int |
readFileBlock(String file,
long pos,
byte[] block)
Read a portion of a virtual file belonging to this directory, and starting at the given file position. |
long |
size()
Returns the total size of this directory in bytes. |
int |
sizeOf(String file)
Returns the size in byte of a virtual file, 0 if the file doesn't exist. |
void |
write(byte[] b)
Appends content to the current file from a given byte array. |
void |
write(byte[] b,
int off,
int len)
Appends content to the current file from a given byte array. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int READ_MODE
public static final int WRITE_MODE
Constructor Detail |
---|
public Directory(String filename, int mode) throws IOException
filename
- the file on disk storing the directory, or where
to put the newly created directorymode
- the open modality
IOException
Method Detail |
---|
public ByteBuffer mapFile(String file) throws IOException
file
- the virtual file to map
ByteBuffer
object to access randomly the file content, or null
if the file doesn't exist
IOException
public InputStream getInputStream(String file) throws IOException
InputStream
to read a virtual file sequentially.
A null reference is returned if the file doesn't exist in
this directory.
file
- the file to access
InputStream
to the content of the file, or null if the file doesn't exist
IOException
public int readFileBlock(String file, long pos, byte[] block) throws IOException
sizeOf(String)
for this file, then the portion to be extracted
must start in the range [0, S-1].
This method tries to read up to block.length bytes. If there are less
bytes to read, the remaining bytes will be unspecified.
file
- the virtual filepos
- the starting position of the block to be fetchedblock
- the buffer in which the block has to be loaded
IOException
public String[] list()
public int sizeOf(String file)
file
- the virtual file to measure
public long size() throws IOException
IOException
public void close() throws IOException
WRITE_MODE
then
this method writes on disk a file containing all the virtual files of this
directory and the auxiliary data
structures needed to efficiently access them. The file name is the one specified at
construction time.
IOException
public boolean exist(String file)
file
- the file name to check
public void newFile(String file) throws IOException
file
- the file to add in this directory
IOException
public void write(byte[] b, int off, int len) throws IOException
b
- the content to append (as a byte array)off
- the offset from which the copy must startlen
- the number of bytes to write
IOException
newFile(String)
public void write(byte[] b) throws IOException
b
- the content to append (as an entire byte array)
IOException
newFile(String)
public OutputStream getOutputStream() throws IOException
OutputStream
to fill sequentially the content
of the current file, or null if the current file doesn't exist
(i.e. this directory is empty). Writing into a file using both the returned
stream and the method write(byte[], int, int)
can generate
undefined results.
OutputStream
to write into the current file, or
null if the directory is empty
IOException
newFile(String)
public void ls(PrintStream out)
out
- the PrintStream
where to list this directory contentpublic void cat(String file, PrintStream out) throws IOException
PrintStream
the content of a file.
file
- the virtual file to printout
- the PrintStream
where to print the file content
IOException
- if the file is not contained into this directory.public long dirSize()
public long getFilePointer() throws IOException
IOException
protected static void printUsage()
public static void main(String[] args) throws Exception
Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |