00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.util;
00011
00012 import java.io.IOException;
00013 import java.io.OutputStream;
00014 import java.io.UnsupportedEncodingException;
00015
00028 public class FastOutputStream extends OutputStream {
00029
00034 public static final int DEFAULT_INIT_SIZE = 100;
00035
00040 public static final int DEFAULT_BUMP_SIZE = 0;
00041
00042 private int len;
00043 private int bumpLen;
00044 private byte[] buf;
00045
00049 public FastOutputStream() {
00050
00051 initBuffer(DEFAULT_INIT_SIZE, DEFAULT_BUMP_SIZE);
00052 }
00053
00060 public FastOutputStream(int initialSize) {
00061
00062 initBuffer(initialSize, DEFAULT_BUMP_SIZE);
00063 }
00064
00072 public FastOutputStream(int initialSize, int bumpSize) {
00073
00074 initBuffer(initialSize, bumpSize);
00075 }
00076
00083 public FastOutputStream(byte[] buffer) {
00084
00085 buf = buffer;
00086 bumpLen = DEFAULT_BUMP_SIZE;
00087 }
00088
00099 public FastOutputStream(byte[] buffer, int bumpSize) {
00100
00101 buf = buffer;
00102 bumpLen = bumpSize;
00103 }
00104
00105 private void initBuffer(int bufferSize, int bumpLen) {
00106 buf = new byte[bufferSize];
00107 this.bumpLen = bumpLen;
00108 }
00109
00110
00111
00112 public int size() {
00113
00114 return len;
00115 }
00116
00117 public void reset() {
00118
00119 len = 0;
00120 }
00121
00122 public void write(int b) throws IOException {
00123
00124 writeFast(b);
00125 }
00126
00127 public void write(byte[] fromBuf) throws IOException {
00128
00129 writeFast(fromBuf);
00130 }
00131
00132 public void write(byte[] fromBuf, int offset, int length)
00133 throws IOException {
00134
00135 writeFast(fromBuf, offset, length);
00136 }
00137
00138 public void writeTo(OutputStream out) throws IOException {
00139
00140 out.write(buf, 0, len);
00141 }
00142
00143 public String toString() {
00144
00145 return new String(buf, 0, len);
00146 }
00147
00148 public String toString(String encoding)
00149 throws UnsupportedEncodingException {
00150
00151 return new String(buf, 0, len, encoding);
00152 }
00153
00154 public byte[] toByteArray() {
00155
00156 byte[] toBuf = new byte[len];
00157
00158 System.arraycopy(buf, 0, toBuf, 0, len);
00159
00160 return toBuf;
00161 }
00162
00163
00164
00170 public final void writeFast(int b) {
00171
00172 if (len + 1 > buf.length)
00173 bump(1);
00174
00175 buf[len++] = (byte) b;
00176 }
00177
00183 public final void writeFast(byte[] fromBuf) {
00184
00185 int needed = len + fromBuf.length - buf.length;
00186 if (needed > 0)
00187 bump(needed);
00188
00189 System.arraycopy(fromBuf, 0, buf, len, fromBuf.length);
00190 len += fromBuf.length;
00191 }
00192
00198 public final void writeFast(byte[] fromBuf, int offset, int length) {
00199
00200 int needed = len + length - buf.length;
00201 if (needed > 0)
00202 bump(needed);
00203
00204 System.arraycopy(fromBuf, offset, buf, len, length);
00205 len += length;
00206 }
00207
00213 public byte[] getBufferBytes() {
00214
00215 return buf;
00216 }
00217
00223 public int getBufferOffset() {
00224
00225 return 0;
00226 }
00227
00234 public int getBufferLength() {
00235
00236 return len;
00237 }
00238
00245 public void makeSpace(int sizeNeeded) {
00246
00247 int needed = len + sizeNeeded - buf.length;
00248 if (needed > 0)
00249 bump(needed);
00250 }
00251
00257 public void addSize(int sizeAdded) {
00258
00259 len += sizeAdded;
00260 }
00261
00262 private void bump(int needed) {
00263
00264
00265 int bump = (bumpLen > 0) ? bumpLen : buf.length;
00266
00267 byte[] toBuf = new byte[buf.length + needed + bump];
00268
00269 System.arraycopy(buf, 0, toBuf, 0, len);
00270
00271 buf = toBuf;
00272 }
00273 }