Debian Codegraph @

src/main/org/h2/value/ValueBinary.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.nio.charset.StandardCharsets;
9
10 import org.h2.engine.SysProperties;
11 import org.h2.util.Utils;
12
13 /**
14 * Implementation of the BINARY data type.
15 */
16 public final class ValueBinary extends ValueBytesBase {
17
18 /**
19 * Associated TypeInfo.
20 */
21 private TypeInfo type;
22
23 private ValueBinary(byte[] value) {
24 super(value);
25 }
26
27 /**
28 * Get or create a VARBINARY value for the given byte array.
29 * Clone the data.
30 *
31 * @param b the byte array
32 * @return the value
33 */
34 public static ValueBinary get(byte[] b) {
35 return getNoCopy(Utils.cloneByteArray(b));
36 }
37
38 /**
39 * Get or create a VARBINARY value for the given byte array.
40 * Do not clone the date.
41 *
42 * @param b the byte array
43 * @return the value
44 */
45 public static ValueBinary getNoCopy(byte[] b) {
46 ValueBinary obj = new ValueBinary(b);
47 if (b.length > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
48 return obj;
49 }
50 return (ValueBinary) Value.cache(obj);
51 }
52
53 @Override
54 public TypeInfo getType() {
55 TypeInfo type = this.type;
56 if (type == null) {
57 long precision = value.length;
58 this.type = type = new TypeInfo(BINARY, precision, 0, null);
59 }
60 return type;
61 }
62
63 @Override
64 public int getValueType() {
65 return BINARY;
66 }
67
68 @Override
69 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
70 if ((sqlFlags & NO_CASTS) == 0) {
71 int length = value.length;
72 return super.getSQL(builder.append("CAST("), sqlFlags).append(" AS BINARY(")
73 .append(length > 0 ? length : 1).append("))");
74 }
75 return super.getSQL(builder, sqlFlags);
76 }
77
78 @Override
79 public String getString() {
80 return new String(value, StandardCharsets.UTF_8);
81 }
82
83 }