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    /**
036     * Since JarMarker is stateless we can always use the same instance.
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
050     * length specifier.
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
060     * Header-ID or length specifier.
061     * @return 0
062     */
063    @Override
064    public ZipShort getCentralDirectoryLength() {
065        return NULL;
066    }
067
068    /**
069     * The Header-ID.
070     * @return the header id
071     */
072    @Override
073    public ZipShort getHeaderId() {
074        return ID;
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     * Length of the extra field in the local file data - without
089     * Header-ID or length specifier.
090     * @return 0
091     */
092    @Override
093    public ZipShort getLocalFileDataLength() {
094        return NULL;
095    }
096
097    /**
098     * Doesn't do anything special since this class always uses the
099     * same data in central directory and local file data.
100     */
101    @Override
102    public void parseFromCentralDirectoryData(final byte[] buffer, final int offset,
103                                              final int length)
104        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     * @param data an array of bytes
111     * @param offset the start offset
112     * @param length the number of bytes in the array from offset
113     *
114     * @throws ZipException on error
115     */
116    @Override
117    public void parseFromLocalFileData(final byte[] data, final int offset, final int length)
118        throws ZipException {
119        if (length != 0) {
120            throw new ZipException("JarMarker doesn't expect any data");
121        }
122    }
123}