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 static org.apache.commons.compress.archivers.zip.ZipConstants.WORD; 021 022import java.io.Serializable; 023 024import org.apache.commons.compress.utils.ByteUtils; 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 /** Central File Header Signature */ 035 public static final ZipLong CFH_SIG = new ZipLong(0X02014B50L); 036 037 /** Local File Header Signature */ 038 public static final ZipLong LFH_SIG = new ZipLong(0X04034B50L); 039 040 /** 041 * Data Descriptor signature. 042 * 043 * <p>Actually, PKWARE uses this as marker for split/spanned 044 * archives and other archivers have started to use it as Data 045 * Descriptor signature (as well).</p> 046 * @since 1.1 047 */ 048 public static final ZipLong DD_SIG = new ZipLong(0X08074B50L); 049 050 /** 051 * Value stored in size and similar fields if ZIP64 extensions are 052 * used. 053 * @since 1.3 054 */ 055 static final ZipLong ZIP64_MAGIC = new ZipLong(ZipConstants.ZIP64_MAGIC); 056 057 /** 058 * Marks ZIP archives that were supposed to be split or spanned 059 * but only needed a single segment in then end (so are actually 060 * neither split nor spanned). 061 * 062 * <p>This is the "PK00" prefix found in some archives.</p> 063 * @since 1.5 064 */ 065 public static final ZipLong SINGLE_SEGMENT_SPLIT_MARKER = 066 new ZipLong(0X30304B50L); 067 068 /** 069 * Archive extra data record signature. 070 * @since 1.5 071 */ 072 public static final ZipLong AED_SIG = new ZipLong(0X08064B50L); 073 074 /** 075 * Get value as four bytes in big endian byte order. 076 * @param value the value to convert 077 * @return value as four bytes in big endian byte order 078 */ 079 public static byte[] getBytes(final long value) { 080 final byte[] result = new byte[WORD]; 081 putLong(value, result, 0); 082 return result; 083 } 084 085 /** 086 * Helper method to get the value as a Java long from a four-byte array 087 * @param bytes the array of bytes 088 * @return the corresponding Java long value 089 */ 090 public static long getValue(final byte[] bytes) { 091 return getValue(bytes, 0); 092 } 093 094 /** 095 * Helper method to get the value as a Java long from four bytes starting at given array offset 096 * @param bytes the array of bytes 097 * @param offset the offset to start 098 * @return the corresponding Java long value 099 */ 100 public static long getValue(final byte[] bytes, final int offset) { 101 return ByteUtils.fromLittleEndian(bytes, offset, 4); 102 } 103 104 /** 105 * put the value as four bytes in big endian byte order. 106 * @param value the Java long to convert to bytes 107 * @param buf the output buffer 108 * @param offset 109 * The offset within the output buffer of the first byte to be written. 110 * must be non-negative and no larger than {@code buf.length-4} 111 */ 112 113 public static void putLong(final long value, final byte[] buf, final int offset) { 114 ByteUtils.toLittleEndian(buf, value, offset, 4); 115 } 116 117 private final long value; 118 119 /** 120 * Create instance from bytes. 121 * @param bytes the bytes to store as a ZipLong 122 */ 123 public ZipLong (final byte[] bytes) { 124 this(bytes, 0); 125 } 126 127 /** 128 * Create instance from the four bytes starting at offset. 129 * @param bytes the bytes to store as a ZipLong 130 * @param offset the offset to start 131 */ 132 public ZipLong (final byte[] bytes, final int offset) { 133 value = ZipLong.getValue(bytes, offset); 134 } 135 136 /** 137 * create instance from a java int. 138 * @param value the int to store as a ZipLong 139 * @since 1.15 140 */ 141 public ZipLong(final int value) { 142 this.value = value; 143 } 144 145 /** 146 * Create instance from a number. 147 * @param value the long to store as a ZipLong 148 */ 149 public ZipLong(final long value) { 150 this.value = value; 151 } 152 153 @Override 154 public Object clone() { 155 try { 156 return super.clone(); 157 } catch (final CloneNotSupportedException cnfe) { 158 // impossible 159 throw new IllegalStateException(cnfe); //NOSONAR 160 } 161 } 162 163 /** 164 * Override to make two instances with same value equal. 165 * @param o an object to compare 166 * @return true if the objects are equal 167 */ 168 @Override 169 public boolean equals(final Object o) { 170 if (!(o instanceof ZipLong)) { 171 return false; 172 } 173 return value == ((ZipLong) o).getValue(); 174 } 175 176 /** 177 * Get value as four bytes in big endian byte order. 178 * @return value as four bytes in big endian order 179 */ 180 public byte[] getBytes() { 181 return ZipLong.getBytes(value); 182 } 183 184 /** 185 * Get value as a (signed) java int 186 * @return value as int 187 * @since 1.15 188 */ 189 public int getIntValue() { return (int)value;} 190 191 /** 192 * Get value as Java long. 193 * @return value as a long 194 */ 195 public long getValue() { 196 return value; 197 } 198 199 /** 200 * Override to make two instances with same value equal. 201 * @return the value stored in the ZipLong 202 */ 203 @Override 204 public int hashCode() { 205 return (int) value; 206 } 207 208 public void putLong(final byte[] buf, final int offset) { 209 putLong(value, buf, offset); 210 } 211 212 @Override 213 public String toString() { 214 return "ZipLong value: " + value; 215 } 216}