|
| 1 | +package com.clickhouse.client; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.nio.ByteBuffer; |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +/** |
| 8 | + * Lite version of {@link java.nio.ByteBuffer}. |
| 9 | + */ |
| 10 | +public class ClickHouseByteBuffer implements Serializable { |
| 11 | + private static final long serialVersionUID = -8178041799873465082L; |
| 12 | + |
| 13 | + /** |
| 14 | + * Empty byte array. |
| 15 | + */ |
| 16 | + public static final byte[] EMPTY_BYTES = new byte[0]; |
| 17 | + |
| 18 | + /** |
| 19 | + * Empty and read-only byte buffer. |
| 20 | + */ |
| 21 | + public static final ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(EMPTY_BYTES).asReadOnlyBuffer(); |
| 22 | + |
| 23 | + /** |
| 24 | + * Creates an empty byte buffer. |
| 25 | + * |
| 26 | + * @return empty byte buffer |
| 27 | + */ |
| 28 | + public static ClickHouseByteBuffer newInstance() { |
| 29 | + return new ClickHouseByteBuffer(EMPTY_BYTES, 0, 0); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Wraps given byte array as byte buffer. |
| 34 | + * |
| 35 | + * @param bytes byte array |
| 36 | + * @return byte buffer |
| 37 | + */ |
| 38 | + public static ClickHouseByteBuffer of(byte[] bytes) { |
| 39 | + return bytes == null || bytes.length == 0 ? newInstance() : new ClickHouseByteBuffer(bytes, 0, bytes.length); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Wraps given byte array as byte buffer. |
| 44 | + * |
| 45 | + * @param bytes byte array |
| 46 | + * @param offset offset |
| 47 | + * @param length length |
| 48 | + * @return byte buffer |
| 49 | + */ |
| 50 | + public static ClickHouseByteBuffer of(byte[] bytes, int offset, int length) { |
| 51 | + if (bytes == null || bytes.length == 0 || length == 0) { |
| 52 | + return newInstance(); |
| 53 | + } else { |
| 54 | + validate(bytes, offset, length); |
| 55 | + } |
| 56 | + |
| 57 | + return new ClickHouseByteBuffer(bytes, offset, length); |
| 58 | + } |
| 59 | + |
| 60 | + static void validate(byte[] bytes, int offset, int length) { |
| 61 | + int len = ClickHouseChecker.nonNull(bytes, "Byte array").length; |
| 62 | + if (ClickHouseChecker.between(offset, "Offset", 0, len) |
| 63 | + + ClickHouseChecker.between(length, "Length", 0, len) > len) { |
| 64 | + throw new IllegalArgumentException( |
| 65 | + ClickHouseUtils.format("Offset(%d) plus length(%d) should not greater than %d", offset, length, |
| 66 | + len)); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + protected byte[] array; |
| 71 | + protected int position; |
| 72 | + protected int length; |
| 73 | + |
| 74 | + protected ClickHouseByteBuffer(byte[] bytes, int offset, int length) { |
| 75 | + this.array = bytes; |
| 76 | + this.position = offset; |
| 77 | + this.length = length; |
| 78 | + } |
| 79 | + |
| 80 | + public boolean isEmpty() { |
| 81 | + return length < 1; |
| 82 | + } |
| 83 | + |
| 84 | + public ClickHouseByteBuffer reset() { |
| 85 | + array = EMPTY_BYTES; |
| 86 | + position = 0; |
| 87 | + length = 0; |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + public ClickHouseByteBuffer update(byte[] bytes) { |
| 92 | + if (bytes == null || bytes.length == 0) { |
| 93 | + reset(); |
| 94 | + } else { |
| 95 | + array = bytes; |
| 96 | + position = 0; |
| 97 | + length = bytes.length; |
| 98 | + } |
| 99 | + |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public ClickHouseByteBuffer update(byte[] bytes, int offset, int length) { |
| 104 | + if (bytes == null || bytes.length == 0 || length == 0) { |
| 105 | + return reset(); |
| 106 | + } else { |
| 107 | + validate(bytes, offset, length); |
| 108 | + } |
| 109 | + |
| 110 | + this.array = bytes; |
| 111 | + this.position = offset; |
| 112 | + this.length = length; |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + public byte[] array() { |
| 117 | + return array; |
| 118 | + } |
| 119 | + |
| 120 | + public int position() { |
| 121 | + return position; |
| 122 | + } |
| 123 | + |
| 124 | + public int length() { |
| 125 | + return length; |
| 126 | + } |
| 127 | + |
| 128 | + public int limit() { |
| 129 | + return position + length; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public int hashCode() { |
| 134 | + final int prime = 31; |
| 135 | + int result = prime + Arrays.hashCode(array); |
| 136 | + result = prime * result + position; |
| 137 | + result = prime * result + length; |
| 138 | + return result; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public boolean equals(Object obj) { |
| 143 | + if (this == obj) { |
| 144 | + return true; |
| 145 | + } else if (obj == null || getClass() != obj.getClass()) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + ClickHouseByteBuffer other = (ClickHouseByteBuffer) obj; |
| 150 | + return Arrays.equals(array, other.array) && position == other.position && length == other.length; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public String toString() { |
| 155 | + return new StringBuilder().append(getClass().getSimpleName()).append("array=").append(array) |
| 156 | + .append(", position=").append(position).append(", length=").append(length).append(')').toString(); |
| 157 | + } |
| 158 | +} |
0 commit comments