KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > value > ValueBytesBase


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.value;
6
7 import java.sql.PreparedStatement JavaDoc;
8 import java.sql.SQLException JavaDoc;
9
10 import org.h2.util.ByteUtils;
11
12 abstract class ValueBytesBase extends Value {
13
14     private byte[] value;
15     private int hash;
16     
17     protected ValueBytesBase(byte[] v) {
18         this.value = v;
19     }
20
21     public String JavaDoc getSQL() {
22         return "X'" + getString() + "'";
23     }
24
25     public byte[] getBytesNoCopy() {
26         return value;
27     }
28
29     public byte[] getBytes() {
30         return ByteUtils.cloneByteArray(value);
31     }
32
33     protected int compareSecure(Value v, CompareMode mode) {
34         byte[] v2 = ((ValueBytesBase) v).value;
35         return ByteUtils.compareNotNull(value, v2);
36     }
37
38     public String JavaDoc getString() {
39         return ByteUtils.convertBytesToString(value);
40     }
41
42     public long getPrecision() {
43         return value.length;
44     }
45
46     public int hashCode() {
47         if (hash == 0) {
48             hash = ByteUtils.getByteArrayHash(value);
49         }
50         return hash;
51     }
52
53     public Object JavaDoc getObject() {
54         return getBytes();
55     }
56
57     public void set(PreparedStatement JavaDoc prep, int parameterIndex) throws SQLException JavaDoc {
58         prep.setBytes(parameterIndex, value);
59     }
60
61     public int getDisplaySize() {
62         return value.length * 2;
63     }
64     
65     protected boolean isEqual(Value v) {
66         return v instanceof ValueBytesBase && ByteUtils.compareNotNull(value, ((ValueBytesBase)v).value) == 0;
67     }
68
69 // public String getJavaString() {
70
// return "ByteUtils.convertStringToBytes(\"" + toString() + "\")";
71
//}
72

73 }
74
Popular Tags