KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > conv > FloatArrayConverter


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.conv;
13
14 import com.versant.core.jdbc.JdbcConverter;
15
16 /**
17  * This converter converts float[] to and from SQL. It converts the float[]
18  * to and from a float[] and delegates to a nested converter.
19  * TODO This could be done much faster with java.nio buffers.
20  * @keep-all
21  */

22 public class FloatArrayConverter extends TypeAsBytesConverterBase {
23
24     public static class Factory extends TypeAsBytesConverterBase.Factory {
25
26         protected JdbcConverter createConverter(JdbcConverter nested) {
27             return new FloatArrayConverter(nested);
28         }
29
30     }
31
32     public FloatArrayConverter(JdbcConverter nested) {
33         super(nested);
34     }
35
36     /**
37      * Convert a byte[] into an instance of our value class.
38      */

39     protected Object JavaDoc fromByteArray(byte[] buf) {
40         int n = buf.length / 4;
41         float[] a = new float[n];
42         int i = 0, j = 0;
43         for (; i < n; ) {
44             a[i++] = Float.intBitsToFloat(
45                       ((buf[j++] & 0xFF) << 24)
46                     + ((buf[j++] & 0xFF) << 16)
47                     + ((buf[j++] & 0xFF) << 8)
48                     + (buf[j++] & 0xFF));
49         }
50         return a;
51     }
52
53     /**
54      * Convert an instance of our value class into a byte[].
55      */

56     protected byte[] toByteArray(Object JavaDoc value) {
57         if (value == null) return null;
58         float[] a = (float[])value;
59         int n = a.length;
60         byte[] buf = new byte[n * 4];
61         int i = 0, j = 0;
62         for (; i < n; ) {
63
64             int x = Float.floatToRawIntBits(a[i++]);
65
66
67             buf[j++] = (byte)((x >>> 24) & 0xFF);
68             buf[j++] = (byte)((x >>> 16) & 0xFF);
69             buf[j++] = (byte)((x >>> 8) & 0xFF);
70             buf[j++] = (byte)(x & 0xFF);
71         }
72         return buf;
73     }
74
75     /**
76      * Get the type of our expected value objects (e.g. java.util.Locale
77      * for a converter for Locale's).
78      */

79     public Class JavaDoc getValueType() {
80         return float[].class;
81     }
82
83 }
84
85
Popular Tags