001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.commons.compress.archivers.zip;
018
019import java.util.zip.ZipException;
020
021import org.apache.commons.compress.utils.ByteUtils;
022
023/**
024 * If this extra field is added as the very first extra field of the archive, Solaris will consider it an executable jar file.
025 *
026 * @Immutable
027 */
028public final class JarMarker implements ZipExtraField {
029
030    static final ZipShort ID = new ZipShort(0xCAFE);
031    private static final ZipShort NULL = new ZipShort(0);
032    private static final JarMarker DEFAULT = new JarMarker();
033
034    /**
035     * Since JarMarker is stateless we can always use the same instance.
036     *
037     * @return the DEFAULT jarmaker.
038     */
039    public static JarMarker getInstance() {
040        return DEFAULT;
041    }
042
043    /** No-arg constructor */
044    public JarMarker() {
045        // empty
046    }
047
048    /**
049     * The actual data to put central directory - without Header-ID or length specifier.
050     *
051     * @return the data
052     */
053    @Override
054    public byte[] getCentralDirectoryData() {
055        return ByteUtils.EMPTY_BYTE_ARRAY;
056    }
057
058    /**
059     * Length of the extra field in the central directory - without Header-ID or length specifier.
060     *
061     * @return 0
062     */
063    @Override
064    public ZipShort getCentralDirectoryLength() {
065        return NULL;
066    }
067
068    /**
069     * The Header-ID.
070     *
071     * @return the header id
072     */
073    @Override
074    public ZipShort getHeaderId() {
075        return ID;
076    }
077
078    /**
079     * The actual data to put into local file data - without Header-ID or length specifier.
080     *
081     * @return the data
082     */
083    @Override
084    public byte[] getLocalFileDataData() {
085        return ByteUtils.EMPTY_BYTE_ARRAY;
086    }
087
088    /**
089     * Length of the extra field in the local file data - without Header-ID or length specifier.
090     *
091     * @return 0
092     */
093    @Override
094    public ZipShort getLocalFileDataLength() {
095        return NULL;
096    }
097
098    /**
099     * Doesn't do anything special since this class always uses the same data in central directory and local file data.
100     */
101    @Override
102    public void parseFromCentralDirectoryData(final byte[] buffer, final int offset, final int length) throws ZipException {
103        parseFromLocalFileData(buffer, offset, length);
104    }
105
106    /**
107     * Populate data from this array as if it was in local file data.
108     *
109     * @param data   an array of bytes
110     * @param offset the start offset
111     * @param length the number of bytes in the array from offset
112     *
113     * @throws ZipException on error
114     */
115    @Override
116    public void parseFromLocalFileData(final byte[] data, final int offset, final int length) throws ZipException {
117        if (length != 0) {
118            throw new ZipException("JarMarker doesn't expect any data");
119        }
120    }
121}