001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018package org.apache.commons.compress.archivers.zip; 019 020import java.io.Serializable; 021 022import org.apache.commons.compress.utils.ByteUtils; 023 024import static org.apache.commons.compress.archivers.zip.ZipConstants.WORD; 025 026/** 027 * Utility class that represents a four byte integer with conversion 028 * rules for the little endian byte order of ZIP files. 029 * @Immutable 030 */ 031public final class ZipLong implements Cloneable, Serializable { 032 private static final long serialVersionUID = 1L; 033 034 private final long value; 035 036 /** Central File Header Signature */ 037 public static final ZipLong CFH_SIG = new ZipLong(0X02014B50L); 038 039 /** Local File Header Signature */ 040 public static final ZipLong LFH_SIG = new ZipLong(0X04034B50L); 041 042 /** 043 * Data Descriptor signature. 044 * 045 * <p>Actually, PKWARE uses this as marker for split/spanned 046 * archives and other archivers have started to use it as Data 047 * Descriptor signature (as well).</p> 048 * @since 1.1 049 */ 050 public static final ZipLong DD_SIG = new ZipLong(0X08074B50L); 051 052 /** 053 * Value stored in size and similar fields if ZIP64 extensions are 054 * used. 055 * @since 1.3 056 */ 057 static final ZipLong ZIP64_MAGIC = new ZipLong(ZipConstants.ZIP64_MAGIC); 058 059 /** 060 * Marks ZIP archives that were supposed to be split or spanned 061 * but only needed a single segment in then end (so are actually 062 * neither split nor spanned). 063 * 064 * <p>This is the "PK00" prefix found in some archives.</p> 065 * @since 1.5 066 */ 067 public static final ZipLong SINGLE_SEGMENT_SPLIT_MARKER = 068 new ZipLong(0X30304B50L); 069 070 /** 071 * Archive extra data record signature. 072 * @since 1.5 073 */ 074 public static final ZipLong AED_SIG = new ZipLong(0X08064B50L); 075 076 /** 077 * Create instance from a number. 078 * @param value the long to store as a ZipLong 079 */ 080 public ZipLong(final long value) { 081 this.value = value; 082 } 083 084 /** 085 * Create instance from bytes. 086 * @param bytes the bytes to store as a ZipLong 087 */ 088 public ZipLong (final byte[] bytes) { 089 this(bytes, 0); 090 } 091 092 /** 093 * Create instance from the four bytes starting at offset. 094 * @param bytes the bytes to store as a ZipLong 095 * @param offset the offset to start 096 */ 097 public ZipLong (final byte[] bytes, final int offset) { 098 value = ZipLong.getValue(bytes, offset); 099 } 100 101 /** 102 * Get value as four bytes in big endian byte order. 103 * @return value as four bytes in big endian order 104 */ 105 public byte[] getBytes() { 106 return ZipLong.getBytes(value); 107 } 108 109 /** 110 * Get value as Java long. 111 * @return value as a long 112 */ 113 public long getValue() { 114 return value; 115 } 116 117 /** 118 * Get value as four bytes in big endian byte order. 119 * @param value the value to convert 120 * @return value as four bytes in big endian byte order 121 */ 122 public static byte[] getBytes(final long value) { 123 final byte[] result = new byte[WORD]; 124 putLong(value, result, 0); 125 return result; 126 } 127 128 /** 129 * put the value as four bytes in big endian byte order. 130 * @param value the Java long to convert to bytes 131 * @param buf the output buffer 132 * @param offset 133 * The offset within the output buffer of the first byte to be written. 134 * must be non-negative and no larger than <tt>buf.length-4</tt> 135 */ 136 137 public static void putLong(final long value, final byte[] buf, int offset) { 138 ByteUtils.toLittleEndian(buf, value, offset, 4); 139 } 140 141 public void putLong(final byte[] buf, final int offset) { 142 putLong(value, buf, offset); 143 } 144 145 /** 146 * Helper method to get the value as a Java long from four bytes starting at given array offset 147 * @param bytes the array of bytes 148 * @param offset the offset to start 149 * @return the corresponding Java long value 150 */ 151 public static long getValue(final byte[] bytes, final int offset) { 152 return ByteUtils.fromLittleEndian(bytes, offset, 4); 153 } 154 155 /** 156 * Helper method to get the value as a Java long from a four-byte array 157 * @param bytes the array of bytes 158 * @return the corresponding Java long value 159 */ 160 public static long getValue(final byte[] bytes) { 161 return getValue(bytes, 0); 162 } 163 164 /** 165 * Override to make two instances with same value equal. 166 * @param o an object to compare 167 * @return true if the objects are equal 168 */ 169 @Override 170 public boolean equals(final Object o) { 171 if (o == null || !(o instanceof ZipLong)) { 172 return false; 173 } 174 return value == ((ZipLong) o).getValue(); 175 } 176 177 /** 178 * Override to make two instances with same value equal. 179 * @return the value stored in the ZipLong 180 */ 181 @Override 182 public int hashCode() { 183 return (int) value; 184 } 185 186 @Override 187 public Object clone() { 188 try { 189 return super.clone(); 190 } catch (final CloneNotSupportedException cnfe) { 191 // impossible 192 throw new RuntimeException(cnfe); //NOSONAR 193 } 194 } 195 196 @Override 197 public String toString() { 198 return "ZipLong value: " + value; 199 } 200}