View Javadoc
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  
21  import java.util.Arrays;
22  
23  /**
24   * Simple placeholder for all those extra fields we don't want to deal with.
25   *
26   * <p>
27   * Assumes local file data and central directory entries are identical - unless told the opposite.
28   * </p>
29   *
30   * @NotThreadSafe
31   */
32  public class UnrecognizedExtraField implements ZipExtraField {
33  
34      /**
35       * The Header-ID.
36       */
37      private ZipShort headerId;
38  
39      /**
40       * Extra field data in local file data - without Header-ID or length specifier.
41       */
42      private byte[] localData;
43  
44      /**
45       * Extra field data in central directory - without Header-ID or length specifier.
46       */
47      private byte[] centralData;
48  
49      /**
50       * Gets the central data.
51       *
52       * @return the central data if present, else return the local file data
53       */
54      @Override
55      public byte[] getCentralDirectoryData() {
56          if (centralData != null) {
57              return ZipUtil.copy(centralData);
58          }
59          return getLocalFileDataData();
60      }
61  
62      /**
63       * Gets the central data length. If there is no central data, get the local file data length.
64       *
65       * @return the central data length
66       */
67      @Override
68      public ZipShort getCentralDirectoryLength() {
69          if (centralData != null) {
70              return new ZipShort(centralData.length);
71          }
72          return getLocalFileDataLength();
73      }
74  
75      /**
76       * Gets the header id.
77       *
78       * @return the header id
79       */
80      @Override
81      public ZipShort getHeaderId() {
82          return headerId;
83      }
84  
85      /**
86       * Gets the local data.
87       *
88       * @return the local data
89       */
90      @Override
91      public byte[] getLocalFileDataData() {
92          return ZipUtil.copy(localData);
93      }
94  
95      /**
96       * Gets the length of the local data.
97       *
98       * @return the length of the local data
99       */
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 }