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
021/**
022 * Wrapper for extra field data that doesn't conform to the recommended format of header-tag + size + data.
023 *
024 * <p>The header-id is artificial (and not listed as a known ID in <a
025 * href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">APPNOTE.TXT</a>).  Since it isn't used anywhere
026 * except to satisfy the ZipExtraField contract it shouldn't matter anyway.</p>
027 *
028 * @since 1.1
029 * @NotThreadSafe
030 */
031public final class UnparseableExtraFieldData implements ZipExtraField {
032    private static final ZipShort HEADER_ID = new ZipShort(0xACC1);
033
034    private byte[] localFileData;
035    private byte[] centralDirectoryData;
036
037    /**
038     * The Header-ID.
039     *
040     * @return a completely arbitrary value that should be ignored.
041     */
042    public ZipShort getHeaderId() {
043        return HEADER_ID;
044    }
045
046    /**
047     * Length of the complete extra field in the local file data.
048     *
049     * @return The LocalFileDataLength value
050     */
051    public ZipShort getLocalFileDataLength() {
052        return new ZipShort(localFileData == null ? 0 : localFileData.length);
053    }
054
055    /**
056     * Length of the complete extra field in the central directory.
057     *
058     * @return The CentralDirectoryLength value
059     */
060    public ZipShort getCentralDirectoryLength() {
061        return centralDirectoryData == null
062            ? getLocalFileDataLength()
063            : new ZipShort(centralDirectoryData.length);
064    }
065
066    /**
067     * The actual data to put into local file data.
068     *
069     * @return The LocalFileDataData value
070     */
071    public byte[] getLocalFileDataData() {
072        return ZipUtil.copy(localFileData);
073    }
074
075    /**
076     * The actual data to put into central directory.
077     *
078     * @return The CentralDirectoryData value
079     */
080    public byte[] getCentralDirectoryData() {
081        return centralDirectoryData == null
082            ? getLocalFileDataData() : ZipUtil.copy(centralDirectoryData);
083    }
084
085    /**
086     * Populate data from this array as if it was in local file data.
087     *
088     * @param buffer the buffer to read data from
089     * @param offset offset into buffer to read data
090     * @param length the length of data
091     */
092    public void parseFromLocalFileData(byte[] buffer, int offset, int length) {
093        localFileData = new byte[length];
094        System.arraycopy(buffer, offset, localFileData, 0, length);
095    }
096
097    /**
098     * Populate data from this array as if it was in central directory data.
099     *
100     * @param buffer the buffer to read data from
101     * @param offset offset into buffer to read data
102     * @param length the length of data
103     */
104    public void parseFromCentralDirectoryData(byte[] buffer, int offset,
105                                              int length) {
106        centralDirectoryData = new byte[length];
107        System.arraycopy(buffer, offset, centralDirectoryData, 0, length);
108        if (localFileData == null) {
109            parseFromLocalFileData(buffer, offset, length);
110        }
111    }
112
113}