< prev index next >

src/jdk.zipfs/share/classes/jdk/nio/zipfs/JarFileSystem.java

Print this page
rev 16859 : 8176709: JarFileSystem::isMultiReleaseJar is incorrect
Reviewed-by: alanb, sherman, psandoz, mchung


  71                 if (s.equals("runtime")) {
  72                     version = Runtime.version().major();
  73                 } else {
  74                     version = Integer.parseInt(s);
  75                 }
  76             } else if (o instanceof Integer) {
  77                 version = (Integer)o;
  78             } else if (o instanceof Version) {
  79                 version = ((Version)o).major();
  80             } else {
  81                 throw new IllegalArgumentException("env parameter must be String, Integer, "
  82                         + "or Version");
  83             }
  84             lookup = createVersionedLinks(version < 0 ? 0 : version);
  85             setReadOnly();
  86         }
  87     }
  88 
  89     private boolean isMultiReleaseJar() {
  90         try (InputStream is = newInputStream(getBytes("/META-INF/MANIFEST.MF"))) {
  91             return (new Manifest(is)).getMainAttributes()
  92                     .containsKey(new Attributes.Name("Multi-Release"));
  93             // fixme change line above after JarFile integration to contain Attributes.Name.MULTI_RELEASE




























  94         } catch (IOException x) {
  95             return false;
  96         }


































































  97     }
  98 
  99     /**
 100      * create a map of aliases for versioned entries, for example:
 101      *   version/PackagePrivate.class -> META-INF/versions/9/version/PackagePrivate.class
 102      *   version/PackagePrivate.java -> META-INF/versions/9/version/PackagePrivate.java
 103      *   version/Version.class -> META-INF/versions/10/version/Version.class
 104      *   version/Version.java -> META-INF/versions/10/version/Version.java
 105      *
 106      * then wrap the map in a function that getEntry can use to override root
 107      * entry lookup for entries that have corresponding versioned entries
 108      */
 109     private Function<byte[],byte[]> createVersionedLinks(int version) {
 110         HashMap<IndexNode,byte[]> aliasMap = new HashMap<>();
 111         getVersionMap(version, getInode(getBytes("/META-INF/versions"))).values()
 112                 .forEach(versionNode -> {   // for each META-INF/versions/{n} directory
 113                     // put all the leaf inodes, i.e. entries, into the alias map
 114                     // possibly shadowing lower versioned entries
 115                     walk(versionNode, entryNode -> {
 116                         byte[] rootName = getRootName(versionNode, entryNode);




  71                 if (s.equals("runtime")) {
  72                     version = Runtime.version().major();
  73                 } else {
  74                     version = Integer.parseInt(s);
  75                 }
  76             } else if (o instanceof Integer) {
  77                 version = (Integer)o;
  78             } else if (o instanceof Version) {
  79                 version = ((Version)o).major();
  80             } else {
  81                 throw new IllegalArgumentException("env parameter must be String, Integer, "
  82                         + "or Version");
  83             }
  84             lookup = createVersionedLinks(version < 0 ? 0 : version);
  85             setReadOnly();
  86         }
  87     }
  88 
  89     private boolean isMultiReleaseJar() {
  90         try (InputStream is = newInputStream(getBytes("/META-INF/MANIFEST.MF"))) {
  91             byte[] b = is.readAllBytes();
  92             // Keep this implementation up to date with
  93             // JarFile::checkForSpecialAttributes
  94             int i = match(MULTIRELEASE_CHARS, b, MULTIRELEASE_LASTOCC,
  95                           MULTIRELEASE_OPTOSFT);
  96             if (i != -1) {
  97                 i += MULTIRELEASE_CHARS.length;
  98                 if (i < b.length) {
  99                     byte c = b[i++];
 100                     // Check that the value is followed by a newline
 101                     // and does not have a continuation
 102                     if (c == '\n' &&
 103                             (i == b.length || b[i] != ' ')) {
 104                         return true;
 105                     } else if (c == '\r') {
 106                         if (i == b.length) {
 107                             return true;
 108                         } else {
 109                             c = b[i++];
 110                             if (c == '\n') {
 111                                 if (i == b.length || b[i] != ' ') {
 112                                     return true;
 113                                 }
 114                             } else if (c != ' ') {
 115                                 return true;
 116                             }
 117                         }
 118                     }
 119                 }
 120             }
 121             return false;
 122         } catch (IOException x) {
 123             return false;
 124         }
 125     }
 126 
 127     private static final byte[] MULTIRELEASE_CHARS =
 128             {'M','U','L','T','I','-','R','E','L','E', 'A', 'S', 'E', ':',
 129                     ' ', 'T', 'R', 'U', 'E'};
 130 
 131     // The bad character shift for "multi-release: true"
 132     private static final byte[] MULTIRELEASE_LASTOCC;
 133 
 134     // The good suffix shift for "multi-release: true"
 135     private static final byte[] MULTIRELEASE_OPTOSFT;
 136 
 137     static {
 138         MULTIRELEASE_LASTOCC = new byte[64];
 139         MULTIRELEASE_OPTOSFT = new byte[19];
 140         MULTIRELEASE_LASTOCC[(int)'M' - 32] = 1;
 141         MULTIRELEASE_LASTOCC[(int)'I' - 32] = 5;
 142         MULTIRELEASE_LASTOCC[(int)'-' - 32] = 6;
 143         MULTIRELEASE_LASTOCC[(int)'L' - 32] = 9;
 144         MULTIRELEASE_LASTOCC[(int)'A' - 32] = 11;
 145         MULTIRELEASE_LASTOCC[(int)'S' - 32] = 12;
 146         MULTIRELEASE_LASTOCC[(int)':' - 32] = 14;
 147         MULTIRELEASE_LASTOCC[(int)' ' - 32] = 15;
 148         MULTIRELEASE_LASTOCC[(int)'T' - 32] = 16;
 149         MULTIRELEASE_LASTOCC[(int)'R' - 32] = 17;
 150         MULTIRELEASE_LASTOCC[(int)'U' - 32] = 18;
 151         MULTIRELEASE_LASTOCC[(int)'E' - 32] = 19;
 152         for (int i = 0; i < 17; i++) {
 153             MULTIRELEASE_OPTOSFT[i] = 19;
 154         }
 155         MULTIRELEASE_OPTOSFT[17] = 6;
 156         MULTIRELEASE_OPTOSFT[18] = 1;
 157     }
 158 
 159     /**
 160      * Returns true if the pattern {@code src} is found in {@code b}.
 161      * The {@code lastOcc} array is the precomputed bad character shifts.
 162      * Since there are no repeated substring in our search strings,
 163      * the good suffix shifts can be replaced with a comparison.
 164      */
 165     private static int match(byte[] src, byte[] b, byte[] lastOcc, byte[] optoSft) {
 166         int len = src.length;
 167         int last = b.length - len;
 168         int i = 0;
 169         next:
 170         while (i <= last) {
 171             for (int j = (len - 1); j >= 0; j--) {
 172                 byte c = b[i + j];
 173                 if (c >= ' ' && c <= 'z') {
 174                     if (c >= 'a') c -= 32; // Canonicalize
 175 
 176                     if (c != src[j]) {
 177                         // no match
 178                         int badShift = lastOcc[c - 32];
 179                         i += Math.max(j + 1 - badShift, optoSft[j]);
 180                         continue next;
 181                     }
 182                 } else {
 183                     // no match, character not valid for name
 184                     i += len;
 185                     continue next;
 186                 }
 187             }
 188             return i;
 189         }
 190         return -1;
 191     }
 192 
 193     /**
 194      * create a map of aliases for versioned entries, for example:
 195      *   version/PackagePrivate.class -> META-INF/versions/9/version/PackagePrivate.class
 196      *   version/PackagePrivate.java -> META-INF/versions/9/version/PackagePrivate.java
 197      *   version/Version.class -> META-INF/versions/10/version/Version.class
 198      *   version/Version.java -> META-INF/versions/10/version/Version.java
 199      *
 200      * then wrap the map in a function that getEntry can use to override root
 201      * entry lookup for entries that have corresponding versioned entries
 202      */
 203     private Function<byte[],byte[]> createVersionedLinks(int version) {
 204         HashMap<IndexNode,byte[]> aliasMap = new HashMap<>();
 205         getVersionMap(version, getInode(getBytes("/META-INF/versions"))).values()
 206                 .forEach(versionNode -> {   // for each META-INF/versions/{n} directory
 207                     // put all the leaf inodes, i.e. entries, into the alias map
 208                     // possibly shadowing lower versioned entries
 209                     walk(versionNode, entryNode -> {
 210                         byte[] rootName = getRootName(versionNode, entryNode);


< prev index next >