##### Coverage Report - org.webmacro.util.ByteBufferOutputStream

Classes in this File | Line Coverage | Branch Coverage | Complexity
ByteBufferOutputStream |
29% | 21/72
100% | 4/4
| 1.2142857142857142;1.214

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 |  |
39 |  |
         /**
40 |  |
          * Create a new ByteBuffer with the specified capacity
41 |  |
          */
42 |  |
         public ByteBufferOutputStream (int size)
43 | 1734 |
         {
44 | 1734 |
             _buf = new byte[size];
45 | 1734 |
             _pos = 0;
46 | 1734 |
         }
47 |  |
48 |  |
         /**
49 |  |
          * Move the read point of the current buffer back to the beginning
50 |  |
          * of the buffer.
51 |  |
          */
52 |  |
         public void reset ()
53 |  |
         {
54 | 1788 |
             _pos = 0;
55 | 1788 |
         }
56 |  |
57 |  |
         public void write (int i)
58 |  |
         {
59 | 0 |
             write((byte) i);
60 | 0 |
         }
61 |  |
62 |  |
         /**
63 |  |
          * Copy an array of bytes on to the end of the buffer
64 |  |
          */
65 |  |
         public void write (byte[] b)
66 |  |
         {
67 | 0 |
             int len = b.length;
68 | 0 |
             ensureCapacity(len);
69 | 0 |
             System.arraycopy(b, 0, _buf, _pos, len);
70 | 0 |
             _pos += len;
71 | 0 |
         }
72 |  |
73 |  |
         /**
74 |  |
          * Copy an array of bytes on to the end of the buffer
75 |  |
          */
76 |  |
         public void write (byte[] b, int offset, int len)
77 |  |
         {
78 | 233016 |
             ensureCapacity(len);
79 | 233016 |
             System.arraycopy(b, 0, _buf, _pos, len);
80 | 233016 |
             _pos += len;
81 | 233016 |
         }
82 |  |
83 |  |
         /**
84 |  |
          * Append a single byte
85 |  |
          */
86 |  |
         public void write (byte b)
87 |  |
         {
88 | 0 |
             ensureCapacity(1);
89 | 0 |
             _buf[_pos] = b;
90 | 0 |
             _pos++;
91 | 0 |
         }
92 |  |
93 |  |
         /**
94 |  |
          * Make sure the buffer contains space for len more bytes.
95 |  |
          */
96 |  |
         public final void ensureCapacity (int len)
97 |  |
         {
98 | 233016 |
             if (_buf.length < _pos + len)
99 |  |
             {
100 | 456 |
                 int blen = _buf.length;
101 |  |
102 | 912 |
                 while (blen < _pos + len)
103 |  |
                 {
104 | 456 |
                     blen *= 2;
105 |  |
                 }
106 |  |
107 | 456 |
                 byte[] tmp = new byte[blen];
108 | 456 |
                 System.arraycopy(_buf, 0, tmp, 0, _pos);
109 | 456 |
                 _buf = tmp;
110 |  |
             }
111 | 233016 |
         }
112 |  |
113 |  |
         /**
114 |  |
          * How many bytes currently in the buffer
115 |  |
          */
116 |  |
         public int size ()
117 |  |
         {
118 | 0 |
             return _pos;
119 |  |
         }
120 |  |
121 |  |
         /**
122 |  |
          * Get the bytes in the buffer. Note that you are getting the
123 |  |
          * live buffer. You also need to call size() to find out how
124 |  |
          * many of these bytes are significant. If you just want a
125 |  |
          * byte array call getBytes() instead--that will allocate a
126 |  |
          * new one for you.
127 |  |
          */
128 |  |
         public byte[] getBuffer ()
129 |  |
         {
130 | 0 |
             return _buf;
131 |  |
         }
132 |  |
133 |  |
         /**
134 |  |
          * Allocate a new byte[] and fill it with the contents of the
135 |  |
          * current byte buffer. If you want the live byte buffer instead
136 |  |
          * of this newly allocated copy call getBuffer() instead.
137 |  |
          */
138 |  |
         public byte[] getBytes ()
139 |  |
         {
140 | 0 |
             byte[] ret = new byte[_pos];
141 | 0 |
             System.arraycopy(_buf, 0, ret, 0, _pos);
142 | 0 |
             return ret;
143 |  |
         }
144 |  |
145 |  |
         /**
146 |  |
          * Convert the bytes to a String using the default encoding
147 |  |
          */
148 |  |
         public String toString ()
149 |  |
         {
150 | 0 |
             return new String(_buf, 0, _pos);
151 |  |
         }
152 |  |
153 |  |
         /**
154 |  |
          * Convert the bytes to a String using the specified encodign
155 |  |
          */
156 |  |
         public String toString (String encoding) throws UnsupportedEncodingException
157 |  |
         {
158 | 642 |
             return new String(_buf, 0, _pos, encoding);
159 |  |
         }
160 |  |
161 |  |
         /**
162 |  |
          * Write the bytes to the specified output stream
163 |  |
          */
164 |  |
         public void writeTo (OutputStream out) throws IOException
165 |  |
         {
166 | 1150 |
             out.write(_buf, 0, _pos);
167 | 1136 |
         }
168 |  |
169 |  |
         public static void main (String arg[])
170 |  |
         {
171 |  |
             try
172 |  |
             {
173 |  |
174 | 0 |
                 ByteBufferOutputStream bb = new ByteBufferOutputStream(5);
175 |  |
176 | 0 |
                 bb.write((byte) 'T');
177 | 0 |
                 bb.write((byte) 'h');
178 | 0 |
                 bb.write((byte) 'i');
179 | 0 |
                 bb.write((byte) 's');
180 | 0 |
                 bb.write((byte) ' ');
181 | 0 |
                 bb.write((byte) 'i');
182 | 0 |
                 bb.write((byte) 's');
183 | 0 |
                 bb.write((byte) ' ');
184 | 0 |
                 bb.write((byte) 'a');
185 | 0 |
                 bb.write((byte) ' ');
186 | 0 |
                 bb.write((byte) 't');
187 | 0 |
                 bb.write((byte) 'e');
188 | 0 |
                 bb.write((byte) 's');
189 | 0 |
                 bb.write((byte) 't');
190 | 0 |
                 bb.write((byte) '\n');
191 | 0 |
                 bb.write("This is the second line of the byte buffer".getBytes());
192 | 0 |
                 bb.write((byte) '\n');
193 | 0 |
                 bb.write("This is the third line\n".getBytes());
194 | 0 |
                 bb.write("This is the fourth line\n".getBytes());
195 | 0 |
                 bb.write((byte) 'E');
196 | 0 |
                 bb.write((byte) 'N');
197 | 0 |
                 bb.write((byte) 'D');
198 | 0 |
                 bb.write((byte) '.');
199 | 0 |
                 bb.write((byte) '\n');
200 |  |
201 | 0 |
                 System.out.println("Byte buffer as a string: [" + bb + "]");
202 | 0 |
                 System.out.print("Byte buffer written to a stream: [");
203 | 0 |
                 bb.writeTo(System.out);
204 | 0 |
                 System.out.print("]");
205 | 0 |
                 System.out.println("DONE");
206 |  |
             }
207 | 0 |
             catch (Exception e)
208 |  |
             {
209 | 0 |
                 e.printStackTrace();
210 | 0 |
             }
211 | 0 |
         }
212 |  |
     }
213 |  |