/** * Helper method to get the value as a java int from a two-byte array * @param bytes the array of bytes * @return the corresponding java int value */ public static int getValue(byte[] bytes) { return getValue(bytes, 0); }
/** {@inheritDoc} */ public ZipShort getCentralDirectoryLength() { return new ZipShort((size != null ? DWORD : 0) + (compressedSize != null ? DWORD : 0) + (relativeHeaderOffset != null ? DWORD : 0) + (diskStart != null ? WORD : 0)); }
/** * Get value as two bytes in big endian byte order. * @return the value as a a two byte array in big endian byte order * @since 1.1 */ public byte[] getBytes() { byte[] result = new byte[2]; putShort(value, result, 0); return result; }
sum += element.getLocalFileDataLength().getValue(); int start = 0; for (int i = 0; i < regularExtraFieldCount; i++) { System.arraycopy(data[i].getHeaderId().getBytes(), 0, result, start, 2); System.arraycopy(data[i].getLocalFileDataLength().getBytes(), 0, result, start + 2, 2); byte[] local = data[i].getLocalFileDataData();
/** * Looks up an extra field by its header id. * * @param type ZipShort * @return null if no such field exists. */ public ZipExtraField getExtraField(final ZipShort type) { if (extraFields != null) { for (ZipExtraField extraField : extraFields) { if (type.equals(extraField.getHeaderId())) { return extraField; } } } return null; }
/** * The actual data to put into local file data - without Header-ID * or length specifier. * @return get the data * @since 1.1 */ public byte[] getLocalFileDataData() { // CRC will be added later byte[] data = new byte[getLocalFileDataLength().getValue() - WORD]; System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2); byte[] linkArray = getLinkedFile().getBytes(); // Uses default charset - see class Javadoc // CheckStyle:MagicNumber OFF System.arraycopy(ZipLong.getBytes(linkArray.length), 0, data, 2, WORD); System.arraycopy(ZipShort.getBytes(getUserId()), 0, data, 6, 2); System.arraycopy(ZipShort.getBytes(getGroupId()), 0, data, 8, 2); System.arraycopy(linkArray, 0, data, 10, linkArray.length); // CheckStyle:MagicNumber ON crc.reset(); crc.update(data); long checksum = crc.getValue(); byte[] result = new byte[data.length + WORD]; System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD); System.arraycopy(data, 0, result, WORD, data.length); return result; }
writeOut(ZipShort.getBytes(INITIAL_VERSION));
/** * Remove an extra field. * * @param type the type of extra field to remove * @since 1.1 */ public void removeExtraField(final ZipShort type) { if (extraFields == null) { throw new NoSuchElementException(); } List<ZipExtraField> newResult = new ArrayList<>(); for (ZipExtraField extraField : extraFields) { if (!type.equals(extraField.getHeaderId())) { newResult.add(extraField); } } if (extraFields.length == newResult.size()) { throw new NoSuchElementException(); } extraFields = newResult.toArray(new ZipExtraField[newResult.size()]); setExtra(); }
/** * Create instance from the two bytes starting at offset. * @param bytes the bytes to store as a ZipShort * @param offset the offset to start * @since 1.1 */ public ZipShort(byte[] bytes, int offset) { value = ZipShort.getValue(bytes, offset); }
/** * Length of the complete extra field in the local file data. * * @return The LocalFileDataLength value */ public ZipShort getLocalFileDataLength() { return new ZipShort(localFileData == null ? 0 : localFileData.length); }
/** * Encodes the set bits in a form suitable for ZIP archives. * * @param buf the output buffer * @param offset * The offset within the output buffer of the first byte to be written. * must be non-negative and no larger than <tt>buf.length-2</tt> */ public void encode(byte[] buf, int offset) { ZipShort.putShort((dataDescriptorFlag ? DATA_DESCRIPTOR_FLAG : 0) | (languageEncodingFlag ? UFT8_NAMES_FLAG : 0) | (encryptionFlag ? ENCRYPTION_FLAG : 0) | (strongEncryptionFlag ? STRONG_ENCRYPTION_FLAG : 0), buf, offset); }
/** * Override to make two instances with same value equal. * @param o an object to compare * @return true if the objects are equal * @since 1.1 */ @Override public boolean equals(Object o) { return o instanceof ZipShort && value == ((ZipShort) o).getValue(); }
/** * Get the length of the local data. * @return the length of the local data */ public ZipShort getLocalFileDataLength() { return new ZipShort(localData.length); }
/** {@inheritDoc} */ public ZipShort getLocalFileDataLength() { return new ZipShort(size != null ? 2 * DWORD : 0); }