UnparseableExtraFieldData.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.  * Wrapper for extra field data that doesn't conform to the recommended format of header-tag + size + data.
  23.  *
  24.  * <p>
  25.  * The header-id is artificial (and not listed as a known ID in <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">APPNOTE.TXT</a>). Since it
  26.  * isn't used anywhere except to satisfy the ZipExtraField contract it shouldn't matter anyway.
  27.  * </p>
  28.  *
  29.  * @since 1.1
  30.  * @NotThreadSafe
  31.  */
  32. public final class UnparseableExtraFieldData implements ZipExtraField {
  33.     private static final ZipShort HEADER_ID = new ZipShort(0xACC1);

  34.     private byte[] localFileData;
  35.     private byte[] centralDirectoryData;

  36.     /**
  37.      * The actual data to put into central directory.
  38.      *
  39.      * @return The CentralDirectoryData value
  40.      */
  41.     @Override
  42.     public byte[] getCentralDirectoryData() {
  43.         return centralDirectoryData == null ? getLocalFileDataData() : ZipUtil.copy(centralDirectoryData);
  44.     }

  45.     /**
  46.      * Length of the complete extra field in the central directory.
  47.      *
  48.      * @return The CentralDirectoryLength value
  49.      */
  50.     @Override
  51.     public ZipShort getCentralDirectoryLength() {
  52.         return centralDirectoryData == null ? getLocalFileDataLength() : new ZipShort(centralDirectoryData.length);
  53.     }

  54.     /**
  55.      * The Header-ID.
  56.      *
  57.      * @return a completely arbitrary value that should be ignored.
  58.      */
  59.     @Override
  60.     public ZipShort getHeaderId() {
  61.         return HEADER_ID;
  62.     }

  63.     /**
  64.      * The actual data to put into local file data.
  65.      *
  66.      * @return The LocalFileDataData value
  67.      */
  68.     @Override
  69.     public byte[] getLocalFileDataData() {
  70.         return ZipUtil.copy(localFileData);
  71.     }

  72.     /**
  73.      * Length of the complete extra field in the local file data.
  74.      *
  75.      * @return The LocalFileDataLength value
  76.      */
  77.     @Override
  78.     public ZipShort getLocalFileDataLength() {
  79.         return new ZipShort(localFileData == null ? 0 : localFileData.length);
  80.     }

  81.     /**
  82.      * Populate data from this array as if it was in central directory data.
  83.      *
  84.      * @param buffer the buffer to read data from
  85.      * @param offset offset into buffer to read data
  86.      * @param length the length of data
  87.      */
  88.     @Override
  89.     public void parseFromCentralDirectoryData(final byte[] buffer, final int offset, final int length) {
  90.         centralDirectoryData = Arrays.copyOfRange(buffer, offset, offset + length);
  91.         if (localFileData == null) {
  92.             parseFromLocalFileData(buffer, offset, length);
  93.         }
  94.     }

  95.     /**
  96.      * Populate data from this array as if it was in local file data.
  97.      *
  98.      * @param buffer the buffer to read data from
  99.      * @param offset offset into buffer to read data
  100.      * @param length the length of data
  101.      */
  102.     @Override
  103.     public void parseFromLocalFileData(final byte[] buffer, final int offset, final int length) {
  104.         localFileData = Arrays.copyOfRange(buffer, offset, offset + length);
  105.     }

  106. }