|
| 1 | +/* |
| 2 | + * This file is part of RskJ |
| 3 | + * Copyright (C) 2024 RSK Labs Ltd. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package co.rsk.core.types.bytes; |
| 20 | + |
| 21 | +import org.ethereum.util.ByteUtil; |
| 22 | +import org.ethereum.util.FastByteComparisons; |
| 23 | + |
| 24 | +import javax.annotation.Nonnull; |
| 25 | +import javax.annotation.Nullable; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Objects; |
| 28 | + |
| 29 | +/** |
| 30 | + * A {@link Bytes} is a readable sequence of <code>byte</code> values. This |
| 31 | + * interface provides uniform, read-only access to many different kinds of |
| 32 | + * <code>byte</code> sequences. |
| 33 | + * |
| 34 | + * <p> This interface does not refine the general contracts of the {@link |
| 35 | + * java.lang.Object#equals(java.lang.Object) equals} and {@link |
| 36 | + * java.lang.Object#hashCode() hashCode} methods. The result of comparing two |
| 37 | + * objects that implement <tt>Bytes</tt> is therefore, in general, |
| 38 | + * undefined. Each object may be implemented by a different class, and there |
| 39 | + * is no guarantee that each class will be capable of testing its instances |
| 40 | + * for equality with those of the other. It is therefore inappropriate to use |
| 41 | + * arbitrary <tt>Bytes</tt> instances as elements in a set or as keys in |
| 42 | + * a map. </p> |
| 43 | + */ |
| 44 | +public interface Bytes extends BytesSlice { |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns an instance of the {@link Bytes} interface, which represents {@code unsafeByteArray}. |
| 48 | + * |
| 49 | + * @return the instance of the {@link Bytes} interface that wraps a provided byte array. |
| 50 | + */ |
| 51 | + static Bytes of(@Nullable byte[] unsafeByteArray) { |
| 52 | + if (unsafeByteArray == null) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + return new BytesImpl(unsafeByteArray); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * A helper method for printing "nullable" byte arrays. |
| 60 | + * |
| 61 | + * @return {@code valueIfNull}, if {@code byteArray} is {@code null}. Otherwise - {@code Bytes.of(byteArray).toPrintableString()}. |
| 62 | + */ |
| 63 | + static String toPrintableString(@Nullable byte[] byteArray, @Nullable String valueIfNull) { |
| 64 | + if (byteArray == null) { |
| 65 | + return valueIfNull; |
| 66 | + } |
| 67 | + return of(byteArray).toPrintableString(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * A helper method for printing "nullable" byte arrays. |
| 72 | + * |
| 73 | + * @return {@code "<null>"}, if {@code byteArray} is {@code null}. Otherwise - {@code Bytes.of(byteArray).toPrintableString()}. |
| 74 | + */ |
| 75 | + @Nonnull |
| 76 | + static String toPrintableString(@Nullable byte[] byteArray) { |
| 77 | + return toPrintableString(byteArray, "<null>"); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * A helper method for extracting "unsafe" underlying byte array from the {@code bytes} instance. |
| 82 | + * |
| 83 | + * @return {@code null}, if {@code bytes} is {@code null}. Otherwise - {@code bytes.asUnsafeByteArray()}. |
| 84 | + */ |
| 85 | + @Nullable |
| 86 | + static byte[] asUnsafeByteArray(@Nullable Bytes bytes) { |
| 87 | + if (bytes == null) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + return bytes.asUnsafeByteArray(); |
| 91 | + } |
| 92 | + |
| 93 | + static boolean equalBytes(Bytes b1, Bytes b2) { |
| 94 | + if (b1 == null && b2 == null) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + if (b1 == null || b2 == null) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + return FastByteComparisons.equalBytes(b1.asUnsafeByteArray(), b2.asUnsafeByteArray()); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns an underlying byte array, which is backing this instance. Any mutations that are being done with the bytes |
| 105 | + * of returned array will have direct impact on the byte array that is being wrapped by this instance. |
| 106 | + * |
| 107 | + * @return the wrapped by this instance byte array. |
| 108 | + */ |
| 109 | + byte[] asUnsafeByteArray(); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * The {@link BytesImpl} class represents a read-only sequence of <code>byte</code> values. |
| 114 | + * <p> |
| 115 | + * Instances of the {@link BytesImpl} class are constant; their values cannot be changed after they |
| 116 | + * are created via the methods that this class provides. But a {@code byteArray} instance itself provided in the constructor |
| 117 | + * is mutable and can be modified outside the class. It's generally a bad idea to mutate a byte array that's being wrapped |
| 118 | + * by an instance of this class, as the idea is to make a byte sequence immutable, which is not the case with the Java |
| 119 | + * built-in {@code byte[]} type. |
| 120 | + * <p> |
| 121 | + * Because {@link BytesImpl} objects are immutable they can be safely used by multiple Java threads, if the wrapped array |
| 122 | + * is not being referenced and modified outside. |
| 123 | + */ |
| 124 | +class BytesImpl implements Bytes { |
| 125 | + |
| 126 | + private final byte[] byteArray; |
| 127 | + |
| 128 | + BytesImpl(@Nonnull byte[] unsafeByteArray) { |
| 129 | + this.byteArray = Objects.requireNonNull(unsafeByteArray); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public int length() { |
| 134 | + return byteArray.length; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public byte byteAt(int index) { |
| 139 | + return byteArray[index]; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public byte[] copyArrayOfRange(int from, int to) { |
| 144 | + return Arrays.copyOfRange(byteArray, from, to); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String toHexString() { |
| 149 | + return ByteUtil.toHexString(byteArray); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public String toHexString(int off, int length) { |
| 154 | + return ByteUtil.toHexString(byteArray, off, length); |
| 155 | + } |
| 156 | + |
| 157 | + @Nonnull |
| 158 | + @Override |
| 159 | + public byte[] asUnsafeByteArray() { |
| 160 | + return byteArray; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public String toString() { |
| 165 | + return toPrintableString(); |
| 166 | + } |
| 167 | +} |
0 commit comments