src/java.base/share/classes/jdk/internal/util/jar/VersionedStream.java
branchhttp-client-branch
changeset 55955 64298b1e890b
parent 55953 10b34c929b4f
parent 48062 f14a1972f35d
child 55957 37126be36316
equal deleted inserted replaced
55953:10b34c929b4f 55955:64298b1e890b
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.internal.util.jar;
       
    27 
       
    28 import java.util.Objects;
       
    29 import java.util.jar.JarEntry;
       
    30 import java.util.jar.JarFile;
       
    31 import java.util.stream.Stream;
       
    32 
       
    33 public class VersionedStream {
       
    34     private static final String META_INF_VERSIONS = "META-INF/versions/";
       
    35 
       
    36     /**
       
    37      * Returns a stream of versioned entries, derived from the base names of
       
    38      * all entries in a multi-release {@code JarFile} that are present either in
       
    39      * the base directory or in any versioned directory with a version number
       
    40      * less than or equal to the {@code Runtime.Version::major} that the
       
    41      * {@code JarFile} was opened with.  These versioned entries are aliases
       
    42      * for the real entries -- i.e. the names are base names and the content
       
    43      * may come from a versioned directory entry.  If the {@code jarFile} is not
       
    44      * a multi-release jar, a stream of all entries is returned.
       
    45      *
       
    46      * @param jf the input JarFile
       
    47      * @return stream of entries
       
    48      * @since 9
       
    49      */
       
    50     public static Stream<JarEntry> stream(JarFile jf) {
       
    51         if (jf.isMultiRelease()) {
       
    52             int version = jf.getVersion().major();
       
    53             return jf.stream()
       
    54                     .map(je -> getBaseSuffix(je, version))
       
    55                     .filter(Objects::nonNull)
       
    56                     .distinct()
       
    57                     .map(jf::getJarEntry);
       
    58         }
       
    59         return jf.stream();
       
    60     }
       
    61 
       
    62     private static String getBaseSuffix(JarEntry je, int version) {
       
    63         String name = je.getName();
       
    64         if (name.startsWith(META_INF_VERSIONS)) {
       
    65             int len = META_INF_VERSIONS.length();
       
    66             int index = name.indexOf('/', len);
       
    67             if (index == -1 || index == (name.length() - 1)) {
       
    68                 // filter out META-INF/versions/* and META-INF/versions/*/
       
    69                 return null;
       
    70             }
       
    71             try {
       
    72                 if (Integer.parseInt(name, len, index, 10) > version) {
       
    73                     // not an integer
       
    74                     return null;
       
    75                 }
       
    76             } catch (NumberFormatException x) {
       
    77                 // silently remove malformed entries
       
    78                 return null;
       
    79             }
       
    80             // We know name looks like META-INF/versions/*/*
       
    81             return name.substring(index + 1);
       
    82         }
       
    83         return name;
       
    84     }
       
    85 }