public class Jar
extends java.lang.Object
implements java.io.Closeable
Modifier and Type | Class and Description |
---|---|
static class |
Jar.Compression |
Modifier and Type | Field and Description |
---|---|
private java.lang.String[] |
algorithms |
private static java.util.regex.Pattern |
BSN |
private static int |
BUFFER_SIZE |
private boolean |
closed |
private Jar.Compression |
compression |
private static java.util.regex.Pattern |
DEFAULT_DO_NOT_COPY |
private static java.lang.String |
DEFAULT_MANIFEST_NAME |
private java.util.NavigableMap<java.lang.String,java.util.Map<java.lang.String,Resource>> |
directories |
private boolean |
doNotTouchManifest |
static java.lang.Object[] |
EMPTY_ARRAY |
private static byte[] |
EOL
Unfortunately we have to write our own manifest :-( because of a stupid
bug in the manifest code.
|
private long |
lastModified |
private java.lang.String |
lastModifiedReason |
private java.util.Optional<java.util.jar.Manifest> |
manifest |
private boolean |
manifestFirst |
private java.lang.String |
manifestName |
private java.util.Optional<ModuleAttribute> |
moduleAttribute |
private java.lang.String |
name |
private boolean |
nomanifest |
private boolean |
reproducible |
private java.util.NavigableMap<java.lang.String,Resource> |
resources |
private static byte[] |
SEPARATOR |
(package private) static java.util.regex.Pattern |
SIGNER_FILES_P |
private java.io.File |
source |
private static long |
ZIP_ENTRY_CONSTANT_TIME
Note that setting the January 1st 1980 (or even worse, "0", as time)
won't work due to Java 8 doing some interesting time processing: It
checks if this date is before January 1st 1980 and if it is it starts
setting some extra fields in the zip.
|
private java.util.zip.ZipFile |
zipFile |
Constructor and Description |
---|
Jar(java.io.File f) |
Jar(java.lang.String name) |
Jar(java.lang.String string,
java.io.File file) |
Jar(java.lang.String name,
java.io.File dirOrFile,
java.util.regex.Pattern doNotCopy) |
Jar(java.lang.String string,
java.io.InputStream resourceAsStream) |
Jar(java.lang.String name,
java.io.InputStream in,
long lastModified) |
Jar(java.lang.String name,
java.lang.String path) |
Modifier and Type | Method and Description |
---|---|
boolean |
addAll(Jar src) |
boolean |
addAll(Jar sub,
Instruction filter)
Add all the resources in the given jar that match the given filter.
|
boolean |
addAll(Jar sub,
Instruction filter,
java.lang.String destination)
Add all the resources in the given jar that match the given filter.
|
boolean |
addDirectory(java.util.Map<java.lang.String,Resource> directory,
boolean overwrite) |
private static void |
attributes(java.util.jar.Attributes value,
java.io.OutputStream out)
Output an Attributes map.
|
(package private) java.lang.String |
automaticModuleName() |
private Jar |
buildFromDirectory(java.nio.file.Path baseDir,
java.util.regex.Pattern doNotCopy) |
private Jar |
buildFromInputStream(java.io.InputStream in,
long lastModified) |
private Jar |
buildFromResource(Resource resource) |
private Jar |
buildFromZip(java.io.File file) |
void |
calcChecksums(java.lang.String[] algorithms)
Calculate the checksums and set them in the manifest.
|
(package private) void |
check() |
private static java.util.jar.Manifest |
clean(java.util.jar.Manifest org) |
private static java.lang.String |
clean(java.lang.String s) |
private static java.lang.String |
cleanPath(java.lang.String path) |
void |
close() |
void |
copy(Jar srce,
java.lang.String path,
boolean overwrite) |
private void |
copyResource(java.io.File dir,
java.lang.String path,
Resource resource) |
(package private) void |
createDirectories(java.util.Set<java.lang.String> directories,
java.util.zip.ZipOutputStream zip,
java.lang.String name) |
void |
doChecksums(java.io.OutputStream out) |
private void |
doManifest(java.util.zip.ZipOutputStream jout,
java.util.Set<java.lang.String> directories,
java.lang.String manifestName) |
void |
ensureManifest()
Make sure we have a manifest
|
boolean |
exists(java.lang.String path) |
void |
expand(java.io.File dir)
Expand the JAR file to a directory.
|
static Jar |
fromResource(java.lang.String name,
Resource resource) |
java.lang.String |
getBsn()
Get the jar bsn from the
Constants.BUNDLE_SYMBOLICNAME manifest
header. |
java.net.URI |
getDataURI(java.lang.String path,
java.lang.String mime,
int max)
Return a data uri from the JAR.
|
java.util.Map<java.lang.String,java.util.Map<java.lang.String,Resource>> |
getDirectories() |
java.util.Map<java.lang.String,Resource> |
getDirectory(java.lang.String path) |
java.util.jar.Manifest |
getManifest() |
java.lang.String |
getModuleName() |
java.lang.String |
getModuleVersion() |
java.lang.String |
getName() |
private static java.lang.String |
getName(java.io.File f)
Make the JAR file name the project name if we get a src or bin directory.
|
java.util.List<java.lang.String> |
getPackages() |
private java.lang.String |
getParent(java.lang.String path) |
Resource |
getResource(java.lang.String path) |
java.util.Map<java.lang.String,Resource> |
getResources() |
java.util.stream.Stream<Resource> |
getResources(java.util.function.Predicate<java.lang.String> matches) |
java.io.File |
getSource() |
byte[] |
getTimelessDigest() |
java.lang.String |
getVersion()
Get the jar version from the
Constants.BUNDLE_VERSION manifest
header. |
Jar.Compression |
hasCompression() |
boolean |
hasDirectory(java.lang.String path) |
boolean |
isManifestFirst()
Answer if the manifest was the first entry
|
boolean |
isReproducible() |
long |
lastModified() |
(package private) java.lang.String |
lastModifiedReason() |
(package private) java.util.Optional<java.util.jar.Manifest> |
manifest() |
(package private) java.util.Optional<ModuleAttribute> |
moduleAttribute() |
static void |
outputManifest(java.util.jar.Manifest manifest,
java.io.OutputStream out)
Main function to output a manifest properly in UTF-8.
|
private java.lang.String |
padString(java.lang.String s,
int length,
char pad) |
private void |
putEntry(java.util.zip.ZipOutputStream jout,
java.util.zip.ZipEntry entry,
Resource r) |
boolean |
putResource(java.lang.String path,
Resource resource) |
boolean |
putResource(java.lang.String path,
Resource resource,
boolean overwrite) |
Resource |
remove(java.lang.String path) |
void |
removePrefix(java.lang.String prefixLow) |
void |
removeSubDirs(java.lang.String dir) |
boolean |
rename(java.lang.String oldPath,
java.lang.String newPath) |
void |
setCompression(Jar.Compression compression) |
void |
setDigestAlgorithms(java.lang.String[] algorithms) |
void |
setDoNotTouchManifest()
Make sure nobody touches the manifest! If the bundle is signed, we do not
want anybody to touch the manifest after the digests have been
calculated.
|
void |
setManifest(java.io.File file) |
void |
setManifest(java.util.jar.Manifest manifest) |
void |
setManifestName(java.lang.String manifestName) |
void |
setName(java.lang.String name) |
void |
setReproducible(boolean reproducible) |
void |
stripSignatures() |
java.lang.String |
toString() |
void |
updateModified(long time,
java.lang.String reason) |
void |
write(java.io.File file) |
void |
write(java.io.OutputStream out) |
private static int |
write(java.io.OutputStream out,
int width,
byte[] bytes)
Write the bytes but ensure that the line length does not exceed 72
characters.
|
private static int |
write(java.io.OutputStream out,
int width,
java.lang.String s)
Convert a string to bytes with UTF-8 and then output in max 72 bytes
|
void |
write(java.lang.String file) |
private static void |
writeEntry(java.io.OutputStream out,
java.lang.String name,
java.lang.String value)
Write out an entry, handling proper unicode and line length constraints
|
void |
writeFolder(java.io.File dir) |
static void |
writeManifest(java.util.jar.Manifest manifest,
java.io.OutputStream out) |
void |
writeManifest(java.io.OutputStream out)
Cleanup the manifest for writing.
|
private void |
writeResource(java.util.zip.ZipOutputStream jout,
java.util.Set<java.lang.String> directories,
java.lang.String path,
Resource resource) |
private static final int BUFFER_SIZE
private static final long ZIP_ENTRY_CONSTANT_TIME
ZipUtils
#javaToDosTime() so we must use default
timezone and locale. The date is 1980 February 1st CET.private static final java.lang.String DEFAULT_MANIFEST_NAME
private static final java.util.regex.Pattern DEFAULT_DO_NOT_COPY
public static final java.lang.Object[] EMPTY_ARRAY
private final java.util.NavigableMap<java.lang.String,Resource> resources
private final java.util.NavigableMap<java.lang.String,java.util.Map<java.lang.String,Resource>> directories
private java.util.Optional<java.util.jar.Manifest> manifest
private java.util.Optional<ModuleAttribute> moduleAttribute
private boolean manifestFirst
private java.lang.String manifestName
private java.lang.String name
private java.io.File source
private java.util.zip.ZipFile zipFile
private long lastModified
private java.lang.String lastModifiedReason
private boolean doNotTouchManifest
private boolean nomanifest
private boolean reproducible
private Jar.Compression compression
private boolean closed
private java.lang.String[] algorithms
private static final byte[] EOL
'Manifest-Version: 1.0\r\n' main-attributes * \r\n name-section main-attributes ::= attributes attributes ::= key ': ' value '\r\n' name-section ::= 'Name: ' name '\r\n' attributesLines in the manifest should not exceed 72 bytes (! this is where the manifest screwed up as well when 16 bit unicodes were used).
As a bonus, we can now sort the manifest!
private static final byte[] SEPARATOR
private static final java.util.regex.Pattern BSN
static java.util.regex.Pattern SIGNER_FILES_P
public Jar(java.lang.String name)
public Jar(java.lang.String name, java.io.File dirOrFile, java.util.regex.Pattern doNotCopy) throws java.io.IOException
java.io.IOException
public Jar(java.lang.String name, java.io.InputStream in, long lastModified) throws java.io.IOException
java.io.IOException
public Jar(java.lang.String name, java.lang.String path) throws java.io.IOException
java.io.IOException
public Jar(java.io.File f) throws java.io.IOException
java.io.IOException
public Jar(java.lang.String string, java.io.InputStream resourceAsStream) throws java.io.IOException
java.io.IOException
public Jar(java.lang.String string, java.io.File file) throws java.io.IOException
java.io.IOException
public static Jar fromResource(java.lang.String name, Resource resource) throws java.lang.Exception
java.lang.Exception
private static java.lang.String getName(java.io.File f)
f
- private Jar buildFromDirectory(java.nio.file.Path baseDir, java.util.regex.Pattern doNotCopy) throws java.io.IOException
java.io.IOException
private Jar buildFromZip(java.io.File file) throws java.io.IOException
java.io.IOException
private Jar buildFromResource(Resource resource) throws java.lang.Exception
java.lang.Exception
private Jar buildFromInputStream(java.io.InputStream in, long lastModified) throws java.io.IOException
java.io.IOException
public void setName(java.lang.String name)
public java.lang.String toString()
toString
in class java.lang.Object
public boolean putResource(java.lang.String path, Resource resource)
private static java.lang.String cleanPath(java.lang.String path)
public boolean putResource(java.lang.String path, Resource resource, boolean overwrite)
public Resource getResource(java.lang.String path)
public java.util.stream.Stream<Resource> getResources(java.util.function.Predicate<java.lang.String> matches)
private java.lang.String getParent(java.lang.String path)
public java.util.Map<java.lang.String,java.util.Map<java.lang.String,Resource>> getDirectories()
public java.util.Map<java.lang.String,Resource> getDirectory(java.lang.String path)
public java.util.Map<java.lang.String,Resource> getResources()
public boolean addDirectory(java.util.Map<java.lang.String,Resource> directory, boolean overwrite)
public java.util.jar.Manifest getManifest() throws java.lang.Exception
java.lang.Exception
java.util.Optional<java.util.jar.Manifest> manifest()
java.util.Optional<ModuleAttribute> moduleAttribute() throws java.lang.Exception
java.lang.Exception
public java.lang.String getModuleName() throws java.lang.Exception
java.lang.Exception
java.lang.String automaticModuleName()
public java.lang.String getModuleVersion() throws java.lang.Exception
java.lang.Exception
public boolean exists(java.lang.String path)
public void setManifest(java.util.jar.Manifest manifest)
public void setManifest(java.io.File file) throws java.io.IOException
java.io.IOException
public void setManifestName(java.lang.String manifestName)
public void write(java.io.File file) throws java.lang.Exception
java.lang.Exception
public void write(java.lang.String file) throws java.lang.Exception
java.lang.Exception
public void write(java.io.OutputStream out) throws java.lang.Exception
java.lang.Exception
public void writeFolder(java.io.File dir) throws java.lang.Exception
java.lang.Exception
private void copyResource(java.io.File dir, java.lang.String path, Resource resource) throws java.lang.Exception
java.lang.Exception
public void doChecksums(java.io.OutputStream out) throws java.lang.Exception
java.lang.Exception
private java.lang.String padString(java.lang.String s, int length, char pad)
private void doManifest(java.util.zip.ZipOutputStream jout, java.util.Set<java.lang.String> directories, java.lang.String manifestName) throws java.lang.Exception
java.lang.Exception
private void putEntry(java.util.zip.ZipOutputStream jout, java.util.zip.ZipEntry entry, Resource r) throws java.lang.Exception
java.lang.Exception
public void writeManifest(java.io.OutputStream out) throws java.lang.Exception
out
- Outputjava.io.IOException
java.lang.Exception
public static void writeManifest(java.util.jar.Manifest manifest, java.io.OutputStream out) throws java.io.IOException
java.io.IOException
public static void outputManifest(java.util.jar.Manifest manifest, java.io.OutputStream out) throws java.io.IOException
manifest
- The manifest to outputout
- The output streamjava.io.IOException
- when something failsprivate static void writeEntry(java.io.OutputStream out, java.lang.String name, java.lang.String value) throws java.io.IOException
java.io.IOException
private static int write(java.io.OutputStream out, int width, java.lang.String s) throws java.io.IOException
out
- the output stringwidth
- the current widths
- the string to outputjava.io.IOException
- when something failsprivate static int write(java.io.OutputStream out, int width, byte[] bytes) throws java.io.IOException
out
- The output streamwidth
- The nr of characters output in a line before this method
startedbytes
- the bytes to outputjava.io.IOException
- if something failsprivate static void attributes(java.util.jar.Attributes value, java.io.OutputStream out) throws java.io.IOException
value
- the attrbutesout
- the output streamjava.io.IOException
- when something failsprivate static java.util.jar.Manifest clean(java.util.jar.Manifest org)
private static java.lang.String clean(java.lang.String s)
private void writeResource(java.util.zip.ZipOutputStream jout, java.util.Set<java.lang.String> directories, java.lang.String path, Resource resource) throws java.lang.Exception
java.lang.Exception
void createDirectories(java.util.Set<java.lang.String> directories, java.util.zip.ZipOutputStream zip, java.lang.String name) throws java.io.IOException
java.io.IOException
public java.lang.String getName()
public boolean addAll(Jar sub, Instruction filter)
sub
- the jarfilter
- a pattern that should match the resoures in sub to be addedpublic boolean addAll(Jar sub, Instruction filter, java.lang.String destination)
sub
- the jarfilter
- a pattern that should match the resoures in sub to be addedpublic void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public long lastModified()
java.lang.String lastModifiedReason()
public void updateModified(long time, java.lang.String reason)
public boolean hasDirectory(java.lang.String path)
public java.util.List<java.lang.String> getPackages()
public java.io.File getSource()
public boolean addAll(Jar src)
public boolean rename(java.lang.String oldPath, java.lang.String newPath)
public Resource remove(java.lang.String path)
public void setDoNotTouchManifest()
public void calcChecksums(java.lang.String[] algorithms) throws java.lang.Exception
java.lang.Exception
public java.lang.String getBsn() throws java.lang.Exception
Constants.BUNDLE_SYMBOLICNAME
manifest
header.Constants.BUNDLE_SYMBOLICNAME
header, or when the value
of the header is not a valid bsn according to BSN
.java.lang.Exception
- when the jar is closed or when the manifest could not
be retrieved.public java.lang.String getVersion() throws java.lang.Exception
Constants.BUNDLE_VERSION
manifest
header.Constants.BUNDLE_VERSION
headerjava.lang.Exception
- when the jar is closed or when the manifest could not
be retrieved.public void expand(java.io.File dir) throws java.lang.Exception
dir
- the dst directory, is not required to existjava.lang.Exception
- if anything does not work as expected.public void ensureManifest() throws java.lang.Exception
java.lang.Exception
public boolean isManifestFirst()
public boolean isReproducible()
public void setReproducible(boolean reproducible)
public void copy(Jar srce, java.lang.String path, boolean overwrite)
public void setCompression(Jar.Compression compression)
public Jar.Compression hasCompression()
void check()
public java.net.URI getDataURI(java.lang.String path, java.lang.String mime, int max) throws java.lang.Exception
path
- the path in the jarmime
- the mime typejava.lang.Exception
public void setDigestAlgorithms(java.lang.String[] algorithms)
public byte[] getTimelessDigest() throws java.lang.Exception
java.lang.Exception
public void stripSignatures()
public void removePrefix(java.lang.String prefixLow)
public void removeSubDirs(java.lang.String dir)