    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 *
    017 */
    018package org.apache.commons.compress.archivers.zip;
    019
    020import java.util.zip.ZipException;
    021
    022import org.apache.commons.compress.utils.ByteUtils;
    023
    024/**
    025 * If this extra field is added as the very first extra field of the
    026 * archive, Solaris will consider it an executable jar file.
    027 * @Immutable
    028 */
    029public final class JarMarker implements ZipExtraField {
    030
    031    private static final ZipShort ID = new ZipShort(0xCAFE);
    032    private static final ZipShort NULL = new ZipShort(0);
    033    private static final JarMarker DEFAULT = new JarMarker();
    034
    035    /** No-arg constructor */
    036    public JarMarker() {
    037        // empty
    038    }
    039
    040    /**
    041     * Since JarMarker is stateless we can always use the same instance.
    042     * @return the DEFAULT jarmaker.
    043     */
    044    public static JarMarker getInstance() {
    045        return DEFAULT;
    046    }
    047
    048    /**
    049     * The Header-ID.
    050     * @return the header id
    051     */
    052    @Override
    053    public ZipShort getHeaderId() {
    054        return ID;
    055    }
    056
    057    /**
    058     * Length of the extra field in the local file data - without
    059     * Header-ID or length specifier.
    060     * @return 0
    061     */
    062    @Override
    063    public ZipShort getLocalFileDataLength() {
    064        return NULL;
    065    }
    066
    067    /**
    068     * Length of the extra field in the central directory - without
    069     * Header-ID or length specifier.
    070     * @return 0
    071     */
    072    @Override
    073    public ZipShort getCentralDirectoryLength() {
    074        return NULL;
    075    }
    076
    077    /**
    078     * The actual data to put into local file data - without Header-ID
    079     * or length specifier.
    080     * @return the data
    081     */
    082    @Override
    083    public byte[] getLocalFileDataData() {
    084        return ByteUtils.EMPTY_BYTE_ARRAY;
    085    }
    086
    087    /**
    088     * The actual data to put central directory - without Header-ID or
    089     * length specifier.
    090     * @return the data
    091     */
    092    @Override
    093    public byte[] getCentralDirectoryData() {
    094        return ByteUtils.EMPTY_BYTE_ARRAY;
    095    }
    096
    097    /**
    098     * Populate data from this array as if it was in local file data.
    099     * @param data an array of bytes
    100     * @param offset the start offset
    101     * @param length the number of bytes in the array from offset
    102     *
    103     * @throws ZipException on error
    104     */
    105    @Override
    106    public void parseFromLocalFileData(final byte[] data, final int offset, final int length)
    107        throws ZipException {
    108        if (length != 0) {
    109            throw new ZipException("JarMarker doesn't expect any data");
    110        }
    111    }
    112
    113    /**
    114     * Doesn't do anything special since this class always uses the
    115     * same data in central directory and local file data.
    116     */
    117    @Override
    118    public void parseFromCentralDirectoryData(final byte[] buffer, final int offset,
    119                                              final int length)
    120        throws ZipException {
    121        parseFromLocalFileData(buffer, offset, length);
    122    }
    123}