View Javadoc

1   /*
2    * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted under the terms of either of the following
6    * Open Source licenses:
7    *
8    * The GNU General Public License, version 2, or any later version, as
9    * published by the Free Software Foundation
10   * (http://www.fsf.org/copyleft/gpl.html);
11   *
12   *  or
13   *
14   * The Semiotek Public License (http://webmacro.org/LICENSE.)
15   *
16   * This software is provided "as is", with NO WARRANTY, not even the
17   * implied warranties of fitness to purpose, or merchantability. You
18   * assume all risks and liabilities associated with its use.
19   *
20   * See www.webmacro.org for more information on the WebMacro project.
21   */
22  
23  
24  package org.webmacro.util;
25  
26  import java.io.IOException;
27  import java.io.OutputStream;
28  import java.io.UnsupportedEncodingException;
29  
30  /***
31   * An optimized implementation of a ByteBuffer stream.
32   */
33  final public class ByteBufferOutputStream extends OutputStream
34  {
35  
36      private byte[] _buf;
37      private int _pos;
38      private final int _initialSize;
39  
40      /***
41       * Create a new ByteBuffer with the specified capacity
42       */
43      public ByteBufferOutputStream (int size)
44      {
45          _buf = new byte[size];
46          _pos = 0;
47          _initialSize = size;
48      }
49  
50      /***
51       * Move the read point of the current buffer back to the beginning
52       * of the buffer.
53       */
54      public void reset ()
55      {
56          _pos = 0;
57      }
58  
59      public void write (int i)
60      {
61          write((byte) i);
62      }
63  
64      /***
65       * Copy an array of bytes on to the end of the buffer
66       */
67      public void write (byte[] b)
68      {
69          int len = b.length;
70          ensureCapacity(len);
71          System.arraycopy(b, 0, _buf, _pos, len);
72          _pos += len;
73      }
74  
75      /***
76       * Copy an array of bytes on to the end of the buffer
77       */
78      public void write (byte[] b, int offset, int len)
79      {
80          ensureCapacity(len);
81          System.arraycopy(b, 0, _buf, _pos, len);
82          _pos += len;
83      }
84  
85      /***
86       * Append a single byte
87       */
88      public void write (byte b)
89      {
90          ensureCapacity(1);
91          _buf[_pos] = b;
92          _pos++;
93      }
94  
95      /***
96       * Make sure the buffer contains space for len more bytes.
97       */
98      public final void ensureCapacity (int len)
99      {
100         if (_buf.length < _pos + len)
101         {
102             int blen = _buf.length;
103 
104             while (blen < _pos + len)
105             {
106                 blen *= 2;
107             }
108 
109             byte[] tmp = new byte[blen];
110             System.arraycopy(_buf, 0, tmp, 0, _pos);
111             _buf = tmp;
112         }
113     }
114 
115     /***
116      * How many bytes currently in the buffer
117      */
118     public int size ()
119     {
120         return _pos;
121     }
122 
123     /***
124      * Get the bytes in the buffer. Note that you are getting the
125      * live buffer. You also need to call size() to find out how
126      * many of these bytes are significant. If you just want a
127      * byte array call getBytes() instead--that will allocate a
128      * new one for you.
129      */
130     public byte[] getBuffer ()
131     {
132         return _buf;
133     }
134 
135     /***
136      * Allocate a new byte[] and fill it with the contents of the
137      * current byte buffer. If you want the live byte buffer instead
138      * of this newly allocated copy call getBuffer() instead.
139      */
140     public byte[] getBytes ()
141     {
142         byte[] ret = new byte[_pos];
143         System.arraycopy(_buf, 0, ret, 0, _pos);
144         return ret;
145     }
146 
147     /***
148      * Convert the bytes to a String using the default encoding
149      */
150     public String toString ()
151     {
152         return new String(_buf, 0, _pos);
153     }
154 
155     /***
156      * Convert the bytes to a String using the specified encodign
157      */
158     public String toString (String encoding) throws UnsupportedEncodingException
159     {
160         return new String(_buf, 0, _pos, encoding);
161     }
162 
163     /***
164      * Write the bytes to the specified output stream
165      */
166     public void writeTo (OutputStream out) throws IOException
167     {
168         out.write(_buf, 0, _pos);
169     }
170 
171     public static void main (String arg[])
172     {
173         try
174         {
175 
176             ByteBufferOutputStream bb = new ByteBufferOutputStream(5);
177 
178             bb.write((byte) 'T');
179             bb.write((byte) 'h');
180             bb.write((byte) 'i');
181             bb.write((byte) 's');
182             bb.write((byte) ' ');
183             bb.write((byte) 'i');
184             bb.write((byte) 's');
185             bb.write((byte) ' ');
186             bb.write((byte) 'a');
187             bb.write((byte) ' ');
188             bb.write((byte) 't');
189             bb.write((byte) 'e');
190             bb.write((byte) 's');
191             bb.write((byte) 't');
192             bb.write((byte) '\n');
193             bb.write("This is the second line of the byte buffer".getBytes());
194             bb.write((byte) '\n');
195             bb.write("This is the third line\n".getBytes());
196             bb.write("This is the fourth line\n".getBytes());
197             bb.write((byte) 'E');
198             bb.write((byte) 'N');
199             bb.write((byte) 'D');
200             bb.write((byte) '.');
201             bb.write((byte) '\n');
202 
203             System.out.println("Byte buffer as a string: [" + bb + "]");
204             System.out.print("Byte buffer written to a stream: [");
205             bb.writeTo(System.out);
206             System.out.print("]");
207             System.out.println("DONE");
208         }
209         catch (Exception e)
210         {
211             e.printStackTrace();
212         }
213     }
214 }
215