1 30 31 32 package org.hsqldb.lib; 33 34 import java.io.DataOutput ; 35 import java.io.IOException ; 36 import java.io.OutputStream ; 37 import java.io.UTFDataFormatException ; 38 import java.io.UnsupportedEncodingException ; 39 40 48 public class HsqlByteArrayOutputStream extends java.io.OutputStream 49 implements DataOutput { 50 51 protected byte[] buf; 52 protected int count; 53 54 public HsqlByteArrayOutputStream() { 55 this(128); 56 } 57 58 public HsqlByteArrayOutputStream(int size) { 59 60 if (size < 128) { 61 size = 128; 62 } 63 64 buf = new byte[size]; 65 } 66 67 public HsqlByteArrayOutputStream(byte[] buffer) { 68 buf = buffer; 69 } 70 71 public final void writeShort(int v) { 73 74 ensureRoom(2); 75 76 buf[count++] = (byte) (v >>> 8); 77 buf[count++] = (byte) v; 78 } 79 80 public final void writeInt(int v) { 81 82 if (count + 4 > buf.length) { 83 ensureRoom(4); 84 } 85 86 buf[count++] = (byte) (v >>> 24); 87 buf[count++] = (byte) (v >>> 16); 88 buf[count++] = (byte) (v >>> 8); 89 buf[count++] = (byte) v; 90 } 91 92 public final void writeLong(long v) { 93 writeInt((int) (v >>> 32)); 94 writeInt((int) v); 95 } 96 97 public final void writeBytes(String s) { 98 99 int len = s.length(); 100 101 ensureRoom(len); 102 103 for (int i = 0; i < len; i++) { 104 buf[count++] = (byte) s.charAt(i); 105 } 106 } 107 108 public final void writeFloat(float v) { 109 writeInt(Float.floatToIntBits(v)); 110 } 111 112 public final void writeDouble(double v) { 113 writeLong(Double.doubleToLongBits(v)); 114 } 115 116 public void writeBoolean(boolean v) throws IOException { 117 118 ensureRoom(1); 119 120 buf[count++] = (byte) (v ? 1 121 : 0); 122 } 123 124 public void writeByte(int v) throws IOException { 125 126 ensureRoom(1); 127 128 buf[count++] = (byte) (v); 129 } 130 131 public void writeChar(int v) throws IOException { 132 133 ensureRoom(2); 134 135 buf[count++] = (byte) (v >>> 8); 136 buf[count++] = (byte) v; 137 } 138 139 public void writeChars(String s) throws IOException { 140 141 int len = s.length(); 142 143 ensureRoom(len * 2); 144 145 for (int i = 0; i < len; i++) { 146 int v = s.charAt(i); 147 148 buf[count++] = (byte) (v >>> 8); 149 buf[count++] = (byte) v; 150 } 151 } 152 153 public void writeUTF(String str) throws IOException { 154 155 int len = str.length(); 156 157 if (len > 0xffff) { 158 throw new UTFDataFormatException (); 159 } 160 161 ensureRoom(len * 3 + 2); 162 163 int initpos = count; 165 166 count += 2; 167 168 StringConverter.writeUTF(str, this); 169 170 int bytecount = count - initpos - 2; 171 172 if (bytecount > 0xffff) { 173 count = initpos; 174 175 throw new UTFDataFormatException (); 176 } 177 178 buf[initpos++] = (byte) (bytecount >>> 8); 179 buf[initpos] = (byte) bytecount; 180 } 181 182 185 public void flush() throws java.io.IOException { 186 super.flush(); 187 } 188 189 public void write(int b) { 191 192 ensureRoom(1); 193 194 buf[count++] = (byte) b; 195 } 196 197 public void write(byte[] b) { 198 write(b, 0, b.length); 199 } 200 201 public void write(byte[] b, int off, int len) { 202 203 ensureRoom(len); 204 System.arraycopy(b, off, buf, count, len); 205 206 count += len; 207 } 208 209 public void writeTo(OutputStream out) throws IOException { 210 out.write(buf, 0, count); 211 } 212 213 public void reset() { 214 count = 0; 215 } 216 217 public byte[] toByteArray() { 218 219 byte[] newbuf = new byte[count]; 220 221 System.arraycopy(buf, 0, newbuf, 0, count); 222 223 return newbuf; 224 } 225 226 public int size() { 227 return count; 228 } 229 230 public String toString() { 231 return new String (buf, 0, count); 232 } 233 234 public String toString(String enc) throws UnsupportedEncodingException { 235 return new String (buf, 0, count, enc); 236 } 237 238 public void close() throws IOException {} 239 240 public void fill(int b, int len) { 242 243 ensureRoom(len); 244 245 for (int i = 0; i < len; i++) { 246 buf[count++] = (byte) b; 247 } 248 } 249 250 public byte[] getBuffer() { 251 return this.buf; 252 } 253 254 protected void ensureRoom(int extra) { 255 256 int newcount = count + extra; 257 int newsize = buf.length; 258 259 if (newcount > newsize) { 260 while (newcount > newsize) { 261 newsize *= 2; 262 } 263 264 byte[] newbuf = new byte[newsize]; 265 266 System.arraycopy(buf, 0, newbuf, 0, count); 267 268 buf = newbuf; 269 } 270 } 271 272 protected void reset(int newSize) { 273 274 count = 0; 275 276 if (newSize > buf.length) { 277 buf = new byte[newSize]; 278 } 279 } 280 } 281 | Popular Tags |