1 30 31 32 package org.hsqldb.rowio; 33 34 import java.io.UnsupportedEncodingException ; 35 import java.math.BigDecimal ; 36 import java.sql.Date ; 37 import java.sql.Time ; 38 import java.sql.Timestamp ; 39 40 import org.hsqldb.CachedRow; 41 import org.hsqldb.Trace; 42 import org.hsqldb.Types; 43 import org.hsqldb.lib.StringConverter; 44 import org.hsqldb.persist.TextCache; 45 import org.hsqldb.types.Binary; 46 import org.hsqldb.types.JavaObject; 47 48 55 public class RowOutputText extends RowOutputBase { 56 57 protected String fieldSep; 58 protected String varSep; 59 protected String longvarSep; 60 private boolean fieldSepEnd; 61 private boolean varSepEnd; 62 private boolean longvarSepEnd; 63 private String nextSep = ""; 64 private boolean nextSepEnd; 65 protected boolean allQuoted; 66 private String encoding; 67 68 public RowOutputText(String fieldSep, String varSep, String longvarSep, 69 boolean allQuoted, String encoding) { 70 71 super(); 72 73 initTextDatabaseRowOutput(fieldSep, varSep, longvarSep, allQuoted, 74 encoding); 75 } 76 77 private void initTextDatabaseRowOutput(String fieldSep, String varSep, 78 String longvarSep, 79 boolean allQuoted, 80 String encoding) { 81 82 if (fieldSep.endsWith("\n")) { 84 fieldSepEnd = true; 85 fieldSep = fieldSep.substring(0, fieldSep.length() - 1); 86 } 87 88 if (varSep.endsWith("\n")) { 89 varSepEnd = true; 90 varSep = varSep.substring(0, varSep.length() - 1); 91 } 92 93 if (longvarSep.endsWith("\n")) { 94 longvarSepEnd = true; 95 longvarSep = longvarSep.substring(0, longvarSep.length() - 1); 96 } 97 98 this.fieldSep = fieldSep; 99 this.varSep = varSep; 100 this.longvarSep = longvarSep; 101 this.allQuoted = allQuoted; 102 this.encoding = encoding; 103 } 104 105 public void writeEnd() { 106 107 if (nextSepEnd) { 109 writeBytes(nextSep); 110 } 111 112 writeBytes(TextCache.NL); 113 } 114 115 public void writeSize(int size) { 116 117 nextSep = ""; 119 nextSepEnd = false; 120 } 121 122 public void writeType(int type) { 123 124 } 126 127 public void writeString(String s) { 128 129 s = checkConvertString(s, fieldSep); 130 131 if (s == null) { 133 return; 134 } 135 136 byte[] bytes = getBytes(s); 138 139 write(bytes, 0, bytes.length); 140 141 nextSep = fieldSep; 142 nextSepEnd = fieldSepEnd; 143 } 144 145 protected void writeVarString(String s) { 146 147 s = checkConvertString(s, varSep); 148 149 if (s == null) { 150 return; 151 } 152 153 byte[] bytes = getBytes(s); 155 156 write(bytes, 0, bytes.length); 157 158 nextSep = varSep; 159 nextSepEnd = varSepEnd; 160 } 161 162 protected void writeLongVarString(String s) { 163 164 s = checkConvertString(s, longvarSep); 165 166 if (s == null) { 167 return; 168 } 169 170 byte[] bytes = getBytes(s); 172 173 write(bytes, 0, bytes.length); 174 175 nextSep = longvarSep; 176 nextSepEnd = longvarSepEnd; 177 } 178 179 protected String checkConvertString(String s, String sep) { 180 181 if (s.indexOf('\n') != -1 || s.indexOf('\r') != -1) { 182 throw new IllegalArgumentException ( 183 Trace.getMessage(Trace.TEXT_STRING_HAS_NEWLINE)); 184 } else if (s.indexOf(sep) != -1) { 185 return null; 186 } 187 188 return s; 189 } 190 191 private byte[] getBytes(String s) { 192 193 byte[] bytes = null; 194 195 try { 196 bytes = s.getBytes(encoding); 197 } catch (UnsupportedEncodingException e) { 198 bytes = s.getBytes(); 199 } 200 201 return bytes; 202 } 203 204 protected void writeByteArray(byte[] b) { 205 206 ensureRoom(b.length * 2); 207 StringConverter.writeHex(this.getBuffer(), count, b); 208 209 count += b.length * 2; 210 } 211 212 public void writeShortData(short i) { 213 writeIntData(i); 214 } 215 216 public void writeIntData(int i) { 217 218 writeBytes(Integer.toString(i)); 219 220 nextSep = fieldSep; 221 nextSepEnd = fieldSepEnd; 222 } 223 224 public void writeIntData(int i, int position) { 225 throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION, 226 "RowInputText"); 227 } 228 229 public void writeLongData(long i) { 230 throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION, 231 "RowInputText"); 232 } 233 234 protected void writeFieldType(int type) { 236 237 writeBytes(nextSep); 238 239 switch (type) { 240 241 case Types.VARCHAR : 242 case Types.VARCHAR_IGNORECASE : 243 nextSep = varSep; 244 nextSepEnd = varSepEnd; 245 break; 246 247 case Types.LONGVARCHAR : 248 nextSep = longvarSep; 249 nextSepEnd = longvarSepEnd; 250 break; 251 252 default : 253 nextSep = fieldSep; 254 nextSepEnd = fieldSepEnd; 255 break; 256 } 257 } 258 259 protected void writeNull(int type) { 260 writeFieldType(type); 261 } 262 263 protected void writeChar(String s, int t) { 264 265 switch (t) { 266 267 case Types.CHAR : 268 writeString(s); 269 270 return; 271 272 case Types.VARCHAR : 273 case Types.VARCHAR_IGNORECASE : 274 writeVarString(s); 275 276 return; 277 278 case Types.LONGVARCHAR : 279 default : 280 writeLongVarString(s); 281 282 return; 283 } 284 } 285 286 protected void writeSmallint(Number o) { 287 writeString(o.toString()); 288 } 289 290 protected void writeInteger(Number o) { 291 writeString(o.toString()); 292 } 293 294 protected void writeBigint(Number o) { 295 writeString(o.toString()); 296 } 297 298 protected void writeReal(Double o, int type) { 299 writeString(o.toString()); 300 } 301 302 protected void writeDecimal(BigDecimal o) { 303 writeString(o.toString()); 304 } 305 306 protected void writeBit(Boolean o) { 307 writeString(o.toString()); 308 } 309 310 protected void writeDate(Date o) { 311 writeString(o.toString()); 312 } 313 314 protected void writeTime(Time o) { 315 writeString(o.toString()); 316 } 317 318 protected void writeTimestamp(Timestamp o) { 319 writeString(o.toString()); 320 } 321 322 protected void writeOther(JavaObject o) { 323 324 byte[] ba = o.getBytes(); 325 326 writeByteArray(ba); 327 } 328 329 protected void writeBinary(Binary o, int t) { 330 writeByteArray(o.getBytes()); 331 } 332 333 public int getSize(CachedRow r) { 334 335 reset(); 336 337 try { 338 writeSize(0); 339 writeData(r.getData(), r.getTable()); 340 writeEnd(); 341 } catch (Exception e) { 342 reset(); 343 344 } 346 347 int rowsize = size(); 348 349 reset(); 350 351 return rowsize; 352 } 353 } 354 | Popular Tags |