UnrecognizedExtraField.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.archivers.zip;

  20. import java.util.Arrays;

  21. /**
  22.  * Simple placeholder for all those extra fields we don't want to deal with.
  23.  *
  24.  * <p>
  25.  * Assumes local file data and central directory entries are identical - unless told the opposite.
  26.  * </p>
  27.  *
  28.  * @NotThreadSafe
  29.  */
  30. public class UnrecognizedExtraField implements ZipExtraField {

  31.     /**
  32.      * The Header-ID.
  33.      */
  34.     private ZipShort headerId;

  35.     /**
  36.      * Extra field data in local file data - without Header-ID or length specifier.
  37.      */
  38.     private byte[] localData;

  39.     /**
  40.      * Extra field data in central directory - without Header-ID or length specifier.
  41.      */
  42.     private byte[] centralData;

  43.     /**
  44.      * Gets the central data.
  45.      *
  46.      * @return the central data if present, else return the local file data
  47.      */
  48.     @Override
  49.     public byte[] getCentralDirectoryData() {
  50.         if (centralData != null) {
  51.             return ZipUtil.copy(centralData);
  52.         }
  53.         return getLocalFileDataData();
  54.     }

  55.     /**
  56.      * Gets the central data length. If there is no central data, get the local file data length.
  57.      *
  58.      * @return the central data length
  59.      */
  60.     @Override
  61.     public ZipShort getCentralDirectoryLength() {
  62.         if (centralData != null) {
  63.             return new ZipShort(centralData.length);
  64.         }
  65.         return getLocalFileDataLength();
  66.     }

  67.     /**
  68.      * Gets the header id.
  69.      *
  70.      * @return the header id
  71.      */
  72.     @Override
  73.     public ZipShort getHeaderId() {
  74.         return headerId;
  75.     }

  76.     /**
  77.      * Gets the local data.
  78.      *
  79.      * @return the local data
  80.      */
  81.     @Override
  82.     public byte[] getLocalFileDataData() {
  83.         return ZipUtil.copy(localData);
  84.     }

  85.     /**
  86.      * Gets the length of the local data.
  87.      *
  88.      * @return the length of the local data
  89.      */
  90.     @Override
  91.     public ZipShort getLocalFileDataLength() {
  92.         return new ZipShort(localData != null ? localData.length : 0);
  93.     }

  94.     /**
  95.      * @param data   the array of bytes.
  96.      * @param offset the source location in the data array.
  97.      * @param length the number of bytes to use in the data array.
  98.      * @see ZipExtraField#parseFromCentralDirectoryData(byte[], int, int)
  99.      */
  100.     @Override
  101.     public void parseFromCentralDirectoryData(final byte[] data, final int offset, final int length) {
  102.         final byte[] tmp = Arrays.copyOfRange(data, offset, offset + length);
  103.         setCentralDirectoryData(tmp);
  104.         if (localData == null) {
  105.             setLocalFileDataData(tmp);
  106.         }
  107.     }

  108.     /**
  109.      * @param data   the array of bytes.
  110.      * @param offset the source location in the data array.
  111.      * @param length the number of bytes to use in the data array.
  112.      * @see ZipExtraField#parseFromLocalFileData(byte[], int, int)
  113.      */
  114.     @Override
  115.     public void parseFromLocalFileData(final byte[] data, final int offset, final int length) {
  116.         setLocalFileDataData(Arrays.copyOfRange(data, offset, offset + length));
  117.     }

  118.     /**
  119.      * Sets the extra field data in central directory.
  120.      *
  121.      * @param data the data to use
  122.      */
  123.     public void setCentralDirectoryData(final byte[] data) {
  124.         centralData = ZipUtil.copy(data);
  125.     }

  126.     /**
  127.      * Sets the header id.
  128.      *
  129.      * @param headerId the header id to use
  130.      */
  131.     public void setHeaderId(final ZipShort headerId) {
  132.         this.headerId = headerId;
  133.     }

  134.     /**
  135.      * Sets the extra field data in the local file data - without Header-ID or length specifier.
  136.      *
  137.      * @param data the field data to use
  138.      */
  139.     public void setLocalFileDataData(final byte[] data) {
  140.         localData = ZipUtil.copy(data);
  141.     }

  142. }