blob: 9ddc7a5c9d72854375ab122f219cd481d125fc68 [file] [log] [blame]
Torsten Curdtca165392008-07-10 10:17:44 +00001/*
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 */
19package org.apache.commons.compress.archivers.zip;
20
21/**
22 * Simple placeholder for all those extra fields we don't want to deal with. <p>
23 *
24 * Assumes local file data and central directory entries are identical - unless
25 * told the opposite.</p>
Torsten Curdtca165392008-07-10 10:17:44 +000026 */
27public class UnrecognizedExtraField
28 implements ZipExtraField
29{
30 /**
31 * Extra field data in central directory - without Header-ID or length
32 * specifier.
33 *
34 * @since 1.1
35 */
36 private byte[] m_centralData;
37
38 /**
39 * The Header-ID.
40 *
41 * @since 1.1
42 */
43 private ZipShort m_headerID;
44
45 /**
46 * Extra field data in local file data - without Header-ID or length
47 * specifier.
48 *
49 * @since 1.1
50 */
51 private byte[] m_localData;
52
53 /**
54 * Set the central directory data
55 *
56 * @param centralData the central directory data
57 */
58 public void setCentralDirectoryData( final byte[] centralData )
59 {
Stefan Bodewig5e5804c2009-02-05 12:45:23 +000060 m_centralData = copy(centralData);
Torsten Curdtca165392008-07-10 10:17:44 +000061 }
62
63 /**
64 * Set the header ID.
65 *
66 * @param headerID the header ID
67 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000068 public void setHeaderId( final ZipShort headerID )
Torsten Curdtca165392008-07-10 10:17:44 +000069 {
70 m_headerID = headerID;
71 }
72
73 /**
74 * Set the local file data.
75 *
76 * @param localData the local file data
77 */
78 public void setLocalFileDataData( final byte[] localData )
79 {
Stefan Bodewig5e5804c2009-02-05 12:45:23 +000080 m_localData = copy(localData);
Torsten Curdtca165392008-07-10 10:17:44 +000081 }
82
83 /**
84 * Get the central directory data.
85 *
86 * @return the central directory data.
87 */
88 public byte[] getCentralDirectoryData()
89 {
90 if( m_centralData != null )
91 {
Stefan Bodewig5e5804c2009-02-05 12:45:23 +000092 return copy(m_centralData);
Torsten Curdtca165392008-07-10 10:17:44 +000093 }
94 return getLocalFileDataData();
95 }
96
97 /**
98 * Get the length of the central directory in bytes.
99 *
100 * @return the length of the central directory in bytes.
101 */
102 public ZipShort getCentralDirectoryLength()
103 {
104 if( m_centralData != null )
105 {
106 return new ZipShort( m_centralData.length );
107 }
108 return getLocalFileDataLength();
109 }
110
111 /**
112 * Get the HeaderID.
113 *
114 * @return the HeaderID
115 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000116 public ZipShort getHeaderId()
Torsten Curdtca165392008-07-10 10:17:44 +0000117 {
118 return m_headerID;
119 }
120
121 /**
122 * Get the local file data.
123 *
124 * @return the local file data
125 */
126 public byte[] getLocalFileDataData()
127 {
Stefan Bodewig5e5804c2009-02-05 12:45:23 +0000128 return copy(m_localData);
Torsten Curdtca165392008-07-10 10:17:44 +0000129 }
130
131 /**
132 * Get the length of local file data in bytes.
133 *
134 * @return the length of local file data in bytes
135 */
136 public ZipShort getLocalFileDataLength()
137 {
138 return new ZipShort( m_localData.length );
139 }
140
141 /**
142 * Parse LocalFiledata out of supplied buffer.
143 *
144 * @param buffer the buffer to use
145 * @param offset the offset into buffer
146 * @param length then length of data
147 */
148 public void parseFromLocalFileData( final byte[] buffer,
149 final int offset,
150 final int length )
151 {
152 final byte[] fileData = new byte[ length ];
153 System.arraycopy( buffer, offset, fileData, 0, length );
154 setLocalFileDataData( fileData );
155 }
Stefan Bodewig5e5804c2009-02-05 12:45:23 +0000156
157 private static byte[] copy(byte[] from) {
158 if (from != null) {
159 byte[] to = new byte[from.length];
160 System.arraycopy(from, 0, to, 0, to.length);
161 return to;
162 }
163 return null;
164 }
Torsten Curdtca165392008-07-10 10:17:44 +0000165}