1 package com.quadcap.sql.io; 2 3 40 41 import java.io.*; 42 43 import java.io.DataOutput ; 44 import java.io.IOException ; 45 import java.io.OutputStream ; 46 47 import com.quadcap.util.Debug; 48 import com.quadcap.util.Util; 49 50 59 public class DataOutputStream extends OutputStream implements DataOutput { 60 static final int MAX = 4096; 61 OutputStream out; 62 byte[] buf = new byte[MAX]; 63 byte[] tbuf = new byte[16]; 64 Object [] bufs = null; 65 int bufsCnt = 0; 66 int pos = 0; 67 int siz = -1; 68 int count = 0; 69 70 public DataOutputStream(OutputStream out) { 71 this.out = out; 72 } 73 74 public void reset(OutputStream out) { 75 this.out = out; 76 bufsCnt = 0; 77 pos = 0; 78 count = 0; 79 siz = -1; 80 if (bufs != null && bufs.length > 0) { 81 buf = (byte[])bufs[0]; 82 } 83 } 84 85 final Object [] resize(Object [] array, int size) { 86 Object [] narray = new Object [size]; 87 System.arraycopy(array, 0, narray, 0, array.length); 88 return narray; 89 } 90 91 public final void writeBuf() throws IOException { 92 if (trace) Debug.println("writeBuf(), buf = " + buf + ", bufsCnt = " + 94 bufsCnt); 95 97 if (out != null) { 98 if (trace) Debug.println("-----------------------------write:\n " + 100 Util.strBytes(buf, 0, MAX)); 101 out.write(buf, 0, MAX); 103 count += MAX; 104 } else { 105 if (bufs == null) { 106 bufs = new Object [8]; 107 } else if (bufsCnt+1 >= bufs.length) { 108 bufs = resize(bufs, bufsCnt * 2); 109 } 110 bufs[bufsCnt++] = buf; 111 buf = (byte[])bufs[bufsCnt]; 112 if (buf == null) { 113 buf = new byte[MAX]; 114 bufs[bufsCnt] = buf; 115 } 116 } 117 pos = 0; 118 } 119 120 public final void write(int c) throws IOException { 121 if (trace) Debug.println("[" + pos + "]: write(" + c + ")"); 123 if (pos >= MAX) { 125 writeBuf(); 126 } 127 buf[pos++] = (byte)c; 128 } 129 130 public final void write(byte[] b) throws IOException { 131 write(b, 0, b.length); 132 } 133 134 public final void write(byte[] b, int off, int len) throws IOException { 135 if (trace) { 137 Debug.println("[" + pos + "]: write(" + 138 Util.strBytes(b, off, len) + ")"); 139 } 140 if (out != null && len > MAX) { 142 flush(); 143 out.write(b, off, len); 144 count += len; 145 return; 146 } 147 int npos = pos + len; 148 while (npos >= MAX) { 149 final int slen = MAX - pos; 150 System.arraycopy(b, off, buf, pos, slen); 151 writeBuf(); 152 len -= slen; 153 off += slen; 154 npos = pos + len; 155 } 156 if (len > 0) { 157 System.arraycopy(b, off, buf, pos, len); 158 pos = npos; 159 } 160 } 161 162 public final void writeBoolean(boolean v) throws IOException { 163 write(v ? 1 : 0); 164 } 165 166 public final void writeByte(int v) throws IOException { 167 if (trace) Debug.println("writeByte(" + v + ")"); 169 if (pos >= MAX) writeBuf(); 171 buf[pos++] = (byte)v; 172 } 173 174 public void writeLong(long v) throws IOException { 176 while (true) { 177 if (pos >= MAX) writeBuf(); 178 if ((v & 0xffffffffffffff80L) != 0) { 179 buf[pos++] = (byte)(0x80 + (v & 0x7f)); 180 v >>>= 7; 181 } else { 182 buf[pos++] = (byte)(v & 0x7f); 183 break; 184 } 185 } 186 } 187 public void writeInt(int v) throws IOException { 188 if (trace) Debug.println("[" + pos + "].writeInt(" + v + ")"); 190 while (true) { 192 if (pos >= MAX) writeBuf(); 193 if ((v & 0xffffff80) != 0) { 194 buf[pos++] = (byte)(0x80 + (v & 0x7f)); 195 v >>>= 7; 196 } else { 197 buf[pos++] = (byte)(v & 0x7f); 198 break; 199 } 200 } 201 } 202 203 public void writeShort(int v) throws IOException { 204 writeInt(v); 205 } 206 public void writeChar(int v) throws IOException { 207 writeInt(v); 208 } 209 210 259 public final void writeFloat(float v) throws IOException { 260 writeInt(Float.floatToIntBits(v)); 261 } 262 263 public final void writeDouble(double v) throws IOException { 264 writeLong(Double.doubleToLongBits(v)); 265 } 266 267 public final void writeBytes(String s) throws IOException { 268 if (trace) Debug.println("writeBytes(" + s + ")"); 270 int len = s.length(); 272 int off = 0; 273 while (len + pos >= MAX) { 274 int slen = MAX - pos; 275 s.getBytes(off, off + slen, buf, pos); 276 writeBuf(); 277 off += slen; 278 len -= slen; 279 } 280 if (len > 0) { 281 s.getBytes(off, len, buf, pos); 282 pos += len; 283 } 284 } 285 286 public final void writeChars(String s) throws IOException { 287 if (trace) Debug.println("writeChars(" + s + ")"); 289 final int len = s.length(); 291 for (int i = 0 ; i < len ; i++) { 292 int v = s.charAt(i); 293 writeChar(v); 294 } 295 } 296 297 public final void writeUTF(String str) throws IOException { 298 throw new IOException ("UTF write not implemented"); 299 } 300 301 public final void flush() throws IOException { 302 if (out != null) { 303 if (trace) Debug.println("flush, pos = " + pos); 305 if (pos > 0) { 307 if (trace) Debug.println("flush(" + pos + "): " + 309 Util.strBytes(buf, 0, pos)); 310 out.write(buf, 0, pos); 312 count += pos; 313 pos = 0; 314 } 315 out.flush(); 316 } 317 } 318 319 public final void close() throws IOException { 320 flush(); 321 if (out != null) out.close(); 322 } 323 324 public int size() { 325 int s = bufsCnt * MAX + pos; 326 if (siz >= 0) { 327 if (s > siz) siz = -1; 328 else s = siz; 329 } 330 return s; 331 } 332 333 public final void setCount(int count) { 334 this.count = count; 335 } 336 337 public final int getCount() { 338 if (out == null) { 339 return count; 340 } else { 341 return count + size(); 342 } 343 } 344 345 public final void setPosition(int p) { 346 if (trace) Debug.println("setPosition(" + p + ")"); 348 int s = size(); 350 if (p < s && s > siz) siz = s; 351 bufsCnt = p / MAX; 352 pos = p % MAX; 353 if (bufs != null) { 354 buf = (byte[])bufs[bufsCnt]; 355 } 356 } 357 358 public final int getPosition() { 359 return size(); 360 } 361 362 public byte[] toByteArray() { 363 int len = size(); 364 setPosition(len); 365 byte[] ret = new byte[len]; 366 int p = 0; 367 for (int i = 0; i < bufsCnt; i++) { 368 byte[] b = (byte[])bufs[i]; 369 System.arraycopy(b, 0, ret, p, Math.min(MAX, len)); 370 p += MAX; 371 len -= MAX; 372 } 373 if (buf != null) { 374 System.arraycopy(buf, 0, ret, p, pos); 375 } 376 return ret; 377 } 378 379 public static void test1() throws IOException { 381 FileOutputStream fos = new FileOutputStream("dos.out"); 382 ObjectOutputStream oos = new ObjectOutputStream(fos); 383 byte[] bufx = new byte[2000]; 384 for (int i = 0; i < bufx.length; i++) { 385 bufx[i] = (byte)(i & 0xff); 386 } 387 int cnt = 0; 388 for (int i = 1; i < bufx.length; i++) { 389 oos.write(bufx, 0, i); 390 cnt += i; 391 if (cnt != oos.getCount()) throw new RuntimeException ("bad count, i = " + i); 392 oos.flush(); 393 394 } 395 oos.flush(); 396 fos.close(); 397 398 FileInputStream fis = new FileInputStream("dos.out"); 399 BufferedInputStream bis = new BufferedInputStream(fis); 400 for (int i = 1; i < bufx.length; i++) { 401 bis.read(bufx, 0, i); 402 for (int j = 0; j < i; j++) { 403 if ((bufx[j] & 0xff)!= (j&0xff)) { 404 throw new RuntimeException ("bad, i = " + i + ", j = " + j); 405 } 406 } 407 } 408 fis.close(); 409 } 410 411 412 413 public static void main(String args[]) { 414 try { 415 test1(); 428 } catch (Throwable t) { 429 Debug.print(t); 430 } 431 } 432 433 boolean trace = false; 434 public void setTrace(boolean t) { 435 this.trace = t; 436 } 437 } 439 | Popular Tags |