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