• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 package org.apache.commons.compress.archivers.jar;
20 
21 import java.io.IOException;
22 import java.io.InputStream;
23 
24 import org.apache.commons.compress.archivers.ArchiveEntry;
25 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
26 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
27 
28 /**
29  * Implements an input stream that can read entries from jar files.
30  *
31  * @NotThreadSafe
32  */
33 public class JarArchiveInputStream extends ZipArchiveInputStream {
34 
35     /**
36      * Creates an instance from the input stream using the default encoding.
37      *
38      * @param inputStream the input stream to wrap
39      */
JarArchiveInputStream( final InputStream inputStream )40     public JarArchiveInputStream( final InputStream inputStream ) {
41         super(inputStream);
42     }
43 
44     /**
45      * Creates an instance from the input stream using the specified encoding.
46      *
47      * @param inputStream the input stream to wrap
48      * @param encoding the encoding to use
49      * @since 1.10
50      */
JarArchiveInputStream( final InputStream inputStream, final String encoding )51     public JarArchiveInputStream( final InputStream inputStream, final String encoding ) {
52         super(inputStream, encoding);
53     }
54 
getNextJarEntry()55     public JarArchiveEntry getNextJarEntry() throws IOException {
56         final ZipArchiveEntry entry = getNextZipEntry();
57         return entry == null ? null : new JarArchiveEntry(entry);
58     }
59 
60     @Override
getNextEntry()61     public ArchiveEntry getNextEntry() throws IOException {
62         return getNextJarEntry();
63     }
64 
65     /**
66      * Checks if the signature matches what is expected for a jar file
67      * (in this case it is the same as for a zip file).
68      *
69      * @param signature
70      *            the bytes to check
71      * @param length
72      *            the number of bytes to check
73      * @return true, if this stream is a jar archive stream, false otherwise
74      */
matches(final byte[] signature, final int length )75     public static boolean matches(final byte[] signature, final int length ) {
76         return ZipArchiveInputStream.matches(signature, length);
77     }
78 }
79