|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * Locations and data handles: a uniform way to address and read bytes. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2026 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.io3; |
| 31 | + |
| 32 | +import org.scijava.collections.ByteArray; |
| 33 | + |
| 34 | +/** |
| 35 | + * {@link ByteBank} implementation backed by a {@link ByteArray}. Self-growing |
| 36 | + * up to a maximum capacity of {@link Integer#MAX_VALUE}. |
| 37 | + * |
| 38 | + * @author Gabriel Einsdorf |
| 39 | + */ |
| 40 | +public class ByteArrayByteBank implements ByteBank { |
| 41 | + |
| 42 | + private final ByteArray buffer; |
| 43 | + private long size; |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates a {@link ByteArrayByteBank}. |
| 47 | + */ |
| 48 | + public ByteArrayByteBank() { |
| 49 | + this(new ByteArray()); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates a {@link ByteArrayByteBank} with the specified initial capacity. |
| 54 | + * |
| 55 | + * @param initialCapacity the initial capacity of this {@link ByteBank} |
| 56 | + */ |
| 57 | + public ByteArrayByteBank(final int initialCapacity) { |
| 58 | + this(emptyByteArrayOfCapacity(initialCapacity)); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates a {@link ByteArrayByteBank} that wraps the provided byte array. |
| 63 | + * |
| 64 | + * @param bytes the bytes to wrap |
| 65 | + */ |
| 66 | + public ByteArrayByteBank(final byte[] bytes) { |
| 67 | + this(new ByteArray(bytes)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates a {@link ByteArrayByteBank} that wraps the specified |
| 72 | + * {@link ByteArray}. |
| 73 | + * |
| 74 | + * @param bytes the {@link ByteArray} to wrap |
| 75 | + */ |
| 76 | + public ByteArrayByteBank(final ByteArray bytes) { |
| 77 | + buffer = bytes; |
| 78 | + size = bytes.size(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public long getMaxBufferSize() { |
| 83 | + return Integer.MAX_VALUE; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void setBytes(final long startpos, final byte[] bytes, |
| 88 | + final int offset, final int length) |
| 89 | + { |
| 90 | + // ensure we have space |
| 91 | + checkWritePos(startpos, startpos + length); |
| 92 | + final int neededCapacity = (int) (size + length); |
| 93 | + buffer.ensureCapacity(neededCapacity); |
| 94 | + |
| 95 | + // copy the data |
| 96 | + System.arraycopy(bytes, offset, buffer.getArray(), (int) startpos, length); |
| 97 | + buffer.setSize(neededCapacity); |
| 98 | + updateSize(startpos + length); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void setByte(final long pos, final byte b) { |
| 103 | + checkWritePos(pos, pos); |
| 104 | + buffer.ensureCapacity((int) pos); |
| 105 | + // NB: update the size of the underlying buffer before appending to it |
| 106 | + if (pos == buffer.size()) { |
| 107 | + buffer.setSize((int) (pos + 1)); |
| 108 | + } |
| 109 | + buffer.setValue((int) pos, b); |
| 110 | + updateSize(pos + 1); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void clear() { |
| 115 | + buffer.clear(); |
| 116 | + size = 0; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public byte getByte(final long pos) { |
| 121 | + checkReadPos(pos, pos); |
| 122 | + // the buffer might contain bytes with negative value |
| 123 | + // we need to flip the sign to positive to satisfy the method contract |
| 124 | + return buffer.getValue((int) pos); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public int getBytes(final long startPos, final byte[] b, final int offset, |
| 129 | + final int length) |
| 130 | + { |
| 131 | + checkReadPos(startPos, startPos + length); |
| 132 | + // ensure we don't try to read data which is not in the buffer |
| 133 | + final int readLength = (int) Math.min(size() - startPos, length); |
| 134 | + System.arraycopy(buffer.getArray(), (int) startPos, b, offset, readLength); |
| 135 | + return readLength; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public long size() { |
| 140 | + return size; |
| 141 | + } |
| 142 | + |
| 143 | + // -- Helper methods -- |
| 144 | + |
| 145 | + private void updateSize(final long newSize) { |
| 146 | + size = newSize > size ? newSize : size; |
| 147 | + } |
| 148 | + |
| 149 | + private static ByteArray emptyByteArrayOfCapacity(final int capacity) { |
| 150 | + final ByteArray byteArray = new ByteArray(new byte[capacity]); |
| 151 | + byteArray.setSize(0); |
| 152 | + return byteArray; |
| 153 | + } |
| 154 | +} |
0 commit comments