Package aQute.bnd.osgi
Class Jar
- java.lang.Object
-
- aQute.bnd.osgi.Jar
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class Jar extends java.lang.Object implements java.io.Closeable
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJar.Compression
-
Field Summary
Fields Modifier and Type Field Description static java.lang.Object[]EMPTY_ARRAYstatic java.util.regex.PatternMETAINF_SIGNING_P
-
Constructor Summary
Constructors Constructor 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 name, java.io.InputStream in)Jar(java.lang.String name, java.io.InputStream in, long lastModified)Jar(java.lang.String name, java.lang.String path)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanaddAll(Jar src)booleanaddAll(Jar sub, Instruction filter)Add all the resources in the given jar that match the given filter.booleanaddAll(Jar sub, Instruction filter, java.lang.String destination)Add all the resources in the given jar that match the given filter.booleanaddDirectory(java.util.Map<java.lang.String,Resource> directory, boolean overwrite)voidcalcChecksums(java.lang.String[] algorithms)Calculate the checksums and set them in the manifest.voidclose()voidcopy(Jar srce, java.lang.String path, boolean overwrite)voiddoChecksums(java.io.OutputStream out)voidensureManifest()Make sure we have a manifestbooleanexists(java.lang.String path)voidexpand(java.io.File dir)Expand the JAR file to a directory.static JarfromResource(java.lang.String name, Resource resource)java.lang.StringgetBsn()Get the jar bsn from theConstants.BUNDLE_SYMBOLICNAMEmanifest header.java.net.URIgetDataURI(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)intgetLength()Get the length of the last written file or -1 if unavailable.java.util.jar.ManifestgetManifest()java.lang.StringgetManifestName()java.lang.StringgetModuleName()java.lang.StringgetModuleVersion()java.lang.StringgetName()java.util.List<java.lang.String>getPackages()java.util.stream.Stream<Resource>getPomXmlResources()ResourcegetResource(java.lang.String path)java.util.stream.Stream<java.lang.String>getResourceNames(java.util.function.Predicate<java.lang.String> matches)java.util.Map<java.lang.String,Resource>getResources()static java.util.stream.Stream<Resource>getResources(Resource jarResource, java.util.function.Predicate<java.lang.String> filter)java.util.stream.Stream<Resource>getResources(java.util.function.Predicate<java.lang.String> matches)java.util.Optional<byte[]>getSHA256()Get the SHA256 digest of the last write operation whensetCalculateFileDigest(boolean)was on.java.io.FilegetSource()byte[]getTimelessDigest()java.lang.StringgetVersion()Get the jar version from theConstants.BUNDLE_VERSIONmanifest header.Jar.CompressionhasCompression()booleanhasDirectory(java.lang.String path)booleanisEmpty()booleanisManifestFirst()Answer if the manifest was the first entrybooleanisReproducible()longlastModified()static voidoutputManifest(java.util.jar.Manifest manifest, java.io.OutputStream out)Main function to output a manifest properly in UTF-8.booleanputResource(java.lang.String path, Resource resource)booleanputResource(java.lang.String path, Resource resource, boolean overwrite)Resourceremove(java.lang.String path)voidremovePrefix(java.lang.String prefixLow)voidremoveSubDirs(java.lang.String dir)booleanrename(java.lang.String oldPath, java.lang.String newPath)JarsetCalculateFileDigest(boolean onOrOff)Make this jar calculate the SHA256 when it is saved as a file.voidsetCompression(Jar.Compression compression)voidsetDigestAlgorithms(java.lang.String[] algorithms)voidsetDoNotTouchManifest()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.voidsetManifest(java.io.File file)voidsetManifest(java.util.jar.Manifest manifest)voidsetManifestName(java.lang.String manifestName)voidsetName(java.lang.String name)voidsetReproducible(boolean reproducible)Deprecated.Replaced bysetReproducible(String).voidsetReproducible(java.lang.String outputTimestamp)voidsetSource(java.io.File source)voidstripSignatures()java.lang.StringtoString()voidupdateModified(long time, java.lang.String reason)voidwrite(java.io.File file)voidwrite(java.io.OutputStream to)voidwrite(java.lang.String file)voidwriteFolder(java.io.File dir)voidwriteManifest(java.io.OutputStream out)Cleanup the manifest for writing.static voidwriteManifest(java.util.jar.Manifest manifest, java.io.OutputStream out)
-
-
-
Constructor Detail
-
Jar
public Jar(java.lang.String name)
-
Jar
public Jar(java.lang.String name, java.io.File dirOrFile, java.util.regex.Pattern doNotCopy) throws java.io.IOException- Throws:
java.io.IOException
-
Jar
public Jar(java.lang.String name, java.io.InputStream in, long lastModified) throws java.io.IOException- Throws:
java.io.IOException
-
Jar
public Jar(java.lang.String name, java.lang.String path) throws java.io.IOException- Throws:
java.io.IOException
-
Jar
public Jar(java.io.File f) throws java.io.IOException- Throws:
java.io.IOException
-
Jar
public Jar(java.lang.String name, java.io.InputStream in) throws java.io.IOException- Throws:
java.io.IOException
-
Jar
public Jar(java.lang.String string, java.io.File file) throws java.io.IOException- Throws:
java.io.IOException
-
-
Method Detail
-
fromResource
public static Jar fromResource(java.lang.String name, Resource resource) throws java.lang.Exception
- Throws:
java.lang.Exception
-
getResources
public static java.util.stream.Stream<Resource> getResources(Resource jarResource, java.util.function.Predicate<java.lang.String> filter) throws java.lang.Exception
- Throws:
java.lang.Exception
-
setName
public void setName(java.lang.String name)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
putResource
public boolean putResource(java.lang.String path, Resource resource)
-
putResource
public boolean putResource(java.lang.String path, Resource resource, boolean overwrite)
-
getResource
public Resource getResource(java.lang.String path)
-
getResourceNames
public java.util.stream.Stream<java.lang.String> getResourceNames(java.util.function.Predicate<java.lang.String> matches)
-
getResources
public java.util.stream.Stream<Resource> getResources(java.util.function.Predicate<java.lang.String> matches)
-
getDirectories
public java.util.Map<java.lang.String,java.util.Map<java.lang.String,Resource>> getDirectories()
-
getDirectory
public java.util.Map<java.lang.String,Resource> getDirectory(java.lang.String path)
-
getResources
public java.util.Map<java.lang.String,Resource> getResources()
-
addDirectory
public boolean addDirectory(java.util.Map<java.lang.String,Resource> directory, boolean overwrite)
-
getManifest
public java.util.jar.Manifest getManifest() throws java.lang.Exception- Throws:
java.lang.Exception
-
getModuleName
public java.lang.String getModuleName() throws java.lang.Exception- Throws:
java.lang.Exception
-
getModuleVersion
public java.lang.String getModuleVersion() throws java.lang.Exception- Throws:
java.lang.Exception
-
exists
public boolean exists(java.lang.String path)
-
isEmpty
public boolean isEmpty()
-
setManifest
public void setManifest(java.util.jar.Manifest manifest)
-
setManifest
public void setManifest(java.io.File file) throws java.io.IOException- Throws:
java.io.IOException
-
getManifestName
public java.lang.String getManifestName()
-
setManifestName
public void setManifestName(java.lang.String manifestName)
-
write
public void write(java.io.File file) throws java.lang.Exception- Throws:
java.lang.Exception
-
write
public void write(java.lang.String file) throws java.lang.Exception- Throws:
java.lang.Exception
-
write
public void write(java.io.OutputStream to) throws java.lang.Exception- Throws:
java.lang.Exception
-
writeFolder
public void writeFolder(java.io.File dir) throws java.lang.Exception- Throws:
java.lang.Exception
-
doChecksums
public void doChecksums(java.io.OutputStream out) throws java.lang.Exception- Throws:
java.lang.Exception
-
writeManifest
public void writeManifest(java.io.OutputStream out) throws java.lang.ExceptionCleanup the manifest for writing. Cleaning up consists of adding a space after any \n to prevent the manifest to see this newline as a delimiter.- Parameters:
out- Output- Throws:
java.io.IOExceptionjava.lang.Exception
-
writeManifest
public static void writeManifest(java.util.jar.Manifest manifest, java.io.OutputStream out) throws java.io.IOException- Throws:
java.io.IOException
-
outputManifest
public static void outputManifest(java.util.jar.Manifest manifest, java.io.OutputStream out) throws java.io.IOExceptionMain function to output a manifest properly in UTF-8.- Parameters:
manifest- The manifest to outputout- The output stream- Throws:
java.io.IOException- when something fails
-
getName
public java.lang.String getName()
-
addAll
public boolean addAll(Jar sub, Instruction filter)
Add all the resources in the given jar that match the given filter.- Parameters:
sub- the jarfilter- a pattern that should match the resoures in sub to be added
-
addAll
public boolean addAll(Jar sub, Instruction filter, java.lang.String destination)
Add all the resources in the given jar that match the given filter.- Parameters:
sub- the jarfilter- a pattern that should match the resoures in sub to be added
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
lastModified
public long lastModified()
-
updateModified
public void updateModified(long time, java.lang.String reason)
-
hasDirectory
public boolean hasDirectory(java.lang.String path)
-
getPackages
public java.util.List<java.lang.String> getPackages()
-
getSource
public java.io.File getSource()
-
setSource
public void setSource(java.io.File source)
-
addAll
public boolean addAll(Jar src)
-
rename
public boolean rename(java.lang.String oldPath, java.lang.String newPath)
-
remove
public Resource remove(java.lang.String path)
-
setDoNotTouchManifest
public 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.
-
calcChecksums
public void calcChecksums(java.lang.String[] algorithms) throws java.lang.ExceptionCalculate the checksums and set them in the manifest.- Throws:
java.lang.Exception
-
getBsn
public java.lang.String getBsn() throws java.lang.ExceptionGet the jar bsn from theConstants.BUNDLE_SYMBOLICNAMEmanifest header.- Returns:
- null when the jar has no manifest, when the manifest has no
Constants.BUNDLE_SYMBOLICNAMEheader, or when the value of the header is not a valid bsn according toBSN. - Throws:
java.lang.Exception- when the jar is closed or when the manifest could not be retrieved.
-
getVersion
public java.lang.String getVersion() throws java.lang.ExceptionGet the jar version from theConstants.BUNDLE_VERSIONmanifest header.- Returns:
- null when the jar has no manifest or when the manifest has no
Constants.BUNDLE_VERSIONheader - Throws:
java.lang.Exception- when the jar is closed or when the manifest could not be retrieved.
-
expand
public void expand(java.io.File dir) throws java.lang.ExceptionExpand the JAR file to a directory.- Parameters:
dir- the dst directory, is not required to exist- Throws:
java.lang.Exception- if anything does not work as expected.
-
ensureManifest
public void ensureManifest() throws java.lang.ExceptionMake sure we have a manifest- Throws:
java.lang.Exception
-
isManifestFirst
public boolean isManifestFirst()
Answer if the manifest was the first entry
-
isReproducible
public boolean isReproducible()
-
setReproducible
public void setReproducible(java.lang.String outputTimestamp)
-
setReproducible
@Deprecated public void setReproducible(boolean reproducible)
Deprecated.Replaced bysetReproducible(String).
-
copy
public void copy(Jar srce, java.lang.String path, boolean overwrite)
-
setCompression
public void setCompression(Jar.Compression compression)
-
hasCompression
public Jar.Compression hasCompression()
-
getDataURI
public java.net.URI getDataURI(java.lang.String path, java.lang.String mime, int max) throws java.lang.ExceptionReturn a data uri from the JAR. The data must be less than 32k- Parameters:
path- the path in the jarmime- the mime type- Returns:
- a URI or null if conversion could not take place
- Throws:
java.lang.Exception
-
setDigestAlgorithms
public void setDigestAlgorithms(java.lang.String[] algorithms)
-
getTimelessDigest
public byte[] getTimelessDigest() throws java.lang.Exception- Throws:
java.lang.Exception
-
stripSignatures
public void stripSignatures()
-
removePrefix
public void removePrefix(java.lang.String prefixLow)
-
removeSubDirs
public void removeSubDirs(java.lang.String dir)
-
getPomXmlResources
public java.util.stream.Stream<Resource> getPomXmlResources()
-
setCalculateFileDigest
public Jar setCalculateFileDigest(boolean onOrOff)
Make this jar calculate the SHA256 when it is saved as a file. When this JAR is written, the digest is always cleared. If this flag is on, it will be calculated and set when the file is successfully saved.- Parameters:
onOrOff- state of calculating the digest when writing this jar. true is on, otherwise off
-
getSHA256
public java.util.Optional<byte[]> getSHA256()
Get the SHA256 digest of the last write operation whensetCalculateFileDigest(boolean)was on.- Returns:
- the SHA 256 digest or empty
-
getLength
public int getLength()
Get the length of the last written file or -1 if unavailable. The length is only calculated when the checksum calculation was on during the write.- Returns:
- the length of the last written file or -1 if unavailable.
-
-