01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdbc.sql.conv;
12:
13: import com.versant.core.jdbc.JdbcConverter;
14:
15: /**
16: * This converter converts char[] to and from SQL. It converts the char[]
17: * to and from a byte[] and delegates to a nested converter.
18: * TODO This could be done much faster with java.nio buffers.
19: * @keep-all
20: */
21: public class CharArrayConverter extends TypeAsBytesConverterBase {
22:
23: public static class Factory extends
24: TypeAsBytesConverterBase.Factory {
25:
26: protected JdbcConverter createConverter(JdbcConverter nested) {
27: return new CharArrayConverter(nested);
28: }
29:
30: }
31:
32: public CharArrayConverter(JdbcConverter nested) {
33: super (nested);
34: }
35:
36: /**
37: * Convert a byte[] into an instance of our value class.
38: */
39: protected Object fromByteArray(byte[] buf) {
40: int n = buf.length / 2;
41: char[] a = new char[n];
42: int i = 0, j = 0;
43: for (; i < n;) {
44: a[i++] = (char) (((buf[j++] & 0xFF) << 8) + (buf[j++] & 0xFF));
45: }
46: return a;
47: }
48:
49: /**
50: * Convert an instance of our value class into a byte[].
51: */
52: protected byte[] toByteArray(Object value) {
53: if (value == null)
54: return null;
55: char[] a = (char[]) value;
56: int n = a.length;
57: byte[] buf = new byte[n * 2];
58: int i = 0, j = 0;
59: for (; i < n;) {
60: char x = a[i++];
61: buf[j++] = (byte) ((x >>> 8) & 0xFF);
62: buf[j++] = (byte) (x & 0xFF);
63: }
64: return buf;
65: }
66:
67: /**
68: * Get the type of our expected value objects (e.g. java.util.Locale
69: * for a converter for Locale's).
70: */
71: public Class getValueType() {
72: return char[].class;
73: }
74:
75: }
|