public class FilesOutputStream extends OutputStream implements Closeable
FilesInputStream
to transfer files archives. A files
archive is a hierarchical files collections bundled with meta data. Archive is compressed using ZIP lossless data compression
and meta is carried by a standard Java JAR Manifest. This class gets and output stream and inject meta data and files, on the
fly, see putMeta(String, Object)
and addFiles(File)
and related.
Common use case is with URL connection and Servlet HTTP request but is not limited to this scenario. Input stream for this class could be a file archive stored on disk, but file content should be generated by files output stream. Both output and input streams are processed on the fly.
FilesOutputStream outputFiles = new FilesOutputStream(connection.getOutputStream()); outputFiles.putMeta("Base-Directory", baseDir); outputFiles.addFiles(new File("source-directory")); outputFiles.close();
FilesInputStream inputFiles = new FilesInputStream(httpRequest.getInputStream()); File baseDir = inputFiles.getMeta("Base-Directory", File.class); for (File file : inputFiles) { File targetFile = new File(targetDir, file.getPath()); files.copy(targetFile); } inputFiles.close();
Manifest is mandatory even if no meta data is present and is always the first entry in archive. There is a single predefined
attribute, the implementation version but application level meta data is supported. Files output stream can put arbitrary
meta data using putMeta(String, Object)
. On receive side, this files archive input stream can retrieve application
meta data uing FilesInputStream.getMeta(String)
and related methods.
Modifier and Type | Field and Description |
---|---|
private static int |
BUFFER_SIZE
The size of the buffer used for internal bytes processing.
|
private ZipOutputStream |
filesArchive
Output stream where meta data and files are to be injected.
|
private Manifest |
manifest
This archive manifest.
|
private static String |
VERSION
Files archive implementation version.
|
Constructor and Description |
---|
FilesOutputStream(OutputStream filesArchive)
Construct files archive wrapping an output stream where archive bytes are injected.
|
Modifier and Type | Method and Description |
---|---|
void |
addFile(File file)
Inject file content into archive using file relative path as archive entry name.
|
private void |
addFileEntry(String entryName,
InputStream inputStream)
Add file entry to this files archive.
|
void |
addFiles(File baseDir)
Add files hierarchy to this archive.
|
void |
addFiles(File baseDir,
List<String> fileNames)
Add files list to this archive.
|
private void |
addManifestEntry()
Add manifest entry to this files output stream.
|
void |
close()
Close this files output stream and takes care to write manifest if no files was added.
|
void |
putMeta(String key,
Object value)
Put meta data to this archive manifest.
|
void |
write(int b)
Write byte to files archive.
|
flush, write, write
private static final String VERSION
private static final int BUFFER_SIZE
private ZipOutputStream filesArchive
private Manifest manifest
putMeta(String, Object)
. Is
lazily written to this archive just before injecting first file content.public FilesOutputStream(OutputStream filesArchive)
filesArchive
- output stream to files archive to write to.public void write(int b) throws IOException
write
in class OutputStream
IOException
public void putMeta(String key, Object value)
key
already exists is overridden. Meta value
is
converted to string and can be any type for which there is a Converter
registered.key
- meta data key,value
- meta data value.public void addFiles(File baseDir) throws IOException
baseDir
directory files, no matter how deep hierarchy is
and delegate addFile(File)
.baseDir
- files hierarchy base directory.IOException
- if archive writing operation fails.public void addFiles(File baseDir, List<String> fileNames) throws FileNotFoundException, IOException
/var/www/
a base directory and an item from files list
site/index.htm
; resulting entry name is site/index.htm
and file content is read from
/var/www/site/index.htm
.baseDir
- files base directory,fileNames
- file names list.FileNotFoundException
- if file not found on baseDir
descendants.IOException
- if writing to archive fails.public void addFile(File file) throws IOException
file
- file to add to this archive.IllegalArgumentException
- if file
does not exist or is a directory.IOException
- if archive writing operation fails.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
IOException
private void addManifestEntry() throws IOException
manifest
to null signaling manifest was
processed.IOException
- if manifest entry write fails.private void addFileEntry(String entryName, InputStream inputStream) throws IOException
entryName
- entry name,inputStream
- file content.IOException
- if file entry write fails.Copyright © 2018. All rights reserved.