Debian Codegraph @

src/main/java/org/apache/tools/zip/UnrecognizedExtraField.java

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 package org.apache.tools.zip;
20
21 /**
22 * Simple placeholder for all those extra fields we don't want to deal
23 * with.
24 *
25 * <p>Assumes local file data and central directory entries are
26 * identical - unless told the opposite.</p>
27 *
28 */
29 public class UnrecognizedExtraField
30 implements CentralDirectoryParsingZipExtraField {
31
32 /**
33 * The Header-ID.
34 *
35 * @since 1.1
36 */
37 private ZipShort headerId;
38
39 /**
40 * Set the header id.
41 * @param headerId the header id to use
42 */
43 public void setHeaderId(ZipShort headerId) {
44 this.headerId = headerId;
45 }
46
47 /**
48 * Get the header id.
49 * @return the header id
50 */
51 public ZipShort getHeaderId() {
52 return headerId;
53 }
54
55 /**
56 * Extra field data in local file data - without
57 * Header-ID or length specifier.
58 *
59 * @since 1.1
60 */
61 private byte[] localData;
62
63 /**
64 * Set the extra field data in the local file data -
65 * without Header-ID or length specifier.
66 * @param data the field data to use
67 */
68 public void setLocalFileDataData(byte[] data) {
69 localData = ZipUtil.copy(data);
70 }
71
72 /**
73 * Get the length of the local data.
74 * @return the length of the local data
75 */
76 public ZipShort getLocalFileDataLength() {
77 return new ZipShort(localData.length);
78 }
79
80 /**
81 * Get the local data.
82 * @return the local data
83 */
84 public byte[] getLocalFileDataData() {
85 return ZipUtil.copy(localData);
86 }
87
88 /**
89 * Extra field data in central directory - without
90 * Header-ID or length specifier.
91 *
92 * @since 1.1
93 */
94 private byte[] centralData;
95
96 /**
97 * Set the extra field data in central directory.
98 * @param data the data to use
99 */
100 public void setCentralDirectoryData(byte[] data) {
101 centralData = ZipUtil.copy(data);
102 }
103
104 /**
105 * Get the central data length.
106 * If there is no central data, get the local file data length.
107 * @return the central data length
108 */
109 public ZipShort getCentralDirectoryLength() {
110 if (centralData != null) {
111 return new ZipShort(centralData.length);
112 }
113 return getLocalFileDataLength();
114 }
115
116 /**
117 * Get the central data.
118 * @return the central data if present, else return the local file data
119 */
120 public byte[] getCentralDirectoryData() {
121 if (centralData != null) {
122 return ZipUtil.copy(centralData);
123 }
124 return getLocalFileDataData();
125 }
126
127 /**
128 * @param data the array of bytes.
129 * @param offset the source location in the data array.
130 * @param length the number of bytes to use in the data array.
131 * @see ZipExtraField#parseFromLocalFileData(byte[], int, int)
132 */
133 public void parseFromLocalFileData(byte[] data, int offset, int length) {
134 byte[] tmp = new byte[length];
135 System.arraycopy(data, offset, tmp, 0, length);
136 setLocalFileDataData(tmp);
137 }
138
139 /**
140 * @param data the array of bytes.
141 * @param offset the source location in the data array.
142 * @param length the number of bytes to use in the data array.
143 */
144 public void parseFromCentralDirectoryData(byte[] data, int offset,
145 int length) {
146 byte[] tmp = new byte[length];
147 System.arraycopy(data, offset, tmp, 0, length);
148 setCentralDirectoryData(tmp);
149 if (localData == null) {
150 setLocalFileDataData(tmp);
151 }
152 }
153
154 }