Package aQute.bnd.osgi
Class Jar
java.lang.Object
aQute.bnd.osgi.Jar
- All Implemented Interfaces:
Closeable,AutoCloseable
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum(package private) static final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String[]private static final Patternprivate static final intprivate booleanprivate booleanprivate Jar.Compressionprivate static final Patternprivate final NavigableMap<String, Map<String, Resource>> private booleanstatic final Object[]private intprivate longprivate Stringprivate booleanprivate Stringstatic final Patternprivate Optional<ModuleAttribute> private Stringprivate booleanprivate booleanprivate final NavigableMap<String, Resource> private SHA256private Fileprivate static final longNote 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 longprivate ZipFile -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Jar sub, Instruction filter) Add all the resources in the given jar that match the given filter.booleanaddAll(Jar sub, Instruction filter, String destination) Add all the resources in the given jar that match the given filter.booleanaddDirectory(Map<String, Resource> directory, boolean overwrite) (package private) Stringprivate JarbuildFromDirectory(Path baseDir, Pattern doNotCopy) private Jarprivate JarbuildFromResource(Resource resource) private JarbuildFromZip(File file) voidcalcChecksums(String[] algorithms) Calculate the checksums and set them in the manifest.(package private) voidcheck()private static Stringprivate static Manifestvoidclose()voidprivate voidcopyResource(File dir, String path, Resource resource) (package private) voidcreateDirectories(Set<String> directories, ZipOutputStream zip, String name) voiddoChecksums(OutputStream out) private voiddoManifest(ZipOutputStream jout, Set<String> directories, String manifestName) voidMake sure we have a manifestbooleanvoidExpand the JAR file to a directory.static JarfromResource(String name, Resource resource) getBsn()Get the jar bsn from theConstants.BUNDLE_SYMBOLICNAMEmanifest header.getDataURI(String path, String mime, int max) Return a data uri from the JAR.getDirectory(String path) intGet the length of the last written file or -1 if unavailable.getName()private static StringMake the JAR file name the project name if we get a src or bin directory.private StringgetResource(String path) getResourceNames(Predicate<String> matches) getResources(Resource jarResource, Predicate<String> filter) getResources(Predicate<String> matches) Optional<byte[]> Get the SHA256 digest of the last write operation whensetCalculateFileDigest(boolean)was on.byte[]Get the jar version from theConstants.BUNDLE_VERSIONmanifest header.booleanhasDirectory(String path) booleanisEmpty()booleanAnswer if the manifest was the first entrybooleanlong(package private) Stringmanifest()(package private) Optional<ModuleAttribute> static voidoutputManifest(Manifest manifest, OutputStream out) Main function to output a manifest properly in UTF-8.private Stringprivate voidputEntry(ZipOutputStream jout, ZipEntry entry, Resource r) booleanputResource(String path, Resource resource) booleanputResource(String path, Resource resource, boolean overwrite) voidremovePrefix(String prefixLow) voidremoveSubDirs(String dir) booleansetCalculateFileDigest(boolean onOrOff) Make this jar calculate the SHA256 when it is saved as a file.voidsetCompression(Jar.Compression compression) voidsetDigestAlgorithms(String[] algorithms) voidMake 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(File file) voidsetManifest(Manifest manifest) voidsetManifestName(String manifestName) voidvoidsetReproducible(boolean reproducible) Deprecated.voidsetReproducible(String outputTimestamp) voidvoidtoString()voidupdateModified(long time, String reason) voidvoidwrite(OutputStream to) voidvoidwriteFolder(File dir) voidCleanup the manifest for writing.static voidwriteManifest(Manifest manifest, OutputStream out) private voidwriteResource(ZipOutputStream jout, Set<String> directories, String path, Resource resource)
-
Field Details
-
BUFFER_SIZE
private static final int BUFFER_SIZE- See Also:
-
ZIP_ENTRY_CONSTANT_TIME
private static final long ZIP_ENTRY_CONSTANT_TIMENote 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. Java 7 does not do that - but in the zip not the milliseconds are saved but values for each of the date fields - but no time zone. And 1980 is the first year which can be saved. If you use January 1st 1980 then it is treated as a special flag in Java 8. Moreover, only even seconds can be stored in the zip file. Java 8 uses the upper half of some other long to store the remaining millis while Java 7 doesn't do that. So make sure that your seconds are even. Moreover, parsing happens via `new Date(millis)` inZipUtils#javaToDosTime() so we must use default timezone and locale. The date is 1980-02-01T00:00:00Z.- See Also:
-
DEFAULT_DO_NOT_COPY
-
EMPTY_ARRAY
-
resources
-
directories
-
manifest
-
moduleAttribute
-
manifestFirst
private boolean manifestFirst -
manifestName
-
name
-
source
-
zipFile
-
lastModified
private long lastModified -
lastModifiedReason
-
doNotTouchManifest
private boolean doNotTouchManifest -
nomanifest
private boolean nomanifest -
reproducible
private boolean reproducible -
compression
-
closed
private boolean closed -
algorithms
-
sha256
-
calculateFileDigest
private boolean calculateFileDigest -
fileLength
private int fileLength -
zipEntryConstantTime
private long zipEntryConstantTime -
METAINF_SIGNING_P
-
BSN
-
pomXmlFilter
-
-
Constructor Details
-
Jar
-
Jar
- Throws:
IOException
-
Jar
- Throws:
IOException
-
Jar
- Throws:
IOException
-
Jar
- Throws:
IOException
-
Jar
- Throws:
IOException
-
Jar
- Throws:
IOException
-
-
Method Details
-
fromResource
- Throws:
Exception
-
getResources
public static Stream<Resource> getResources(Resource jarResource, Predicate<String> filter) throws Exception - Throws:
Exception
-
getName
Make the JAR file name the project name if we get a src or bin directory.- Parameters:
f-
-
buildFromDirectory
- Throws:
IOException
-
buildFromZip
- Throws:
IOException
-
buildFromResource
- Throws:
Exception
-
buildFromInputStream
- Throws:
IOException
-
setName
-
toString
-
putResource
-
putResource
-
getResource
-
getResourceNames
-
getResources
-
getParent
-
getDirectories
-
getDirectory
-
getResources
-
addDirectory
-
getManifest
- Throws:
Exception
-
manifest
-
moduleAttribute
- Throws:
Exception
-
getModuleName
- Throws:
Exception
-
automaticModuleName
String automaticModuleName() -
getModuleVersion
- Throws:
Exception
-
exists
-
isEmpty
public boolean isEmpty() -
setManifest
-
setManifest
- Throws:
IOException
-
getManifestName
-
setManifestName
-
write
- Throws:
Exception
-
write
- Throws:
Exception
-
write
- Throws:
Exception
-
writeFolder
- Throws:
Exception
-
copyResource
- Throws:
Exception
-
doChecksums
- Throws:
Exception
-
padString
-
doManifest
private void doManifest(ZipOutputStream jout, Set<String> directories, String manifestName) throws Exception - Throws:
Exception
-
putEntry
- Throws:
Exception
-
writeManifest
Cleanup 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:
IOExceptionException
-
writeManifest
- Throws:
IOException
-
outputManifest
Main function to output a manifest properly in UTF-8.- Parameters:
manifest- The manifest to outputout- The output stream- Throws:
IOException- when something fails
-
clean
-
clean
-
writeResource
private void writeResource(ZipOutputStream jout, Set<String> directories, String path, Resource resource) throws Exception - Throws:
Exception
-
createDirectories
void createDirectories(Set<String> directories, ZipOutputStream zip, String name) throws IOException - Throws:
IOException
-
getName
-
addAll
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
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 interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
lastModified
public long lastModified() -
lastModifiedReason
String lastModifiedReason() -
updateModified
-
hasDirectory
-
getPackages
-
getSource
-
setSource
-
addAll
-
rename
-
remove
-
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
Calculate the checksums and set them in the manifest.- Throws:
Exception
-
getBsn
Get 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:
Exception- when the jar is closed or when the manifest could not be retrieved.
-
getVersion
Get 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:
Exception- when the jar is closed or when the manifest could not be retrieved.
-
expand
Expand the JAR file to a directory.- Parameters:
dir- the dst directory, is not required to exist- Throws:
Exception- if anything does not work as expected.
-
ensureManifest
Make sure we have a manifest- Throws:
Exception
-
isManifestFirst
public boolean isManifestFirst()Answer if the manifest was the first entry -
isReproducible
public boolean isReproducible() -
setReproducible
-
setReproducible
Deprecated.Replaced bysetReproducible(String). -
copy
-
setCompression
-
hasCompression
-
check
void check() -
getDataURI
Return 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:
Exception
-
setDigestAlgorithms
-
getTimelessDigest
- Throws:
Exception
-
stripSignatures
public void stripSignatures() -
removePrefix
-
removeSubDirs
-
getPomXmlResources
-
setCalculateFileDigest
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
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.
-
setReproducible(String).