| 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.tools.zip; |
| 20 |
|
| 21 |
/** |
| 22 |
* Wrapper for extra field data that doesn't conform to the recommended format of header-tag + size + data. |
| 23 |
* |
| 24 |
* <p>The header-id is artificial (and not listed as a know ID in |
| 25 |
* {@link http://www.pkware.com/documents/casestudies/APPNOTE.TXT |
| 26 |
* APPNOTE.TXT}. Since it isn't used anywhere except to satisfy the |
| 27 |
* ZipExtraField contract it shouldn't matter anyway.</p> |
| 28 |
* @since Ant 1.8.1 |
| 29 |
*/ |
| 30 |
public final class UnparseableExtraFieldData |
| 31 |
implements CentralDirectoryParsingZipExtraField { |
| 32 |
|
| 33 |
private static final ZipShort HEADER_ID = new ZipShort(0xACC1); |
| 34 |
|
| 35 |
private byte[] localFileData; |
| 36 |
private byte[] centralDirectoryData; |
| 37 |
|
| 38 |
/** |
| 39 |
* The Header-ID. |
| 40 |
* |
| 41 |
* @return a completely arbitrary value that should be ignored. |
| 42 |
*/ |
| 43 |
public ZipShort getHeaderId() { |
| 44 |
return HEADER_ID; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Length of the complete extra field in the local file data. |
| 49 |
* |
| 50 |
* @return The LocalFileDataLength value |
| 51 |
*/ |
| 52 |
public ZipShort getLocalFileDataLength() { |
| 53 |
return new ZipShort(localFileData == null ? 0 : localFileData.length); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Length of the complete extra field in the central directory. |
| 58 |
* |
| 59 |
* @return The CentralDirectoryLength value |
| 60 |
*/ |
| 61 |
public ZipShort getCentralDirectoryLength() { |
| 62 |
return centralDirectoryData == null |
| 63 |
? getLocalFileDataLength() |
| 64 |
: new ZipShort(centralDirectoryData.length); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* The actual data to put into local file data. |
| 69 |
* |
| 70 |
* @return The LocalFileDataData value |
| 71 |
*/ |
| 72 |
public byte[] getLocalFileDataData() { |
| 73 |
return ZipUtil.copy(localFileData); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* The actual data to put into central directory. |
| 78 |
* |
| 79 |
* @return The CentralDirectoryData value |
| 80 |
*/ |
| 81 |
public byte[] getCentralDirectoryData() { |
| 82 |
return centralDirectoryData == null |
| 83 |
? getLocalFileDataData() : ZipUtil.copy(centralDirectoryData); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Populate data from this array as if it was in local file data. |
| 88 |
* |
| 89 |
* @param buffer the buffer to read data from |
| 90 |
* @param offset offset into buffer to read data |
| 91 |
* @param length the length of data |
| 92 |
*/ |
| 93 |
public void parseFromLocalFileData(byte[] buffer, int offset, int length) { |
| 94 |
localFileData = new byte[length]; |
| 95 |
System.arraycopy(buffer, offset, localFileData, 0, length); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Populate data from this array as if it was in central directory data. |
| 100 |
* |
| 101 |
* @param buffer the buffer to read data from |
| 102 |
* @param offset offset into buffer to read data |
| 103 |
* @param length the length of data |
| 104 |
* @exception ZipException on error |
| 105 |
*/ |
| 106 |
public void parseFromCentralDirectoryData(byte[] buffer, int offset, |
| 107 |
int length) { |
| 108 |
centralDirectoryData = new byte[length]; |
| 109 |
System.arraycopy(buffer, offset, centralDirectoryData, 0, length); |
| 110 |
if (localFileData == null) { |
| 111 |
parseFromLocalFileData(buffer, offset, length); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
} |