KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > util > ChunkOutputStream


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: ChunkOutputStream.java,v 1.2 2002/09/18 06:54:18 per_nyfelt Exp $
8

9 package org.ozoneDB.xml.util;
10
11 import java.io.*;
12
13
14 /**
15  */

16 public final class ChunkOutputStream
17              extends OutputStream
18              implements Serializable { //Externalizable {
19

20     /**
21      * the output stream has not yet exceeded its initial size.
22      */

23     public final static byte STATE_NORMAL = 0;
24     
25     
26     /**
27      * the output stream has exceeded its initial size.
28      */

29     public final static byte STATE_OVERFLOW = 1;
30     
31     
32     /**
33      * true if this is the last chunk in a sequence of chunks, false otherwise.
34      */

35     public boolean endFlag = false;
36     
37     
38     /**
39      * The buffer where data is stored.
40      */

41     protected byte[] buf;
42     
43     
44     /**
45      * The number of valid bytes in the buffer.
46      */

47     protected int count;
48     
49     
50     /**
51      * the state of this output stream.
52      */

53     private byte state;
54     
55     
56     /**
57      * The initial size of this output stream
58      */

59     private int initialSize;
60     
61     
62     /**
63      * The amount of bytes by which the buffer will be increased if it overflows.
64      */

65     private int increase;
66     
67     
68     /**
69      * Creates a new byte array output stream. The buffer capacity is initially
70      * 1000 bytes. Its size increases by 100 bytes if necessary.
71      */

72     public ChunkOutputStream() {
73         this( 1000, 100 );
74     }
75     
76     
77     /**
78      * Creates a new byte array output stream, with a buffer capacity of
79      * the specified size in bytes. Its size increases by 100 bytes if necessary.
80      *
81      * @param size the initial size. exceeding this size causes the
82      * stream to enter state {@link #STATE_OVERFLOW}.
83      * @exception IllegalArgumentException if size is negative.
84      */

85     public ChunkOutputStream( int size ) {
86         this( size, 100 );
87     }
88     
89     
90     /**
91      * Creates a new byte array output stream, with a buffer capacity of
92      * the specified size in bytes. Its size increases by the specified increase
93      * in bytes if necessary.
94      *
95      * @param size the initial size. exceeding this size causes the
96      * stream to enter state {@link #STATE_OVERFLOW}.
97      * @param increase the amount of bytes by which the stream will be incre
98      * @exception IllegalArgumentException if <code>size</code> is negative or
99      * <code>increase</code> is less or equal 0.
100      */

101     public ChunkOutputStream( int size, int increase ) {
102         if (size < 0) {
103             throw new IllegalArgumentException JavaDoc( "Negative initial size: " + size );
104         }
105         if (increase <= 0) {
106             throw new IllegalArgumentException JavaDoc( "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 JavaDoc {
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     // output stream only methods
138

139     
140     /**
141      * Writes the specified byte to this byte array output stream.
142      *
143      * @param b the byte to be written.
144      */

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     /**
160      * Writes <code>len</code> bytes from the specified byte array
161      * starting at offset <code>off</code> to this byte array output stream.
162      *
163      * @param b the data.
164      * @param off the start offset in the data.
165      * @param len the number of bytes to write.
166      */

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 JavaDoc();
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     /**
191      * Writes the complete contents of this byte array output stream to
192      * the specified output stream argument, as if by calling the output
193      * stream's write method using <code>out.write(buf, 0, count)</code>.
194      *
195      * @param out the output stream to which to write the data.
196      * @exception IOException if an I/O error occurs.
197      */

198     public final void writeTo( OutputStream out ) throws IOException {
199         out.write( this.buf, 0, this.count );
200     }
201     
202     
203     /**
204      * Resets the <code>count</code> field of this byte array output
205      * stream to zero, so that all currently accumulated output in the
206      * ouput stream is discarded. The output stream can be used again,
207      * reusing the already allocated buffer space.
208      *
209      * @see java.io.ByteArrayInputStream#count
210      */

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     /**
219      * The number of valid bytes in this stream.
220      */

221     public final int size() {
222         return this.count;
223     }
224     
225     
226     /**
227      * Creates a newly allocated byte array. Its size is the current
228      * size of this output stream and the valid contents of the buffer
229      * have been copied into it.
230      *
231      * @return the current contents of this output stream, as a byte array.
232      * @see java.io.ByteArrayOutputStream#size()
233      */

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     /**
242      * @return the state of this output stream
243      */

244     public final byte getState() {
245         return this.state;
246     }
247     
248     
249     /**
250      * marks this chunk as the last in a sequence of chunks
251      */

252     public final void setEndFlag() {
253         this.endFlag = true;
254     }
255     
256     
257     /**
258      * @return true, if this chunk is the last in a sequence of chunks
259      */

260     public final boolean getEndFlag() {
261         return this.endFlag;
262     }
263 }
264
Popular Tags