KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > server > OdbcTransfer


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.server;
6
7 import java.io.DataInputStream JavaDoc;
8 import java.io.DataOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10
11 /**
12  * @author Thomas
13  */

14
15 public class OdbcTransfer {
16     static final int BUFFER_SIZE = 1024;
17
18     private DataInputStream JavaDoc in;
19     private DataOutputStream JavaDoc out;
20
21     OdbcTransfer(DataInputStream JavaDoc in, DataOutputStream JavaDoc out) {
22         this.in = in;
23         this.out = out;
24     }
25
26     OdbcTransfer writeBoolean(boolean x) throws IOException JavaDoc {
27         writeInt(x ? 1 : 0);
28         return this;
29     }
30
31     OdbcTransfer writeOk() throws IOException JavaDoc {
32         writeBoolean(true);
33         return this;
34     }
35
36     boolean readBoolean() throws IOException JavaDoc {
37         return readInt() == 1;
38     }
39
40     OdbcTransfer writeByte(byte x) throws IOException JavaDoc {
41         out.write(x);
42         return this;
43     }
44
45     int readByte() throws IOException JavaDoc {
46         return in.read();
47     }
48
49     OdbcTransfer writeShort(short x) throws IOException JavaDoc {
50         return writeInt(x);
51     }
52
53     short readShort() throws IOException JavaDoc {
54         return (short) readInt();
55     }
56
57     OdbcTransfer writeInt(int i) throws IOException JavaDoc {
58         out.writeInt(i);
59         return this;
60     }
61
62     int readInt() throws IOException JavaDoc {
63         return in.readInt();
64     }
65
66     OdbcTransfer writeLong(long i) throws IOException JavaDoc {
67         out.writeLong(i);
68         return this;
69     }
70
71     long readLong() throws IOException JavaDoc {
72         return in.readLong();
73     }
74
75     OdbcTransfer writeFloat(float i) throws IOException JavaDoc {
76         out.writeFloat(i);
77         return this;
78     }
79
80     float readFloat() throws IOException JavaDoc {
81         return in.readFloat();
82     }
83
84     OdbcTransfer writeDouble(double i) throws IOException JavaDoc {
85         out.writeDouble(i);
86         return this;
87     }
88
89     double readDouble() throws IOException JavaDoc {
90         return in.readDouble();
91
92     }
93
94     OdbcTransfer writeString(String JavaDoc s) throws IOException JavaDoc {
95         if (s == null) {
96             out.writeInt(-1);
97         } else {
98             out.writeInt(s.length());
99             for(int i=0; i<s.length(); i++) {
100                 out.write(s.charAt(i));
101             }
102         }
103         return this;
104     }
105
106     String JavaDoc readString() throws IOException JavaDoc {
107         int len = in.readInt();
108         if (len == -1) {
109             return null;
110         }
111         char[] chars = new char[len];
112         for(int i=0; i<len; i++) {
113             chars[i] = (char)in.readByte();
114         }
115         return new String JavaDoc(chars);
116     }
117
118     OdbcTransfer writeDate(java.sql.Date JavaDoc x) throws IOException JavaDoc {
119         if (x == null) {
120             writeString(null);
121         } else {
122             writeString(x.toString());
123         }
124         return this;
125     }
126
127     OdbcTransfer writeTime(java.sql.Time JavaDoc x) throws IOException JavaDoc {
128         if (x == null) {
129             writeString(null);
130         } else {
131             writeString(x.toString());
132         }
133         return this;
134     }
135
136     OdbcTransfer writeTimestamp(java.sql.Timestamp JavaDoc x) throws IOException JavaDoc {
137         if (x == null) {
138             writeString(null);
139         } else {
140             writeString(x.toString());
141         }
142         return this;
143     }
144
145     java.sql.Date JavaDoc readDate() throws IOException JavaDoc {
146         String JavaDoc s = readString();
147         if (s == null) {
148             return null;
149         }
150         return java.sql.Date.valueOf(s);
151     }
152
153     java.sql.Time JavaDoc readTime() throws IOException JavaDoc {
154         String JavaDoc s = readString();
155         if (s == null) {
156             return null;
157         }
158         return java.sql.Time.valueOf(s);
159     }
160
161     java.sql.Timestamp JavaDoc readTimestamp() throws IOException JavaDoc {
162         String JavaDoc s = readString();
163         if (s == null) {
164             return null;
165         }
166         return java.sql.Timestamp.valueOf(s);
167     }
168
169     OdbcTransfer writeByteArray(byte[] data) throws IOException JavaDoc {
170         if (data == null) {
171             writeInt(-1);
172         } else {
173             writeInt(data.length);
174         }
175         out.write(data);
176
177         return this;
178     }
179
180     byte[] readByteArray() throws IOException JavaDoc {
181         int len = readInt();
182         if (len == -1) {
183             return null;
184         }
185
186         byte[] b = new byte[len];
187         in.readFully(b);
188         return b;
189
190     }
191
192     OdbcTransfer writeIntArray(int[] s) throws IOException JavaDoc {
193         if (s == null) {
194             writeInt(-1);
195         } else {
196             writeInt(s.length);
197             for (int i = 0; i < s.length; i++) {
198                 writeInt(s[i]);
199             }
200         }
201         return this;
202     }
203
204     int[] readIntArray() throws IOException JavaDoc {
205         int len = readInt();
206         if (len == -1) {
207             return null;
208         }
209         int[] s = new int[len];
210         for (int i = 0; i < len; i++) {
211             s[i] = readInt();
212         }
213         return s;
214     }
215
216     OdbcTransfer writeStringArray(String JavaDoc[] s) throws IOException JavaDoc {
217         if (s == null) {
218             writeInt(-1);
219         } else {
220             writeInt(s.length);
221             for (int i = 0; i < s.length; i++) {
222                 writeString(s[i]);
223             }
224         }
225         return this;
226     }
227
228
229     String JavaDoc[] readStringArray() throws IOException JavaDoc {
230         int len = readInt();
231         if (len == -1) {
232             return null;
233         }
234         String JavaDoc[] s = new String JavaDoc[len];
235         for (int i = 0; i < len; i++) {
236             s[i] = readString();
237         }
238         return s;
239     }
240
241     // buffer - cannot be null
242
OdbcTransfer writeBuffer(byte[] buffer) throws IOException JavaDoc {
243         out.write(buffer);
244         return this;
245     }
246
247 }
248
Popular Tags