public class JarWriter extends java.lang.Object implements DataEntryWriter
DataEntryWriter sends data entries to another given data entry writer, automatically
adding a manifest file.
You'll typically wrap a ZipWriter or one of its extensions:
new JarWriter(new ZipWriter(...))
| Modifier and Type | Class and Description |
|---|---|
protected static class |
JarWriter.MyMultiDigestOutputStream
This FilterOutputStream automatically appends a file digest entry to a given manifest writer,
when the stream is closed.
|
| Modifier and Type | Field and Description |
|---|---|
protected java.lang.String |
creator |
protected DataEntry |
currentManifestEntry |
static java.lang.String |
DEFAULT_DIGEST_ALGORITHM |
protected java.lang.String[] |
digestAlgorithms |
protected DataEntryWriter |
manifestEntryWriter |
protected DataEntryWriter |
zipEntryWriter |
| Constructor and Description |
|---|
JarWriter(DataEntryWriter zipEntryWriter)
Creates a new JarWriter wth default manifest digest "SHA-256" and manifest file name
"META-INF/MANIFEST.MF".
|
JarWriter(java.lang.String[] digestAlgorithms,
DataEntryWriter zipEntryWriter)
Creates a new JarWriter wth default manifest file name "META-INF/MANIFEST.MF".
|
JarWriter(java.lang.String[] digestAlgorithms,
java.lang.String creator,
DataEntryWriter zipEntryWriter)
Creates a new JarWriter wth default manifest file name "META-INF/MANIFEST.MF".
|
JarWriter(java.lang.String[] digestAlgorithms,
java.lang.String creator,
java.lang.String manifestFileName,
StringFunction manifestEntryNameFunction,
DataEntryWriter zipEntryWriter,
DataEntryWriter manifestEntryWriter)
Creates a new JarWriter.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Finishes writing all data entries.
|
boolean |
createDirectory(DataEntry dataEntry)
Creates a directory.
|
protected java.io.OutputStream |
createManifestOutputStream(DataEntry manifestEntry)
Creates an output stream for the specified manifest file.
|
java.io.OutputStream |
createOutputStream(DataEntry dataEntry)
Creates a new output stream for writing data.
|
protected void |
finish()
Writes out the collected manifest file before closing the jar, if any.
|
protected void |
finishIfNecessary(DataEntry dataEntry)
Writes out the collected manifest file for the current jar, if we're entering a new jar with
this data entry.
|
static void |
main(java.lang.String[] args)
Provides a simple test for this class, creating a signed apk file (only v1) with the given name
and a few aligned/compressed/uncompressed zip entries.
|
protected void |
openManifestFiles()
Prepares streams and writers for capturing digests of a parent entry.
|
void |
println(java.io.PrintWriter pw,
java.lang.String prefix)
Prints out the structure of the data entry writer.
|
protected java.io.PrintWriter |
printWriter(java.io.OutputStream outputStream)
Creates a convenience writer.
|
boolean |
sameOutputStream(DataEntry dataEntry1,
DataEntry dataEntry2)
Returns whether the two given data entries would result in the same output stream.
|
public static final java.lang.String DEFAULT_DIGEST_ALGORITHM
protected final java.lang.String[] digestAlgorithms
protected final java.lang.String creator
protected final DataEntryWriter zipEntryWriter
protected final DataEntryWriter manifestEntryWriter
protected DataEntry currentManifestEntry
public JarWriter(DataEntryWriter zipEntryWriter)
zipEntryWriter - the data entry writer that can provide output streams for the jar
entries.public JarWriter(java.lang.String[] digestAlgorithms,
DataEntryWriter zipEntryWriter)
digestAlgorithms - the manifest digest algorithms, e.g. "SHA-256".zipEntryWriter - the data entry writer that can provide output streams for the jar
entries.public JarWriter(java.lang.String[] digestAlgorithms,
java.lang.String creator,
DataEntryWriter zipEntryWriter)
digestAlgorithms - the manifest digest algorithms, e.g. "SHA-256".creator - the creator to mention in the manifest file.zipEntryWriter - the data entry writer that can provide output streams for the jar
entries.public JarWriter(java.lang.String[] digestAlgorithms,
java.lang.String creator,
java.lang.String manifestFileName,
StringFunction manifestEntryNameFunction,
DataEntryWriter zipEntryWriter,
DataEntryWriter manifestEntryWriter)
digestAlgorithms - the manifest digest algorithms, e.g. "SHA-256".creator - the creator to mention in the manifest file.manifestFileName - the manifest file name, e.g. "META-INF/MANIFEST.MF".manifestEntryNameFunction - the function to transform entry names in the manifest (not in
the jar).zipEntryWriter - the data entry writer that can provide output streams for the jar
entries.manifestEntryWriter - the data entry writer that can provide an output stream for the
manifest file.public boolean createDirectory(DataEntry dataEntry) throws java.io.IOException
DataEntryWritercreateDirectory in interface DataEntryWriterdataEntry - the data entry for which the directory is to be created.java.io.IOExceptionpublic boolean sameOutputStream(DataEntry dataEntry1, DataEntry dataEntry2) throws java.io.IOException
DataEntryWritersameOutputStream in interface DataEntryWriterdataEntry1 - the first data entry.dataEntry2 - the second data entry.java.io.IOExceptionpublic java.io.OutputStream createOutputStream(DataEntry dataEntry) throws java.io.IOException
DataEntryWritercreateOutputStream in interface DataEntryWriterdataEntry - the data entry for which the output stream is to be created.null to indicate that the data entry
should not be written.java.io.IOExceptionpublic void close()
throws java.io.IOException
DataEntryWriterImplementations typically create graphs of writers that can split and merge again, possibly even with cycles.
For splits and merges, implementations need to be idempotent; once closed, subsequent
attempts to close a writer have no effect. If needed, the wrapper NonClosingDataEntryWriter can avoid closing a branch prematurely.
For cycles, implementations must perform any custom behavior, then delegate DataEntryWriter.close()
invocations, and only finally clean up. It is possible that delegates call DataEntryWriter.createOutputStream(DataEntry) while DataEntryWriter.close() is in progress.
close in interface java.lang.AutoCloseableclose in interface DataEntryWriterjava.io.IOExceptionpublic void println(java.io.PrintWriter pw,
java.lang.String prefix)
DataEntryWriterprintln in interface DataEntryWriterpw - the print stream to which the structure should be printed.prefix - a prefix for every printed line.protected void openManifestFiles()
throws java.io.IOException
java.io.IOExceptionprotected java.io.OutputStream createManifestOutputStream(DataEntry manifestEntry) throws java.io.IOException
java.io.IOExceptionprotected void finishIfNecessary(DataEntry dataEntry) throws java.io.IOException
java.io.IOExceptionprotected void finish()
throws java.io.IOException
java.io.IOExceptionprotected java.io.PrintWriter printWriter(java.io.OutputStream outputStream)
throws java.io.IOException
outputStream - the underlying output stream.java.io.IOExceptionpublic static void main(java.lang.String[] args)