01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.value;
07:
08: import java.sql.PreparedStatement;
09: import java.sql.SQLException;
10:
11: import org.h2.util.ByteUtils;
12: import org.h2.util.MathUtils;
13:
14: /**
15: * This is the base class for ValueBytes and ValueJavaObject.
16: */
17: abstract class ValueBytesBase extends Value {
18:
19: private final byte[] value;
20: private int hash;
21:
22: protected ValueBytesBase(byte[] v) {
23: this .value = v;
24: }
25:
26: public String getSQL() {
27: return "X'" + getString() + "'";
28: }
29:
30: public byte[] getBytesNoCopy() {
31: return value;
32: }
33:
34: public byte[] getBytes() {
35: return ByteUtils.cloneByteArray(value);
36: }
37:
38: protected int compareSecure(Value v, CompareMode mode) {
39: byte[] v2 = ((ValueBytesBase) v).value;
40: return ByteUtils.compareNotNull(value, v2);
41: }
42:
43: public String getString() {
44: return ByteUtils.convertBytesToString(value);
45: }
46:
47: public long getPrecision() {
48: return value.length;
49: }
50:
51: public int hashCode() {
52: if (hash == 0) {
53: hash = ByteUtils.getByteArrayHash(value);
54: }
55: return hash;
56: }
57:
58: public Object getObject() {
59: return getBytes();
60: }
61:
62: public void set(PreparedStatement prep, int parameterIndex)
63: throws SQLException {
64: prep.setBytes(parameterIndex, value);
65: }
66:
67: public int getDisplaySize() {
68: return MathUtils.convertLongToInt(value.length * 2L);
69: }
70:
71: public int getMemory() {
72: return value.length + 4;
73: }
74:
75: public boolean equals(Object other) {
76: return other instanceof ValueBytesBase
77: && ByteUtils.compareNotNull(value,
78: ((ValueBytesBase) other).value) == 0;
79: }
80:
81: }
|