1 8 9 package com.sleepycat.util; 10 11 import java.io.IOException ; 12 import java.io.OutputStream ; 13 import java.io.UnsupportedEncodingException ; 14 15 27 public class FastOutputStream extends OutputStream { 28 29 33 public static final int DEFAULT_INIT_SIZE = 100; 34 35 39 public static final int DEFAULT_BUMP_SIZE = 0; 40 41 private int len; 42 private int bumpLen; 43 private byte[] buf; 44 45 48 private static byte[] ZERO_LENGTH_BYTE_ARRAY = new byte[0]; 49 50 53 public FastOutputStream() { 54 55 initBuffer(DEFAULT_INIT_SIZE, DEFAULT_BUMP_SIZE); 56 } 57 58 64 public FastOutputStream(int initialSize) { 65 66 initBuffer(initialSize, DEFAULT_BUMP_SIZE); 67 } 68 69 76 public FastOutputStream(int initialSize, int bumpSize) { 77 78 initBuffer(initialSize, bumpSize); 79 } 80 81 87 public FastOutputStream(byte[] buffer) { 88 89 buf = buffer; 90 bumpLen = DEFAULT_BUMP_SIZE; 91 } 92 93 103 public FastOutputStream(byte[] buffer, int bumpSize) { 104 105 buf = buffer; 106 bumpLen = bumpSize; 107 } 108 109 private void initBuffer(int bufferSize, int bumpLen) { 110 buf = new byte[bufferSize]; 111 this.bumpLen = bumpLen; 112 } 113 114 116 public int size() { 117 118 return len; 119 } 120 121 public void reset() { 122 123 len = 0; 124 } 125 126 public void write(int b) throws IOException { 127 128 writeFast(b); 129 } 130 131 public void write(byte[] fromBuf) throws IOException { 132 133 writeFast(fromBuf); 134 } 135 136 public void write(byte[] fromBuf, int offset, int length) 137 throws IOException { 138 139 writeFast(fromBuf, offset, length); 140 } 141 142 public void writeTo(OutputStream out) throws IOException { 143 144 out.write(buf, 0, len); 145 } 146 147 public String toString() { 148 149 return new String (buf, 0, len); 150 } 151 152 public String toString(String encoding) 153 throws UnsupportedEncodingException { 154 155 return new String (buf, 0, len, encoding); 156 } 157 158 public byte[] toByteArray() { 159 160 if (len == 0) { 161 return ZERO_LENGTH_BYTE_ARRAY; 162 } else { 163 byte[] toBuf = new byte[len]; 164 System.arraycopy(buf, 0, toBuf, 0, len); 165 166 return toBuf; 167 } 168 } 169 170 172 177 public final void writeFast(int b) { 178 179 if (len + 1 > buf.length) 180 bump(1); 181 182 buf[len++] = (byte) b; 183 } 184 185 190 public final void writeFast(byte[] fromBuf) { 191 192 int needed = len + fromBuf.length - buf.length; 193 if (needed > 0) 194 bump(needed); 195 196 System.arraycopy(fromBuf, 0, buf, len, fromBuf.length); 197 len += fromBuf.length; 198 } 199 200 205 public final void writeFast(byte[] fromBuf, int offset, int length) { 206 207 int needed = len + length - buf.length; 208 if (needed > 0) 209 bump(needed); 210 211 System.arraycopy(fromBuf, offset, buf, len, length); 212 len += length; 213 } 214 215 220 public byte[] getBufferBytes() { 221 222 return buf; 223 } 224 225 230 public int getBufferOffset() { 231 232 return 0; 233 } 234 235 241 public int getBufferLength() { 242 243 return len; 244 } 245 246 252 public void makeSpace(int sizeNeeded) { 253 254 int needed = len + sizeNeeded - buf.length; 255 if (needed > 0) 256 bump(needed); 257 } 258 259 264 public void addSize(int sizeAdded) { 265 266 len += sizeAdded; 267 } 268 269 private void bump(int needed) { 270 271 272 int bump = (bumpLen > 0) ? bumpLen : buf.length; 273 274 byte[] toBuf = new byte[buf.length + needed + bump]; 275 276 System.arraycopy(buf, 0, toBuf, 0, len); 277 278 buf = toBuf; 279 } 280 } 281 | Popular Tags |