Package org.wikidata.wdtk.util
Class DirectoryManagerImpl
- java.lang.Object
-
- org.wikidata.wdtk.util.DirectoryManagerImpl
-
- All Implemented Interfaces:
DirectoryManager
public class DirectoryManagerImpl extends Object implements DirectoryManager
Class to read and write files from one directory. It is guaranteed that the directory always exists (it is created if needed).- Author:
- Markus Kroetzsch
-
-
Constructor Summary
Constructors Constructor Description DirectoryManagerImpl(Path baseDirectory, Boolean readOnly)Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longcreateFile(String fileName, InputStream inputStream)Creates a new file in the current directory, and fill it with the data from the given input stream.voidcreateFile(String fileName, String fileContents)Creates a new file in the current directory, and fill it with the given data, encoded in UTF-8.longcreateFileAtomic(String fileName, InputStream inputStream)Creates a new file in the current directory, and fill it with the data from the given input stream.protected InputStreamgetCompressorInputStream(InputStream inputStream, CompressionType compressionType)Returns an input stream that applies the required decompression to the given input stream.InputStreamgetInputStreamForFile(String fileName, CompressionType compressionType)Returns an input stream to access file of the given name within the current directory, possibly uncompressing it if required.OutputStreamgetOutputStreamForFile(String fileName)Opens and returns an output stream that can be used to write to the file of the given name within the current directory.List<String>getSubdirectories(String glob)Returns a list of the names of all subdirectories of the base directory.DirectoryManagergetSubdirectoryManager(String subdirectoryName)Returns a new directory manager for the subdirectory of the given name.booleanhasFile(String fileName)Checks if there is a file of the given name.booleanhasSubdirectory(String subdirectoryName)Checks if there is a subdirectory of the given name.StringtoString()
-
-
-
Constructor Detail
-
DirectoryManagerImpl
public DirectoryManagerImpl(Path baseDirectory, Boolean readOnly) throws IOException
Constructor- Parameters:
baseDirectory- the directory where the file manager should point initially; will be created if not existingreadOnly- if false, the directory manager will attempt to create directories when changing to a location that does not exist- Throws:
IOException- if there was a problem creating the directory
-
-
Method Detail
-
getSubdirectoryManager
public DirectoryManager getSubdirectoryManager(String subdirectoryName) throws IOException
Description copied from interface:DirectoryManagerReturns a new directory manager for the subdirectory of the given name. If the subdirectory does not exist yet, it will be created. If this is not desired, its existence can be checked withDirectoryManager.hasSubdirectory(String)first (ignoring the fact that there might be race conditions when accessing the file system).- Specified by:
getSubdirectoryManagerin interfaceDirectoryManager- Parameters:
subdirectoryName- the string name of the subdirectory- Returns:
- DirectoryManager for subdirectory
- Throws:
IOException- if directory could not be created
-
hasSubdirectory
public boolean hasSubdirectory(String subdirectoryName)
Description copied from interface:DirectoryManagerChecks if there is a subdirectory of the given name.- Specified by:
hasSubdirectoryin interfaceDirectoryManager- Parameters:
subdirectoryName- the name of the subdirectory- Returns:
- true if the subdirectory exists
-
hasFile
public boolean hasFile(String fileName)
Description copied from interface:DirectoryManagerChecks if there is a file of the given name.- Specified by:
hasFilein interfaceDirectoryManager- Parameters:
fileName- the name of the file- Returns:
- true if the file exists and is not a directory
-
createFile
public long createFile(String fileName, InputStream inputStream) throws IOException
Description copied from interface:DirectoryManagerCreates a new file in the current directory, and fill it with the data from the given input stream. If the stream encodes a string, then it should generally be encoded in UTF-8, since access methods assume this.- Specified by:
createFilein interfaceDirectoryManager- Parameters:
fileName- the name of the fileinputStream- the input stream from which to load the file- Returns:
- size of the new file in bytes
- Throws:
IOException
-
createFileAtomic
public long createFileAtomic(String fileName, InputStream inputStream) throws IOException
Description copied from interface:DirectoryManagerCreates a new file in the current directory, and fill it with the data from the given input stream. This is done by first writing the data to a temporary file that uses a suffix to the file name, and then moving the completed file to the new location. This should be used when fetching larger files through from slow stream (e.g., a download) to prevent partially finished file downloads lying around if the program is terminated during download. The temporary file will still be lying around, but it will not be mistaken for the completed download by any other parts of the program.If the stream encodes a string, then it should generally be encoded in UTF-8, since access methods assume this.
- Specified by:
createFileAtomicin interfaceDirectoryManager- Parameters:
fileName- the name of the fileinputStream- the input stream from which to load the file- Returns:
- size of the new file in bytes
- Throws:
IOException
-
createFile
public void createFile(String fileName, String fileContents) throws IOException
Description copied from interface:DirectoryManagerCreates a new file in the current directory, and fill it with the given data, encoded in UTF-8. Should only be used for short pieces of data.- Specified by:
createFilein interfaceDirectoryManager- Parameters:
fileName- the name of the filefileContents- the data to write into the file- Throws:
IOException
-
getOutputStreamForFile
public OutputStream getOutputStreamForFile(String fileName) throws IOException
Description copied from interface:DirectoryManagerOpens and returns an output stream that can be used to write to the file of the given name within the current directory. The stream is owned by the caller and must be closed after use. If the file already exists, it will be truncated at this operation.- Specified by:
getOutputStreamForFilein interfaceDirectoryManager- Parameters:
fileName- the name of the file- Returns:
- the stream to write to
- Throws:
IOException
-
getInputStreamForFile
public InputStream getInputStreamForFile(String fileName, CompressionType compressionType) throws IOException
Description copied from interface:DirectoryManagerReturns an input stream to access file of the given name within the current directory, possibly uncompressing it if required.It is important to close the stream after using it to free memory.
- Specified by:
getInputStreamForFilein interfaceDirectoryManager- Parameters:
fileName- the name of the filecompressionType- for types other thanCompressionType.NONE, the file will be uncompressed appropriately and the returned input stream will provide access to the uncompressed content- Returns:
- an InputStream to fetch data from the file
- Throws:
IOException
-
getCompressorInputStream
protected InputStream getCompressorInputStream(InputStream inputStream, CompressionType compressionType) throws IOException
Returns an input stream that applies the required decompression to the given input stream.- Parameters:
inputStream- the input stream with the (possibly compressed) datacompressionType- the kind of compression- Returns:
- an input stream with decompressed data
- Throws:
IOException- if there was a problem creating the decompression streams
-
getSubdirectories
public List<String> getSubdirectories(String glob) throws IOException
Description copied from interface:DirectoryManagerReturns a list of the names of all subdirectories of the base directory. The glob pattern can be used to filter the names; "*" should be used if no filtering is desired.- Specified by:
getSubdirectoriesin interfaceDirectoryManager- Parameters:
glob- pattern to filter directoy names- Returns:
- list of subdirectory names
- Throws:
IOException
-
-