1 5 package org.h2.server; 6 7 import java.io.DataInputStream ; 8 import java.io.DataOutputStream ; 9 import java.io.IOException ; 10 11 14 15 public class OdbcTransfer { 16 static final int BUFFER_SIZE = 1024; 17 18 private DataInputStream in; 19 private DataOutputStream out; 20 21 OdbcTransfer(DataInputStream in, DataOutputStream out) { 22 this.in = in; 23 this.out = out; 24 } 25 26 OdbcTransfer writeBoolean(boolean x) throws IOException { 27 writeInt(x ? 1 : 0); 28 return this; 29 } 30 31 OdbcTransfer writeOk() throws IOException { 32 writeBoolean(true); 33 return this; 34 } 35 36 boolean readBoolean() throws IOException { 37 return readInt() == 1; 38 } 39 40 OdbcTransfer writeByte(byte x) throws IOException { 41 out.write(x); 42 return this; 43 } 44 45 int readByte() throws IOException { 46 return in.read(); 47 } 48 49 OdbcTransfer writeShort(short x) throws IOException { 50 return writeInt(x); 51 } 52 53 short readShort() throws IOException { 54 return (short) readInt(); 55 } 56 57 OdbcTransfer writeInt(int i) throws IOException { 58 out.writeInt(i); 59 return this; 60 } 61 62 int readInt() throws IOException { 63 return in.readInt(); 64 } 65 66 OdbcTransfer writeLong(long i) throws IOException { 67 out.writeLong(i); 68 return this; 69 } 70 71 long readLong() throws IOException { 72 return in.readLong(); 73 } 74 75 OdbcTransfer writeFloat(float i) throws IOException { 76 out.writeFloat(i); 77 return this; 78 } 79 80 float readFloat() throws IOException { 81 return in.readFloat(); 82 } 83 84 OdbcTransfer writeDouble(double i) throws IOException { 85 out.writeDouble(i); 86 return this; 87 } 88 89 double readDouble() throws IOException { 90 return in.readDouble(); 91 92 } 93 94 OdbcTransfer writeString(String s) throws IOException { 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 readString() throws IOException { 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 (chars); 116 } 117 118 OdbcTransfer writeDate(java.sql.Date x) throws IOException { 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 x) throws IOException { 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 x) throws IOException { 137 if (x == null) { 138 writeString(null); 139 } else { 140 writeString(x.toString()); 141 } 142 return this; 143 } 144 145 java.sql.Date readDate() throws IOException { 146 String s = readString(); 147 if (s == null) { 148 return null; 149 } 150 return java.sql.Date.valueOf(s); 151 } 152 153 java.sql.Time readTime() throws IOException { 154 String s = readString(); 155 if (s == null) { 156 return null; 157 } 158 return java.sql.Time.valueOf(s); 159 } 160 161 java.sql.Timestamp readTimestamp() throws IOException { 162 String 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 { 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 { 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 { 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 { 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 [] s) throws IOException { 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 [] readStringArray() throws IOException { 230 int len = readInt(); 231 if (len == -1) { 232 return null; 233 } 234 String [] s = new String [len]; 235 for (int i = 0; i < len; i++) { 236 s[i] = readString(); 237 } 238 return s; 239 } 240 241 OdbcTransfer writeBuffer(byte[] buffer) throws IOException { 243 out.write(buffer); 244 return this; 245 } 246 247 } 248 | Popular Tags |