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
025 * archive, Solaris will consider it an executable jar file.
026 * @Immutable
027 */
028public final class JarMarker implements ZipExtraField {
029
030    private 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     * @return the DEFAULT jarmaker.
037     */
038    public static JarMarker getInstance() {
039        return DEFAULT;
040    }
041
042    /** No-arg constructor */
043    public JarMarker() {
044        // empty
045    }
046
047    /**
048     * The actual data to put central directory - without Header-ID or
049     * length specifier.
050     * @return the data
051     */
052    @Override
053    public byte[] getCentralDirectoryData() {
054        return ByteUtils.EMPTY_BYTE_ARRAY;
055    }
056
057    /**
058     * Length of the extra field in the central directory - without
059     * Header-ID or length specifier.
060     * @return 0
061     */
062    @Override
063    public ZipShort getCentralDirectoryLength() {
064        return NULL;
065    }
066
067    /**
068     * The Header-ID.
069     * @return the header id
070     */
071    @Override
072    public ZipShort getHeaderId() {
073        return ID;
074    }
075
076    /**
077     * The actual data to put into local file data - without Header-ID
078     * or length specifier.
079     * @return the data
080     */
081    @Override
082    public byte[] getLocalFileDataData() {
083        return ByteUtils.EMPTY_BYTE_ARRAY;
084    }
085
086    /**
087     * Length of the extra field in the local file data - without
088     * Header-ID or length specifier.
089     * @return 0
090     */
091    @Override
092    public ZipShort getLocalFileDataLength() {
093        return NULL;
094    }
095
096    /**
097     * Doesn't do anything special since this class always uses the
098     * same data in central directory and local file data.
099     */
100    @Override
101    public void parseFromCentralDirectoryData(final byte[] buffer, final int offset,
102                                              final int length)
103        throws ZipException {
104        parseFromLocalFileData(buffer, offset, length);
105    }
106
107    /**
108     * Populate data from this array as if it was in local file data.
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)
117        throws ZipException {
118        if (length != 0) {
119            throw new ZipException("JarMarker doesn't expect any data");
120        }
121    }
122}