Class Jar

java.lang.Object
aQute.bnd.osgi.Jar
All Implemented Interfaces:
Closeable, AutoCloseable

public class Jar extends Object implements Closeable
  • Field Details

    • BUFFER_SIZE

      private static final int BUFFER_SIZE
      See Also:
    • ZIP_ENTRY_CONSTANT_TIME

      private static final 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. 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)` in ZipUtils#javaToDosTime() so we must use default timezone and locale. The date is 1980-02-01T00:00:00Z.
      See Also:
    • DEFAULT_DO_NOT_COPY

      private static final Pattern DEFAULT_DO_NOT_COPY
    • EMPTY_ARRAY

      public static final Object[] EMPTY_ARRAY
    • resources

      private final NavigableMap<String,Resource> resources
    • directories

      private final NavigableMap<String,Map<String,Resource>> directories
    • manifest

      private Optional<Manifest> manifest
    • moduleAttribute

      private Optional<ModuleAttribute> moduleAttribute
    • manifestFirst

      private boolean manifestFirst
    • manifestName

      private String manifestName
    • name

      private String name
    • source

      private File source
    • zipFile

      private ZipFile zipFile
    • lastModified

      private long lastModified
    • lastModifiedReason

      private String lastModifiedReason
    • doNotTouchManifest

      private boolean doNotTouchManifest
    • nomanifest

      private boolean nomanifest
    • reproducible

      private boolean reproducible
    • compression

      private Jar.Compression compression
    • closed

      private boolean closed
    • algorithms

      private String[] algorithms
    • sha256

      private SHA256 sha256
    • calculateFileDigest

      private boolean calculateFileDigest
    • fileLength

      private int fileLength
    • zipEntryConstantTime

      private long zipEntryConstantTime
    • METAINF_SIGNING_P

      public static final Pattern METAINF_SIGNING_P
    • BSN

      private static final Pattern BSN
    • pomXmlFilter

      private static final Predicate<String> pomXmlFilter
  • Constructor Details

  • Method Details

    • fromResource

      public static Jar fromResource(String name, Resource resource) throws Exception
      Throws:
      Exception
    • getResources

      public static Stream<Resource> getResources(Resource jarResource, Predicate<String> filter) throws Exception
      Throws:
      Exception
    • getName

      private static String getName(File f)
      Make the JAR file name the project name if we get a src or bin directory.
      Parameters:
      f -
    • buildFromDirectory

      private Jar buildFromDirectory(Path baseDir, Pattern doNotCopy) throws IOException
      Throws:
      IOException
    • buildFromZip

      private Jar buildFromZip(File file) throws IOException
      Throws:
      IOException
    • buildFromResource

      private Jar buildFromResource(Resource resource) throws Exception
      Throws:
      Exception
    • buildFromInputStream

      private Jar buildFromInputStream(InputStream in) throws IOException
      Throws:
      IOException
    • setName

      public void setName(String name)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • putResource

      public boolean putResource(String path, Resource resource)
    • putResource

      public boolean putResource(String path, Resource resource, boolean overwrite)
    • getResource

      public Resource getResource(String path)
    • getResourceNames

      public Stream<String> getResourceNames(Predicate<String> matches)
    • getResources

      public Stream<Resource> getResources(Predicate<String> matches)
    • getParent

      private String getParent(String path)
    • getDirectories

      public Map<String,Map<String,Resource>> getDirectories()
    • getDirectory

      public Map<String,Resource> getDirectory(String path)
    • getResources

      public Map<String,Resource> getResources()
    • addDirectory

      public boolean addDirectory(Map<String,Resource> directory, boolean overwrite)
    • getManifest

      public Manifest getManifest() throws Exception
      Throws:
      Exception
    • manifest

      Optional<Manifest> manifest()
    • moduleAttribute

      Optional<ModuleAttribute> moduleAttribute() throws Exception
      Throws:
      Exception
    • getModuleName

      public String getModuleName() throws Exception
      Throws:
      Exception
    • automaticModuleName

      String automaticModuleName()
    • getModuleVersion

      public String getModuleVersion() throws Exception
      Throws:
      Exception
    • exists

      public boolean exists(String path)
    • isEmpty

      public boolean isEmpty()
    • setManifest

      public void setManifest(Manifest manifest)
    • setManifest

      public void setManifest(File file) throws IOException
      Throws:
      IOException
    • getManifestName

      public String getManifestName()
    • setManifestName

      public void setManifestName(String manifestName)
    • write

      public void write(File file) throws Exception
      Throws:
      Exception
    • write

      public void write(String file) throws Exception
      Throws:
      Exception
    • write

      public void write(OutputStream to) throws Exception
      Throws:
      Exception
    • writeFolder

      public void writeFolder(File dir) throws Exception
      Throws:
      Exception
    • copyResource

      private void copyResource(File dir, String path, Resource resource) throws Exception
      Throws:
      Exception
    • doChecksums

      public void doChecksums(OutputStream out) throws Exception
      Throws:
      Exception
    • padString

      private String padString(String s, int length, char pad)
    • doManifest

      private void doManifest(ZipOutputStream jout, Set<String> directories, String manifestName) throws Exception
      Throws:
      Exception
    • putEntry

      private void putEntry(ZipOutputStream jout, ZipEntry entry, Resource r) throws Exception
      Throws:
      Exception
    • writeManifest

      public void writeManifest(OutputStream out) throws Exception
      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:
      IOException
      Exception
    • writeManifest

      public static void writeManifest(Manifest manifest, OutputStream out) throws IOException
      Throws:
      IOException
    • outputManifest

      public static void outputManifest(Manifest manifest, OutputStream out) throws IOException
      Main function to output a manifest properly in UTF-8.
      Parameters:
      manifest - The manifest to output
      out - The output stream
      Throws:
      IOException - when something fails
    • clean

      private static Manifest clean(Manifest org)
    • clean

      private static String clean(String s)
    • 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

      public 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 jar
      filter - a pattern that should match the resoures in sub to be added
    • addAll

      public boolean addAll(Jar sub, Instruction filter, String destination)
      Add all the resources in the given jar that match the given filter.
      Parameters:
      sub - the jar
      filter - a pattern that should match the resoures in sub to be added
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • lastModified

      public long lastModified()
    • lastModifiedReason

      String lastModifiedReason()
    • updateModified

      public void updateModified(long time, String reason)
    • hasDirectory

      public boolean hasDirectory(String path)
    • getPackages

      public List<String> getPackages()
    • getSource

      public File getSource()
    • setSource

      public void setSource(File source)
    • addAll

      public boolean addAll(Jar src)
    • rename

      public boolean rename(String oldPath, String newPath)
    • remove

      public Resource remove(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(String[] algorithms) throws Exception
      Calculate the checksums and set them in the manifest.
      Throws:
      Exception
    • getBsn

      public String getBsn() throws Exception
      Get the jar bsn from the Constants.BUNDLE_SYMBOLICNAME manifest header.
      Returns:
      null when the jar has no manifest, when the manifest has no Constants.BUNDLE_SYMBOLICNAME header, or when the value of the header is not a valid bsn according to BSN.
      Throws:
      Exception - when the jar is closed or when the manifest could not be retrieved.
    • getVersion

      public String getVersion() throws Exception
      Get the jar version from the Constants.BUNDLE_VERSION manifest header.
      Returns:
      null when the jar has no manifest or when the manifest has no Constants.BUNDLE_VERSION header
      Throws:
      Exception - when the jar is closed or when the manifest could not be retrieved.
    • expand

      public void expand(File dir) throws Exception
      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

      public void ensureManifest() throws Exception
      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

      public void setReproducible(String outputTimestamp)
    • setReproducible

      @Deprecated public void setReproducible(boolean reproducible)
      Deprecated.
    • copy

      public void copy(Jar srce, String path, boolean overwrite)
    • setCompression

      public void setCompression(Jar.Compression compression)
    • hasCompression

      public Jar.Compression hasCompression()
    • check

      void check()
    • getDataURI

      public URI getDataURI(String path, String mime, int max) throws Exception
      Return a data uri from the JAR. The data must be less than 32k
      Parameters:
      path - the path in the jar
      mime - the mime type
      Returns:
      a URI or null if conversion could not take place
      Throws:
      Exception
    • setDigestAlgorithms

      public void setDigestAlgorithms(String[] algorithms)
    • getTimelessDigest

      public byte[] getTimelessDigest() throws Exception
      Throws:
      Exception
    • stripSignatures

      public void stripSignatures()
    • removePrefix

      public void removePrefix(String prefixLow)
    • removeSubDirs

      public void removeSubDirs(String dir)
    • getPomXmlResources

      public 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 Optional<byte[]> getSHA256()
      Get the SHA256 digest of the last write operation when setCalculateFileDigest(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.