|
| 1 | +/* |
| 2 | + * Copyright 2017 Patrick Favre-Bulle |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +package at.favre.lib.bytes; |
| 23 | + |
| 24 | +import java.nio.ByteOrder; |
| 25 | +import java.security.SecureRandom; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Objects; |
| 28 | + |
| 29 | +/** |
| 30 | + * Mutable version of {@link Bytes} created by calling {@link #mutable()}. If possible, all transformations are done in place, without creating a copy. |
| 31 | + * <p> |
| 32 | + * Adds additional mutator, which may change the internal array in-place, like {@link #wipe()} |
| 33 | + */ |
| 34 | +@SuppressWarnings("WeakerAccess") |
| 35 | +public final class MutableBytes extends Bytes implements AutoCloseable { |
| 36 | + |
| 37 | + MutableBytes(byte[] byteArray, ByteOrder byteOrder) { |
| 38 | + super(byteArray, byteOrder, new Factory()); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public boolean isMutable() { |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Uses given array to overwrite internal array |
| 48 | + * |
| 49 | + * @param newArray used to overwrite internal |
| 50 | + * @return this instance |
| 51 | + * @throws IndexOutOfBoundsException if newArray.length > internal length |
| 52 | + */ |
| 53 | + public MutableBytes overwrite(byte[] newArray) { |
| 54 | + return overwrite(newArray, 0); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Uses given array to overwrite internal array. |
| 59 | + * |
| 60 | + * @param newArray used to overwrite internal |
| 61 | + * @param offsetInternalArray index of the internal array to start overwriting |
| 62 | + * @return this instance |
| 63 | + * @throws IndexOutOfBoundsException if newArray.length + offsetInternalArray > internal length |
| 64 | + */ |
| 65 | + public MutableBytes overwrite(byte[] newArray, int offsetInternalArray) { |
| 66 | + Objects.requireNonNull(newArray, "must provide non-null array as source"); |
| 67 | + System.arraycopy(newArray, 0, internalArray(), offsetInternalArray, newArray.length); |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Sets new byte to given index |
| 73 | + * |
| 74 | + * @param index the index to change |
| 75 | + * @param newByte the new byte to set |
| 76 | + * @return this instance |
| 77 | + */ |
| 78 | + public MutableBytes setByteAt(int index, byte newByte) { |
| 79 | + internalArray()[index] = newByte; |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Fills the internal byte array with all zeros |
| 85 | + * |
| 86 | + * @return this instance |
| 87 | + */ |
| 88 | + public MutableBytes wipe() { |
| 89 | + return fill((byte) 0); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Fills the internal byte array with provided byte |
| 94 | + * |
| 95 | + * @param fillByte to fill with |
| 96 | + * @return this instance |
| 97 | + */ |
| 98 | + public MutableBytes fill(byte fillByte) { |
| 99 | + Arrays.fill(internalArray(), fillByte); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Fills the internal byte array with random data provided by {@link SecureRandom} |
| 105 | + * |
| 106 | + * @return this instance |
| 107 | + */ |
| 108 | + public MutableBytes secureWipe() { |
| 109 | + return secureWipe(new SecureRandom()); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Fills the internal byte array with random data provided by given random instance |
| 114 | + * |
| 115 | + * @param random to generate entropy |
| 116 | + * @return this instance |
| 117 | + */ |
| 118 | + public MutableBytes secureWipe(SecureRandom random) { |
| 119 | + Objects.requireNonNull(random, "random param must not be null"); |
| 120 | + if (length() > 0) { |
| 121 | + random.nextBytes(internalArray()); |
| 122 | + } |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Convert this instance to an immutable version with the same reference of the internal array and byte-order. |
| 128 | + * If the mutable instance is kept, it can be used to alter the internal array of the just created instance, so be |
| 129 | + * aware. |
| 130 | + * |
| 131 | + * @return immutable version of this instance |
| 132 | + */ |
| 133 | + public Bytes immutable() { |
| 134 | + return Bytes.wrap(internalArray(), byteOrder()); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public int hashCode() { |
| 139 | + return Util.Obj.hashCode(internalArray(), byteOrder()); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public boolean equals(Object o) { |
| 144 | + return super.equals(o); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void close() { |
| 149 | + secureWipe(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Factory creating mutable byte types |
| 154 | + */ |
| 155 | + private static class Factory implements BytesFactory { |
| 156 | + @Override |
| 157 | + public Bytes wrap(byte[] array, ByteOrder byteOrder) { |
| 158 | + return new MutableBytes(array, byteOrder); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments