File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueBytes.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.value;
8
9 import java.sql.PreparedStatement;
10 import java.sql.SQLException;
11 import org.h2.constant.SysProperties;
12 import org.h2.util.MathUtils;
13 import org.h2.util.StringUtils;
14 import org.h2.util.Utils;
15
16 /**
17 * Implementation of the BINARY data type.
18 * It is also the base class for ValueJavaObject.
19 */
20 public class ValueBytes extends Value {
21
22 private static final ValueBytes EMPTY = new ValueBytes(Utils.EMPTY_BYTES);
23
24 /**
25 * The value.
26 */
27 protected byte[] value;
28
29 /**
30 * The hash code.
31 */
32 protected int hash;
33
34 protected ValueBytes(byte[] v) {
35 this.value = v;
36 }
37
38 /**
39 * Get or create a bytes value for the given byte array.
40 * Clone the data.
41 *
42 * @param b the byte array
43 * @return the value
44 */
45 public static ValueBytes get(byte[] b) {
46 if (b.length == 0) {
47 return EMPTY;
48 }
49 b = Utils.cloneByteArray(b);
50 return getNoCopy(b);
51 }
52
53 /**
54 * Get or create a bytes value for the given byte array.
55 * Do not clone the date.
56 *
57 * @param b the byte array
58 * @return the value
59 */
60 public static ValueBytes getNoCopy(byte[] b) {
61 if (b.length == 0) {
62 return EMPTY;
63 }
64 ValueBytes obj = new ValueBytes(b);
65 if (b.length > SysProperties.OBJECT_CACHE_MAX_PER_ELEMENT_SIZE) {
66 return obj;
67 }
68 return (ValueBytes) Value.cache(obj);
69 }
70
71 public int getType() {
72 return Value.BYTES;
73 }
74
75 public String getSQL() {
76 return "X'" + StringUtils.convertBytesToHex(getBytesNoCopy()) + "'";
77 }
78
79 public byte[] getBytesNoCopy() {
80 return value;
81 }
82
83 public byte[] getBytes() {
84 return Utils.cloneByteArray(getBytesNoCopy());
85 }
86
87 protected int compareSecure(Value v, CompareMode mode) {
88 byte[] v2 = ((ValueBytes) v).value;
89 return Utils.compareNotNull(value, v2);
90 }
91
92 public String getString() {
93 return StringUtils.convertBytesToHex(value);
94 }
95
96 public long getPrecision() {
97 return value.length;
98 }
99
100 public int hashCode() {
101 if (hash == 0) {
102 hash = Utils.getByteArrayHash(value);
103 }
104 return hash;
105 }
106
107 public Object getObject() {
108 return getBytes();
109 }
110
111 public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
112 prep.setBytes(parameterIndex, value);
113 }
114
115 public int getDisplaySize() {
116 return MathUtils.convertLongToInt(value.length * 2L);
117 }
118
119 public int getMemory() {
120 return value.length + 24;
121 }
122
123 public boolean equals(Object other) {
124 return other instanceof ValueBytes && Utils.compareNotNull(value, ((ValueBytes) other).value) == 0;
125 }
126
127 public Value convertPrecision(long precision, boolean force) {
128 if (value.length <= precision) {
129 return this;
130 }
131 int len = MathUtils.convertLongToInt(precision);
132 byte[] buff = new byte[len];
133 System.arraycopy(value, 0, buff, 0, len);
134 return get(buff);
135 }
136
137 }
138