|
| 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 java.nio.ByteBuffer; |
| 33 | +import java.util.function.Function; |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * A {@link ByteBank} backed by a {@link ByteBuffer}. Self-growing up to a |
| 38 | + * maximal capacity of {@link Integer#MAX_VALUE} |
| 39 | + * |
| 40 | + * @author Gabriel Einsdorf |
| 41 | + */ |
| 42 | +public class ByteBufferByteBank implements ByteBank { |
| 43 | + |
| 44 | + private static final int DEFAULT_CAPACITY = 10_000; |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a bank backed by an <em>off-heap</em> direct buffer. |
| 48 | + * <p> |
| 49 | + * Direct buffers are what make this bank worth having alongside |
| 50 | + * {@link ByteArrayByteBank}: bytes live outside the Java heap, so they can |
| 51 | + * be handed to native code and to shared memory without a copy. Appose's |
| 52 | + * named shared memory blocks are the motivating case. |
| 53 | + * </p> |
| 54 | + */ |
| 55 | + public static ByteBufferByteBank direct() { |
| 56 | + return new ByteBufferByteBank(ByteBuffer::allocateDirect); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates a bank backed by an <em>off-heap</em> direct buffer of the given |
| 61 | + * initial capacity. |
| 62 | + * |
| 63 | + * @see #direct() |
| 64 | + */ |
| 65 | + public static ByteBufferByteBank direct(final int initialCapacity) { |
| 66 | + return new ByteBufferByteBank(ByteBuffer::allocateDirect, initialCapacity); |
| 67 | + } |
| 68 | + |
| 69 | + private ByteBuffer buffer; |
| 70 | + |
| 71 | + private int size; |
| 72 | + |
| 73 | + private Function<Integer, ByteBuffer> provider; |
| 74 | + |
| 75 | + public ByteBufferByteBank() { |
| 76 | + provider = ByteBuffer::allocate; |
| 77 | + buffer = provider.apply(DEFAULT_CAPACITY); |
| 78 | + } |
| 79 | + |
| 80 | + public ByteBufferByteBank(final Function<Integer, ByteBuffer> provider) { |
| 81 | + this.provider = provider; |
| 82 | + buffer = provider.apply(DEFAULT_CAPACITY); |
| 83 | + } |
| 84 | + |
| 85 | + public ByteBufferByteBank(final Function<Integer, ByteBuffer> provider, |
| 86 | + final int initialCapacity) |
| 87 | + { |
| 88 | + this.provider = provider; |
| 89 | + buffer = provider.apply(initialCapacity); |
| 90 | + } |
| 91 | + |
| 92 | + public ByteBufferByteBank(final int initialCapacity) { |
| 93 | + provider = ByteBuffer::allocate; |
| 94 | + buffer = provider.apply(initialCapacity); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Gets whether this bank's bytes live off the Java heap, and so can reach |
| 99 | + * native code and shared memory without a copy. |
| 100 | + * |
| 101 | + * @see #direct() |
| 102 | + */ |
| 103 | + public boolean isDirect() { |
| 104 | + return buffer.isDirect(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public long getMaxBufferSize() { |
| 109 | + return Integer.MAX_VALUE; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void setBytes(final long startpos, final byte[] bytes, |
| 114 | + final int offset, final int length) |
| 115 | + { |
| 116 | + // ensure we have space |
| 117 | + checkWritePos(startpos, startpos + length); |
| 118 | + final int neededCapacity = size + length; |
| 119 | + ensureCapacity(neededCapacity); |
| 120 | + |
| 121 | + // copy the data |
| 122 | + buffer.position((int) startpos); |
| 123 | + buffer.put(bytes, offset, length); |
| 124 | + |
| 125 | + // update the maxpos |
| 126 | + updateSize(startpos + length); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void setByte(final long pos, final byte b) { |
| 131 | + checkWritePos(pos, pos); |
| 132 | + if (pos == buffer.capacity()) { |
| 133 | + ensureCapacity((int) pos + 1); |
| 134 | + } |
| 135 | + buffer.put((int) pos, b); |
| 136 | + updateSize(pos + 1); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void clear() { |
| 141 | + buffer.clear(); |
| 142 | + size = 0; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public byte getByte(final long pos) { |
| 147 | + checkReadPos(pos, pos); |
| 148 | + // the buffer might contain bytes with negative value |
| 149 | + // we need to flip the sign to positive to satisfy the contract of this |
| 150 | + // method |
| 151 | + return buffer.get((int) pos); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public int getBytes(final long startPos, final byte[] b, final int offset, |
| 156 | + final int length) |
| 157 | + { |
| 158 | + checkReadPos(startPos, startPos + length); |
| 159 | + // ensure we don't try to read data which is not in the buffer |
| 160 | + final int readLength = (int) Math.min(size() - startPos, length); |
| 161 | + buffer.position((int) startPos); |
| 162 | + buffer.get(b, offset, readLength); |
| 163 | + |
| 164 | + return readLength; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public long size() { |
| 169 | + return size; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public boolean isReadOnly() { |
| 174 | + // NB: Some ByteBuffers are read-only. But there is no API to check it. |
| 175 | + // Therefore, we make a "best effort" guess based on known read-only types. |
| 176 | + // Since these read-only types are not public, we compare class names rather |
| 177 | + // than checking for type equality or using instanceof. |
| 178 | + final String className = buffer.getClass().getName(); |
| 179 | + return className.equals("java.nio.HeapByteBufferR") || |
| 180 | + className.equals("java.nio.DirectByteBufferR"); |
| 181 | + } |
| 182 | + |
| 183 | + // -- Helper methods -- |
| 184 | + |
| 185 | + private void ensureCapacity(final int minCapacity) { |
| 186 | + final int oldCapacity = buffer.capacity(); |
| 187 | + if (minCapacity <= oldCapacity) return; // no need to grow |
| 188 | + |
| 189 | + // grow the array by up to 50% (plus a small constant) |
| 190 | + final int growth = Math.min(oldCapacity / 2 + 16, Integer.MAX_VALUE); |
| 191 | + final int newCapacity; |
| 192 | + if (growth > Integer.MAX_VALUE - oldCapacity) { |
| 193 | + // growth would push array over the maximum array size |
| 194 | + newCapacity = Integer.MAX_VALUE; |
| 195 | + } |
| 196 | + else newCapacity = oldCapacity + growth; |
| 197 | + // ensure the array grows by at least the requested minimum capacity |
| 198 | + final int newLength = Math.max(minCapacity, newCapacity); |
| 199 | + |
| 200 | + // copy the data into a new buffer |
| 201 | + buffer.position(0); |
| 202 | + // NB: allocate through the provider, not ByteBuffer.allocate. Growing a |
| 203 | + // direct bank must yield another direct buffer: SciJava Common's version |
| 204 | + // quietly moved the bytes back onto the heap here, losing the off-heap |
| 205 | + // property that is the whole reason to choose this bank. |
| 206 | + final ByteBuffer newBuffer = provider.apply(newLength); |
| 207 | + newBuffer.order(buffer.order()); |
| 208 | + newBuffer.put(buffer); |
| 209 | + buffer = newBuffer; |
| 210 | + } |
| 211 | + |
| 212 | + private void updateSize(final long newSize) { |
| 213 | + size = (int) (newSize > size ? newSize : size); |
| 214 | + } |
| 215 | + |
| 216 | +} |
0 commit comments