View Javadoc

1   /*
2    * $Id: ByteBufferOutputStream.java,v 1.1 2006/09/14 14:34:52 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.lang;
5   
6   import java.io.IOException;
7   import java.io.OutputStream;
8   
9   /**
10   * 
11   * @author Java team (latest modification by $Author: oeuillot $)
12   * @version $Revision: 1.1 $ $Date: 2006/09/14 14:34:52 $
13   */
14  public final class ByteBufferOutputStream extends OutputStream {
15      private static final String REVISION = "$Revision: 1.1 $";
16  
17      /**
18       * The buffer where data is stored.
19       */
20      private byte buf[];
21  
22      /**
23       * The number of valid bytes in the buffer.
24       */
25      private int count;
26  
27      /**
28       * Creates a new byte array output stream, with a buffer capacity of the
29       * specified size, in bytes.
30       * 
31       * @param size
32       *            the initial size.
33       * @exception IllegalArgumentException
34       *                if size is negative.
35       */
36      public ByteBufferOutputStream(int size) {
37          if (size < 0) {
38              throw new IllegalArgumentException("Negative initial size: " + size);
39          }
40          buf = new byte[size];
41      }
42  
43      /**
44       * Writes the specified byte to this byte array output stream.
45       * 
46       * @param b
47       *            the byte to be written.
48       */
49      public void write(int b) {
50          int newcount = count + 1;
51          if (newcount > buf.length) {
52              byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
53              System.arraycopy(buf, 0, newbuf, 0, count);
54              buf = newbuf;
55          }
56          buf[count] = (byte) b;
57          count = newcount;
58      }
59  
60      /**
61       * Writes <code>len</code> bytes from the specified byte array starting at
62       * offset <code>off</code> to this byte array output stream.
63       * 
64       * @param b
65       *            the data.
66       * @param off
67       *            the start offset in the data.
68       * @param len
69       *            the number of bytes to write.
70       */
71      public void write(byte b[], int off, int len) {
72          if ((off < 0) || (off > b.length) || (len < 0)
73                  || ((off + len) > b.length) || ((off + len) < 0)) {
74              throw new IndexOutOfBoundsException();
75          }
76  
77          if (len == 0) {
78              return;
79          }
80          int newcount = count + len;
81          if (newcount > buf.length) {
82              byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
83              System.arraycopy(buf, 0, newbuf, 0, count);
84              buf = newbuf;
85          }
86          System.arraycopy(b, off, buf, count, len);
87          count = newcount;
88      }
89  
90      /**
91       * Writes the complete contents of this byte array output stream to the
92       * specified output stream argument, as if by calling the output stream's
93       * write method using <code>out.write(buf, 0, count)</code>.
94       * 
95       * @param out
96       *            the output stream to which to write the data.
97       * @exception IOException
98       *                if an I/O error occurs.
99       */
100     public void writeTo(OutputStream out) throws IOException {
101         out.write(buf, 0, count);
102     }
103 
104     /**
105      * Resets the <code>count</code> field of this byte array output stream to
106      * zero, so that all currently accumulated output in the ouput stream is
107      * discarded. The output stream can be used again, reusing the already
108      * allocated buffer space.
109      * 
110      * @see java.io.ByteBufferInputStream#count
111      */
112     public void reset() {
113         count = 0;
114     }
115 
116     /**
117      * Creates a newly allocated byte array. Its size is the current size of
118      * this output stream and the valid contents of the buffer have been copied
119      * into it.
120      * 
121      * @return the current contents of this output stream, as a byte array.
122      * @see java.io.ByteArrayOutputStream#size()
123      */
124     public byte[] toByteArray() {
125         byte newbuf[] = new byte[count];
126         System.arraycopy(buf, 0, newbuf, 0, count);
127         return newbuf;
128     }
129 
130     /**
131      * Returns the current size of the buffer.
132      * 
133      * @return the value of the <code>count</code> field, which is the number
134      *         of valid bytes in this output stream.
135      * @see java.io.ByteArrayOutputStream#count
136      */
137     public int size() {
138         return count;
139     }
140 
141     public void flush() {
142     }
143 
144     public void close() {
145     }
146 }