All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.conqat.lib.commons.collections.ByteArrayWrapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) CQSE GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.conqat.lib.commons.collections;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;

import org.conqat.lib.commons.string.StringUtils;
import org.conqat.lib.commons.test.IndexValueClass;

/**
 * A wrapper class around a byte array that supports a clean implementation of {@link #hashCode()}
 * and {@link #equals(Object)}, so byte arrays can be used, e.g., in a {@link java.util.HashMap}.
 * 

* The class is immutable. * * @implNote Custom (de)serialization is provided to make this efficient to use in storage or RMI * scenarios. */ @IndexValueClass(containedInBackup = true) public class ByteArrayWrapper implements Serializable, Comparable { /** Version used for serialization. */ private static final long serialVersionUID = 1; /** The wrapped array. */ protected byte[] array; /** Constructor. */ public ByteArrayWrapper(byte[] array) { this.array = array.clone(); } /** Returns a copy of the internal byte representation. */ public byte[] getBytes() { return array.clone(); } /** Returns the length of this array in bytes. */ public int getLength() { return array.length; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof ByteArrayWrapper)) { return false; } ByteArrayWrapper that = (ByteArrayWrapper) o; if (!that.canEqual(this)) { return false; } return Objects.deepEquals(array, that.array); } /** * @return Whether {@code other} can be equal to this object. * @apiNote This is called during {@link #equals(Object)} on the other object, to make sure * that the equality is symmetric. * @see How to Write an Equality * Method in Java */ protected boolean canEqual(Object other) { return other instanceof ByteArrayWrapper; } @Override public int hashCode() { return Arrays.hashCode(array); } @Override public String toString() { return StringUtils.encodeAsHex(array); } @Override public int compareTo(ByteArrayWrapper other) { int lengthDelta = array.length - other.array.length; if (lengthDelta != 0) { return lengthDelta; } for (int i = 0; i < array.length; ++i) { int delta = array[i] - other.array[i]; if (delta != 0) { return delta; } } return 0; } /** Custom serialization. */ private void writeObject(ObjectOutputStream out) throws IOException { out.writeInt(array.length); out.write(array); } /** Custom deserialization. */ private void readObject(ObjectInputStream in) throws IOException { int size = in.readInt(); array = new byte[size]; int pos = 0; while (pos < size) { pos += in.read(array, pos, size - pos); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy