    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 * http://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.commons.compress.archivers.zip;
    020
    021import java.util.Arrays;
    022
    023/**
    024 * Simple placeholder for all those extra fields we don't want to deal with.
    025 *
    026 * <p>
    027 * Assumes local file data and central directory entries are identical - unless told the opposite.
    028 * </p>
    029 *
    030 * @NotThreadSafe
    031 */
    032public class UnrecognizedExtraField implements ZipExtraField {
    033
    034    /**
    035     * The Header-ID.
    036     */
    037    private ZipShort headerId;
    038
    039    /**
    040     * Extra field data in local file data - without Header-ID or length specifier.
    041     */
    042    private byte[] localData;
    043
    044    /**
    045     * Extra field data in central directory - without Header-ID or length specifier.
    046     */
    047    private byte[] centralData;
    048
    049    /**
    050     * Gets the central data.
    051     *
    052     * @return the central data if present, else return the local file data
    053     */
    054    @Override
    055    public byte[] getCentralDirectoryData() {
    056        if (centralData != null) {
    057            return ZipUtil.copy(centralData);
    058        }
    059        return getLocalFileDataData();
    060    }
    061
    062    /**
    063     * Gets the central data length. If there is no central data, get the local file data length.
    064     *
    065     * @return the central data length
    066     */
    067    @Override
    068    public ZipShort getCentralDirectoryLength() {
    069        if (centralData != null) {
    070            return new ZipShort(centralData.length);
    071        }
    072        return getLocalFileDataLength();
    073    }
    074
    075    /**
    076     * Gets the header id.
    077     *
    078     * @return the header id
    079     */
    080    @Override
    081    public ZipShort getHeaderId() {
    082        return headerId;
    083    }
    084
    085    /**
    086     * Gets the local data.
    087     *
    088     * @return the local data
    089     */
    090    @Override
    091    public byte[] getLocalFileDataData() {
    092        return ZipUtil.copy(localData);
    093    }
    094
    095    /**
    096     * Gets the length of the local data.
    097     *
    098     * @return the length of the local data
    099     */
    100    @Override
    101    public ZipShort getLocalFileDataLength() {
    102        return new ZipShort(localData != null ? localData.length : 0);
    103    }
    104
    105    /**
    106     * @param data   the array of bytes.
    107     * @param offset the source location in the data array.
    108     * @param length the number of bytes to use in the data array.
    109     * @see ZipExtraField#parseFromCentralDirectoryData(byte[], int, int)
    110     */
    111    @Override
    112    public void parseFromCentralDirectoryData(final byte[] data, final int offset, final int length) {
    113        final byte[] tmp = Arrays.copyOfRange(data, offset, offset + length);
    114        setCentralDirectoryData(tmp);
    115        if (localData == null) {
    116            setLocalFileDataData(tmp);
    117        }
    118    }
    119
    120    /**
    121     * @param data   the array of bytes.
    122     * @param offset the source location in the data array.
    123     * @param length the number of bytes to use in the data array.
    124     * @see ZipExtraField#parseFromLocalFileData(byte[], int, int)
    125     */
    126    @Override
    127    public void parseFromLocalFileData(final byte[] data, final int offset, final int length) {
    128        setLocalFileDataData(Arrays.copyOfRange(data, offset, offset + length));
    129    }
    130
    131    /**
    132     * Sets the extra field data in central directory.
    133     *
    134     * @param data the data to use
    135     */
    136    public void setCentralDirectoryData(final byte[] data) {
    137        centralData = ZipUtil.copy(data);
    138    }
    139
    140    /**
    141     * Sets the header id.
    142     *
    143     * @param headerId the header id to use
    144     */
    145    public void setHeaderId(final ZipShort headerId) {
    146        this.headerId = headerId;
    147    }
    148
    149    /**
    150     * Sets the extra field data in the local file data - without Header-ID or length specifier.
    151     *
    152     * @param data the field data to use
    153     */
    154    public void setLocalFileDataData(final byte[] data) {
    155        localData = ZipUtil.copy(data);
    156    }
    157
    158}