View Javadoc
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   *   https://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.zip;
20  
21  import java.util.zip.ZipException;
22  
23  import org.apache.commons.compress.utils.ByteUtils;
24  
25  /**
26   * If this extra field is added as the very first extra field of the archive, Solaris will consider it an executable jar file.
27   *
28   * @Immutable
29   */
30  public final class JarMarker implements ZipExtraField {
31  
32      static final ZipShort ID = new ZipShort(0xCAFE);
33      private static final ZipShort NULL = new ZipShort(0);
34      private static final JarMarker DEFAULT = new JarMarker();
35  
36      /**
37       * Since JarMarker is stateless we can always use the same instance.
38       *
39       * @return the DEFAULT jarmaker.
40       */
41      public static JarMarker getInstance() {
42          return DEFAULT;
43      }
44  
45      /** No-arg constructor */
46      public JarMarker() {
47          // empty
48      }
49  
50      /**
51       * The actual data to put central directory - without Header-ID or length specifier.
52       *
53       * @return the data
54       */
55      @Override
56      public byte[] getCentralDirectoryData() {
57          return ByteUtils.EMPTY_BYTE_ARRAY;
58      }
59  
60      /**
61       * Length of the extra field in the central directory - without Header-ID or length specifier.
62       *
63       * @return 0
64       */
65      @Override
66      public ZipShort getCentralDirectoryLength() {
67          return NULL;
68      }
69  
70      /**
71       * The Header-ID.
72       *
73       * @return the header id
74       */
75      @Override
76      public ZipShort getHeaderId() {
77          return ID;
78      }
79  
80      /**
81       * The actual data to put into local file data - without Header-ID or length specifier.
82       *
83       * @return the data
84       */
85      @Override
86      public byte[] getLocalFileDataData() {
87          return ByteUtils.EMPTY_BYTE_ARRAY;
88      }
89  
90      /**
91       * Length of the extra field in the local file data - without Header-ID or length specifier.
92       *
93       * @return 0
94       */
95      @Override
96      public ZipShort getLocalFileDataLength() {
97          return NULL;
98      }
99  
100     /**
101      * Doesn't do anything special since this class always uses the same data in central directory and local file data.
102      */
103     @Override
104     public void parseFromCentralDirectoryData(final byte[] buffer, final int offset, final int length) throws ZipException {
105         parseFromLocalFileData(buffer, offset, length);
106     }
107 
108     /**
109      * Populate data from this array as if it was in local file data.
110      *
111      * @param data   an array of bytes
112      * @param offset the start offset
113      * @param length the number of bytes in the array from offset
114      * @throws ZipException on error
115      */
116     @Override
117     public void parseFromLocalFileData(final byte[] data, final int offset, final int length) throws ZipException {
118         if (length != 0) {
119             throw new ZipException("JarMarker doesn't expect any data");
120         }
121     }
122 }