Debian Codegraph @

src/main/org/h2/value/ValueBytesBase.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.value;
7
8 import java.util.Arrays;
9
10 import org.h2.engine.CastDataProvider;
11 import org.h2.engine.Constants;
12 import org.h2.message.DbException;
13 import org.h2.util.Bits;
14 import org.h2.util.StringUtils;
15 import org.h2.util.Utils;
16
17 /**
18 * Base implementation of byte array based data types.
19 */
20 abstract class ValueBytesBase extends Value {
21
22 /**
23 * The value.
24 */
25 byte[] value;
26
27 /**
28 * The hash code.
29 */
30 int hash;
31
32 ValueBytesBase(byte[] value) {
33 int length = value.length;
34 if (length > Constants.MAX_STRING_LENGTH) {
35 throw DbException.getValueTooLongException(getTypeName(getValueType()),
36 StringUtils.convertBytesToHex(value, 41), length);
37 }
38 this.value = value;
39 }
40
41 @Override
42 public final byte[] getBytes() {
43 return Utils.cloneByteArray(value);
44 }
45
46 @Override
47 public final byte[] getBytesNoCopy() {
48 return value;
49 }
50
51 @Override
52 public final int compareTypeSafe(Value v, CompareMode mode, CastDataProvider provider) {
53 return Bits.compareNotNullUnsigned(value, ((ValueBytesBase) v).value);
54 }
55
56 @Override
57 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
58 return StringUtils.convertBytesToHex(builder.append("X'"), value).append('\'');
59 }
60
61 @Override
62 public final int hashCode() {
63 int h = hash;
64 if (h == 0) {
65 h = getClass().hashCode() ^ Utils.getByteArrayHash(value);
66 if (h == 0) {
67 h = 1_234_570_417;
68 }
69 hash = h;
70 }
71 return h;
72 }
73
74 @Override
75 public int getMemory() {
76 return value.length + 24;
77 }
78
79 @Override
80 public final boolean equals(Object other) {
81 return other != null && getClass() == other.getClass() && Arrays.equals(value, ((ValueBytesBase) other).value);
82 }
83
84 }