1 9 package org.ozoneDB.xml.util; 10 11 import java.io.*; 12 13 14 16 public final class ChunkOutputStream 17 extends OutputStream 18 implements Serializable { 20 23 public final static byte STATE_NORMAL = 0; 24 25 26 29 public final static byte STATE_OVERFLOW = 1; 30 31 32 35 public boolean endFlag = false; 36 37 38 41 protected byte[] buf; 42 43 44 47 protected int count; 48 49 50 53 private byte state; 54 55 56 59 private int initialSize; 60 61 62 65 private int increase; 66 67 68 72 public ChunkOutputStream() { 73 this( 1000, 100 ); 74 } 75 76 77 85 public ChunkOutputStream( int size ) { 86 this( size, 100 ); 87 } 88 89 90 101 public ChunkOutputStream( int size, int increase ) { 102 if (size < 0) { 103 throw new IllegalArgumentException ( "Negative initial size: " + size ); 104 } 105 if (increase <= 0) { 106 throw new IllegalArgumentException ( "Increase less or equal 0: " + size ); 107 } 108 this.buf = new byte[size + increase]; 109 this.increase = increase; 110 this.initialSize = size; 111 this.state = ChunkOutputStream.STATE_NORMAL; 112 this.count = 0; 113 } 114 115 116 public void writeObject(ObjectOutputStream out) throws IOException { 117 out.writeBoolean( endFlag ); 118 out.writeInt( this.increase ); 119 out.writeInt( this.initialSize ); 120 out.writeByte( this.state ); 121 out.writeInt( this.count ); 122 out.write( this.buf, 0, this.count ); 123 } 124 125 126 public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 127 this.endFlag = in.readBoolean(); 128 this.increase = in.readInt(); 129 this.initialSize = in.readInt(); 130 this.state = in.readByte(); 131 this.count = in.readInt(); 132 this.buf = new byte[this.initialSize + this.increase]; 133 in.read( this.buf, 0, this.count ); 134 } 135 136 137 139 140 145 public final void write( int b ) { 146 147 int newcount = this.count + 1; 148 if (newcount > this.buf.length) { 149 byte[] newbuf = new byte[this.count + this.increase]; 150 System.arraycopy( this.buf, 0, newbuf, 0, this.count ); 151 this.buf = newbuf; 152 this.state = ChunkOutputStream.STATE_OVERFLOW; 153 } 154 this.buf[this.count] = (byte)b; 155 this.count = newcount; 156 } 157 158 159 167 public final void write( byte[] b, int off, int len ) { 168 if (off < 0 || off > b.length || len < 0 || off + len > b.length || off + len < 0) { 169 throw new IndexOutOfBoundsException (); 170 } else { 171 if (len == 0) { 172 return; 173 } 174 } 175 176 int newCount = this.count + len; 177 if (newCount > this.buf.length) { 178 int newLength = this.buf.length + increase; 179 byte[] newbuf = new byte[(newLength >= newCount) ? newLength: newCount]; 180 System.arraycopy( this.buf, 0, newbuf, 0, this.count ); 181 this.buf = newbuf; 182 this.state = ChunkOutputStream.STATE_OVERFLOW; 183 } 184 185 System.arraycopy( b, off, this.buf, this.count, len ); 186 this.count = newCount; 187 } 188 189 190 198 public final void writeTo( OutputStream out ) throws IOException { 199 out.write( this.buf, 0, this.count ); 200 } 201 202 203 211 public final void reset() { 212 this.buf = new byte[initialSize + increase]; 213 this.state = ChunkOutputStream.STATE_NORMAL; 214 this.count = 0; 215 } 216 217 218 221 public final int size() { 222 return this.count; 223 } 224 225 226 234 public final byte[] toByteArray() { 235 byte[] newbuf = new byte[this.count]; 236 System.arraycopy( this.buf, 0, newbuf, 0, this.count ); 237 return newbuf; 238 } 239 240 241 244 public final byte getState() { 245 return this.state; 246 } 247 248 249 252 public final void setEndFlag() { 253 this.endFlag = true; 254 } 255 256 257 260 public final boolean getEndFlag() { 261 return this.endFlag; 262 } 263 } 264 | Popular Tags |