ZipShort.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.archivers.zip;

  18. import java.io.Serializable;

  19. import org.apache.commons.compress.utils.ByteUtils;

  20. /**
  21.  * Utility class that represents a two byte integer with conversion rules for the little-endian byte order of ZIP files.
  22.  *
  23.  * @Immutable
  24.  */
  25. public final class ZipShort implements Cloneable, Serializable {
  26.     /**
  27.      * ZipShort with a value of 0.
  28.      *
  29.      * @since 1.14
  30.      */
  31.     public static final ZipShort ZERO = new ZipShort(0);

  32.     private static final long serialVersionUID = 1L;

  33.     /**
  34.      * Gets value as two bytes in big-endian byte order.
  35.      *
  36.      * @param value the Java int to convert to bytes
  37.      * @return the converted int as a byte array in big-endian byte order
  38.      */
  39.     public static byte[] getBytes(final int value) {
  40.         final byte[] result = new byte[2];
  41.         putShort(value, result, 0);
  42.         return result;
  43.     }

  44.     /**
  45.      * Helper method to get the value as a Java int from a two-byte array
  46.      *
  47.      * @param bytes the array of bytes
  48.      * @return the corresponding Java int value
  49.      */
  50.     public static int getValue(final byte[] bytes) {
  51.         return getValue(bytes, 0);
  52.     }

  53.     /**
  54.      * Helper method to get the value as a Java int from two bytes starting at given array offset
  55.      *
  56.      * @param bytes  the array of bytes
  57.      * @param offset the offset to start
  58.      * @return the corresponding Java int value
  59.      */
  60.     public static int getValue(final byte[] bytes, final int offset) {
  61.         return (int) ByteUtils.fromLittleEndian(bytes, offset, 2);
  62.     }

  63.     /**
  64.      * put the value as two bytes in big-endian byte order.
  65.      *
  66.      * @param value  the Java int to convert to bytes
  67.      * @param buf    the output buffer
  68.      * @param offset The offset within the output buffer of the first byte to be written. must be non-negative and no larger than {@code buf.length-2}
  69.      */
  70.     public static void putShort(final int value, final byte[] buf, final int offset) {
  71.         ByteUtils.toLittleEndian(buf, value, offset, 2);
  72.     }

  73.     private final int value;

  74.     /**
  75.      * Constructs a new instance from bytes.
  76.      *
  77.      * @param bytes the bytes to store as a ZipShort
  78.      */
  79.     public ZipShort(final byte[] bytes) {
  80.         this(bytes, 0);
  81.     }

  82.     /**
  83.      * Constructs a new instance from the two bytes starting at offset.
  84.      *
  85.      * @param bytes  the bytes to store as a ZipShort
  86.      * @param offset the offset to start
  87.      */
  88.     public ZipShort(final byte[] bytes, final int offset) {
  89.         value = getValue(bytes, offset);
  90.     }

  91.     /**
  92.      * Constructs a new instance from a number.
  93.      *
  94.      * @param value the int to store as a ZipShort
  95.      */
  96.     public ZipShort(final int value) {
  97.         this.value = value;
  98.     }

  99.     @Override
  100.     public Object clone() {
  101.         try {
  102.             return super.clone();
  103.         } catch (final CloneNotSupportedException cnfe) {
  104.             // impossible
  105.             throw new UnsupportedOperationException(cnfe); // NOSONAR
  106.         }
  107.     }

  108.     /**
  109.      * Override to make two instances with same value equal.
  110.      *
  111.      * @param o an object to compare
  112.      * @return true if the objects are equal
  113.      */
  114.     @Override
  115.     public boolean equals(final Object o) {
  116.         if (!(o instanceof ZipShort)) {
  117.             return false;
  118.         }
  119.         return value == ((ZipShort) o).getValue();
  120.     }

  121.     /**
  122.      * Gets value as two bytes in big-endian byte order.
  123.      *
  124.      * @return the value as a two byte array in big-endian byte order
  125.      */
  126.     public byte[] getBytes() {
  127.         final byte[] result = new byte[2];
  128.         ByteUtils.toLittleEndian(result, value, 0, 2);
  129.         return result;
  130.     }

  131.     /**
  132.      * Gets value as Java int.
  133.      *
  134.      * @return value as a Java int
  135.      */
  136.     public int getValue() {
  137.         return value;
  138.     }

  139.     /**
  140.      * Override to make two instances with same value equal.
  141.      *
  142.      * @return the value stored in the ZipShort
  143.      */
  144.     @Override
  145.     public int hashCode() {
  146.         return value;
  147.     }

  148.     @Override
  149.     public String toString() {
  150.         return "ZipShort value: " + value;
  151.     }
  152. }