    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 */
    017package org.apache.commons.compress.archivers.zip;
    018
    019import java.io.Serializable;
    020
    021import org.apache.commons.compress.utils.ByteUtils;
    022
    023/**
    024 * Utility class that represents a two byte integer with conversion rules for the little-endian byte order of ZIP files.
    025 *
    026 * @Immutable
    027 */
    028public final class ZipShort implements Cloneable, Serializable {
    029    /**
    030     * ZipShort with a value of 0.
    031     *
    032     * @since 1.14
    033     */
    034    public static final ZipShort ZERO = new ZipShort(0);
    035
    036    private static final long serialVersionUID = 1L;
    037
    038    /**
    039     * Gets value as two bytes in big-endian byte order.
    040     *
    041     * @param value the Java int to convert to bytes
    042     * @return the converted int as a byte array in big-endian byte order
    043     */
    044    public static byte[] getBytes(final int value) {
    045        final byte[] result = new byte[2];
    046        putShort(value, result, 0);
    047        return result;
    048    }
    049
    050    /**
    051     * Helper method to get the value as a Java int from a two-byte array
    052     *
    053     * @param bytes the array of bytes
    054     * @return the corresponding Java int value
    055     */
    056    public static int getValue(final byte[] bytes) {
    057        return getValue(bytes, 0);
    058    }
    059
    060    /**
    061     * Helper method to get the value as a Java int from two bytes starting at given array offset
    062     *
    063     * @param bytes  the array of bytes
    064     * @param offset the offset to start
    065     * @return the corresponding Java int value
    066     */
    067    public static int getValue(final byte[] bytes, final int offset) {
    068        return (int) ByteUtils.fromLittleEndian(bytes, offset, 2);
    069    }
    070
    071    /**
    072     * put the value as two bytes in big-endian byte order.
    073     *
    074     * @param value  the Java int to convert to bytes
    075     * @param buf    the output buffer
    076     * @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}
    077     */
    078    public static void putShort(final int value, final byte[] buf, final int offset) {
    079        ByteUtils.toLittleEndian(buf, value, offset, 2);
    080    }
    081
    082    private final int value;
    083
    084    /**
    085     * Constructs a new instance from bytes.
    086     *
    087     * @param bytes the bytes to store as a ZipShort
    088     */
    089    public ZipShort(final byte[] bytes) {
    090        this(bytes, 0);
    091    }
    092
    093    /**
    094     * Constructs a new instance from the two bytes starting at offset.
    095     *
    096     * @param bytes  the bytes to store as a ZipShort
    097     * @param offset the offset to start
    098     */
    099    public ZipShort(final byte[] bytes, final int offset) {
    100        value = ZipShort.getValue(bytes, offset);
    101    }
    102
    103    /**
    104     * Constructs a new instance from a number.
    105     *
    106     * @param value the int to store as a ZipShort
    107     */
    108    public ZipShort(final int value) {
    109        this.value = value;
    110    }
    111
    112    @Override
    113    public Object clone() {
    114        try {
    115            return super.clone();
    116        } catch (final CloneNotSupportedException cnfe) {
    117            // impossible
    118            throw new UnsupportedOperationException(cnfe); // NOSONAR
    119        }
    120    }
    121
    122    /**
    123     * Override to make two instances with same value equal.
    124     *
    125     * @param o an object to compare
    126     * @return true if the objects are equal
    127     */
    128    @Override
    129    public boolean equals(final Object o) {
    130        if (!(o instanceof ZipShort)) {
    131            return false;
    132        }
    133        return value == ((ZipShort) o).getValue();
    134    }
    135
    136    /**
    137     * Gets value as two bytes in big-endian byte order.
    138     *
    139     * @return the value as a two byte array in big-endian byte order
    140     */
    141    public byte[] getBytes() {
    142        final byte[] result = new byte[2];
    143        ByteUtils.toLittleEndian(result, value, 0, 2);
    144        return result;
    145    }
    146
    147    /**
    148     * Gets value as Java int.
    149     *
    150     * @return value as a Java int
    151     */
    152    public int getValue() {
    153        return value;
    154    }
    155
    156    /**
    157     * Override to make two instances with same value equal.
    158     *
    159     * @return the value stored in the ZipShort
    160     */
    161    @Override
    162    public int hashCode() {
    163        return value;
    164    }
    165
    166    @Override
    167    public String toString() {
    168        return "ZipShort value: " + value;
    169    }
    170}