JarMarker.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.archivers.zip;

  18. import java.util.zip.ZipException;

  19. import org.apache.commons.compress.utils.ByteUtils;

  20. /**
  21.  * If this extra field is added as the very first extra field of the archive, Solaris will consider it an executable jar file.
  22.  *
  23.  * @Immutable
  24.  */
  25. public final class JarMarker implements ZipExtraField {

  26.     static final ZipShort ID = new ZipShort(0xCAFE);
  27.     private static final ZipShort NULL = new ZipShort(0);
  28.     private static final JarMarker DEFAULT = new JarMarker();

  29.     /**
  30.      * Since JarMarker is stateless we can always use the same instance.
  31.      *
  32.      * @return the DEFAULT jarmaker.
  33.      */
  34.     public static JarMarker getInstance() {
  35.         return DEFAULT;
  36.     }

  37.     /** No-arg constructor */
  38.     public JarMarker() {
  39.         // empty
  40.     }

  41.     /**
  42.      * The actual data to put central directory - without Header-ID or length specifier.
  43.      *
  44.      * @return the data
  45.      */
  46.     @Override
  47.     public byte[] getCentralDirectoryData() {
  48.         return ByteUtils.EMPTY_BYTE_ARRAY;
  49.     }

  50.     /**
  51.      * Length of the extra field in the central directory - without Header-ID or length specifier.
  52.      *
  53.      * @return 0
  54.      */
  55.     @Override
  56.     public ZipShort getCentralDirectoryLength() {
  57.         return NULL;
  58.     }

  59.     /**
  60.      * The Header-ID.
  61.      *
  62.      * @return the header id
  63.      */
  64.     @Override
  65.     public ZipShort getHeaderId() {
  66.         return ID;
  67.     }

  68.     /**
  69.      * The actual data to put into local file data - without Header-ID or length specifier.
  70.      *
  71.      * @return the data
  72.      */
  73.     @Override
  74.     public byte[] getLocalFileDataData() {
  75.         return ByteUtils.EMPTY_BYTE_ARRAY;
  76.     }

  77.     /**
  78.      * Length of the extra field in the local file data - without Header-ID or length specifier.
  79.      *
  80.      * @return 0
  81.      */
  82.     @Override
  83.     public ZipShort getLocalFileDataLength() {
  84.         return NULL;
  85.     }

  86.     /**
  87.      * Doesn't do anything special since this class always uses the same data in central directory and local file data.
  88.      */
  89.     @Override
  90.     public void parseFromCentralDirectoryData(final byte[] buffer, final int offset, final int length) throws ZipException {
  91.         parseFromLocalFileData(buffer, offset, length);
  92.     }

  93.     /**
  94.      * Populate data from this array as if it was in local file data.
  95.      *
  96.      * @param data   an array of bytes
  97.      * @param offset the start offset
  98.      * @param length the number of bytes in the array from offset
  99.      *
  100.      * @throws ZipException on error
  101.      */
  102.     @Override
  103.     public void parseFromLocalFileData(final byte[] data, final int offset, final int length) throws ZipException {
  104.         if (length != 0) {
  105.             throw new ZipException("JarMarker doesn't expect any data");
  106.         }
  107.     }
  108. }