|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 original authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micronaut.core.io.buffer; |
| 17 | + |
| 18 | +import io.micronaut.core.annotation.Experimental; |
| 19 | +import io.micronaut.core.annotation.Internal; |
| 20 | + |
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.io.OutputStream; |
| 24 | +import java.nio.charset.Charset; |
| 25 | +import java.util.Arrays; |
| 26 | + |
| 27 | +/** |
| 28 | + * A {@link ByteBuffer} implementation that is backed by a byte array. |
| 29 | + * |
| 30 | + * @since 4.7 |
| 31 | + */ |
| 32 | +@Internal |
| 33 | +@Experimental |
| 34 | +public class ByteArrayByteBuffer implements ByteBuffer<byte[]> { |
| 35 | + |
| 36 | + private final byte[] underlyingBytes; |
| 37 | + private int readerIndex; |
| 38 | + private int writerIndex; |
| 39 | + |
| 40 | + /** |
| 41 | + * Construct a new {@link ByteArrayByteBuffer} for the given bytes. |
| 42 | + * |
| 43 | + * @param underlyingBytes the bytes to wrap |
| 44 | + */ |
| 45 | + ByteArrayByteBuffer(byte[] underlyingBytes) { |
| 46 | + this(underlyingBytes, underlyingBytes.length); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Construct a new {@link ByteArrayByteBuffer} for the given bytes and capacity. |
| 51 | + * If capacity is greater than the length of the underlyingBytes, extra bytes will be zeroed out. |
| 52 | + * If capacity is less than the length of the underlyingBytes, the underlyingBytes will be truncated. |
| 53 | + * |
| 54 | + * @param underlyingBytes the bytes to wrap |
| 55 | + * @param capacity the capacity of the buffer |
| 56 | + */ |
| 57 | + ByteArrayByteBuffer(byte[] underlyingBytes, int capacity) { |
| 58 | + if (capacity < underlyingBytes.length) { |
| 59 | + this.underlyingBytes = Arrays.copyOf(underlyingBytes, capacity); |
| 60 | + } else if (capacity > underlyingBytes.length) { |
| 61 | + this.underlyingBytes = new byte[capacity]; |
| 62 | + System.arraycopy(underlyingBytes, 0, this.underlyingBytes, 0, underlyingBytes.length); |
| 63 | + } else { |
| 64 | + this.underlyingBytes = underlyingBytes; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public byte[] asNativeBuffer() { |
| 70 | + return underlyingBytes; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public int readableBytes() { |
| 75 | + return underlyingBytes.length - readerIndex; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public int writableBytes() { |
| 80 | + return underlyingBytes.length - writerIndex; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public int maxCapacity() { |
| 85 | + return underlyingBytes.length; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public ByteArrayByteBuffer capacity(int capacity) { |
| 90 | + return new ByteArrayByteBuffer(underlyingBytes, capacity); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public int readerIndex() { |
| 95 | + return readerIndex; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public ByteArrayByteBuffer readerIndex(int readPosition) { |
| 100 | + this.readerIndex = Math.min(readPosition, underlyingBytes.length); |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public int writerIndex() { |
| 106 | + return writerIndex; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public ByteArrayByteBuffer writerIndex(int position) { |
| 111 | + this.writerIndex = Math.min(position, underlyingBytes.length); |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public byte read() { |
| 117 | + return underlyingBytes[readerIndex++]; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public CharSequence readCharSequence(int length, Charset charset) { |
| 122 | + String s = new String(underlyingBytes, readerIndex, length, charset); |
| 123 | + readerIndex += length; |
| 124 | + return s; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public ByteArrayByteBuffer read(byte[] destination) { |
| 129 | + int count = Math.min(readableBytes(), destination.length); |
| 130 | + System.arraycopy(underlyingBytes, readerIndex, destination, 0, count); |
| 131 | + readerIndex += count; |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public ByteArrayByteBuffer read(byte[] destination, int offset, int length) { |
| 137 | + int count = Math.min(readableBytes(), Math.min(destination.length - offset, length)); |
| 138 | + System.arraycopy(underlyingBytes, readerIndex, destination, offset, count); |
| 139 | + readerIndex += count; |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public ByteArrayByteBuffer write(byte b) { |
| 145 | + underlyingBytes[writerIndex++] = b; |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public ByteArrayByteBuffer write(byte[] source) { |
| 151 | + int count = Math.min(writableBytes(), source.length); |
| 152 | + System.arraycopy(source, 0, underlyingBytes, writerIndex, count); |
| 153 | + writerIndex += count; |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public ByteArrayByteBuffer write(CharSequence source, Charset charset) { |
| 159 | + write(source.toString().getBytes(charset)); |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public ByteArrayByteBuffer write(byte[] source, int offset, int length) { |
| 165 | + int count = Math.min(writableBytes(), length); |
| 166 | + System.arraycopy(source, offset, underlyingBytes, writerIndex, count); |
| 167 | + writerIndex += count; |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public ByteArrayByteBuffer write(ByteBuffer... buffers) { |
| 173 | + for (ByteBuffer<?> buffer : buffers) { |
| 174 | + write(buffer.toByteArray()); |
| 175 | + } |
| 176 | + return this; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public ByteArrayByteBuffer write(java.nio.ByteBuffer... buffers) { |
| 181 | + for (java.nio.ByteBuffer buffer : buffers) { |
| 182 | + write(buffer.array()); |
| 183 | + } |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public ByteArrayByteBuffer slice(int index, int length) { |
| 189 | + return new ByteArrayByteBuffer(Arrays.copyOfRange(underlyingBytes, index, index + length), length); |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public java.nio.ByteBuffer asNioBuffer() { |
| 194 | + return java.nio.ByteBuffer.wrap(underlyingBytes, readerIndex, readableBytes()); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public java.nio.ByteBuffer asNioBuffer(int index, int length) { |
| 199 | + return java.nio.ByteBuffer.wrap(underlyingBytes, index, length); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public InputStream toInputStream() { |
| 204 | + return new ByteArrayInputStream(underlyingBytes, readerIndex, readableBytes()); |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public OutputStream toOutputStream() { |
| 209 | + throw new IllegalStateException("Not implemented"); |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public byte[] toByteArray() { |
| 214 | + return Arrays.copyOfRange(underlyingBytes, readerIndex, readableBytes()); |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public String toString(Charset charset) { |
| 219 | + return new String(underlyingBytes, readerIndex, readableBytes(), charset); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public int indexOf(byte b) { |
| 224 | + for (int i = readerIndex; i < underlyingBytes.length; i++) { |
| 225 | + if (underlyingBytes[i] == b) { |
| 226 | + return i; |
| 227 | + } |
| 228 | + } |
| 229 | + return -1; |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public byte getByte(int index) { |
| 234 | + return underlyingBytes[index]; |
| 235 | + } |
| 236 | +} |
0 commit comments