KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > DataByteArrayOutputStream


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.util;
19
20 import java.io.DataOutput JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.UTFDataFormatException JavaDoc;
24 import org.apache.activemq.util.ByteSequence;
25 /**
26  * Optimized ByteArrayOutputStream
27  *
28  * @version $Revision: 1.1.1.1 $
29  */

30 public final class DataByteArrayOutputStream extends OutputStream JavaDoc implements DataOutput JavaDoc{
31     private static final int DEFAULT_SIZE = 2048;
32     private byte buf[];
33     private int pos;
34
35     /**
36      * Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
37      *
38      * @param size the initial size.
39      * @exception IllegalArgumentException if size is negative.
40      */

41     public DataByteArrayOutputStream(int size){
42         if(size<0){
43             throw new IllegalArgumentException JavaDoc("Invalid size: "+size);
44         }
45         buf=new byte[size];
46     }
47
48     /**
49      * Creates a new byte array output stream.
50      */

51     public DataByteArrayOutputStream(){
52         this(DEFAULT_SIZE);
53     }
54
55     /**
56      * start using a fresh byte array
57      *
58      * @param size
59      */

60     public void restart(int size){
61         buf=new byte[size];
62         pos=0;
63     }
64     
65     /**
66      * start using a fresh byte array
67      */

68     public void restart(){
69        restart(DEFAULT_SIZE);
70     }
71     
72     /**
73      * Get a ByteSequence from the stream
74      * @return the byte sequence
75      */

76     public ByteSequence toByteSequence() {
77         return new ByteSequence(buf, 0, pos);
78     }
79
80     /**
81      * Writes the specified byte to this byte array output stream.
82      *
83      * @param b the byte to be written.
84      */

85     public void write(int b){
86         int newcount=pos+1;
87         ensureEnoughBuffer(newcount);
88         buf[pos]=(byte) b;
89         pos=newcount;
90     }
91
92     /**
93      * Writes <code>len</code> bytes from the specified byte array starting at offset <code>off</code> to this byte
94      * array output stream.
95      *
96      * @param b the data.
97      * @param off the start offset in the data.
98      * @param len the number of bytes to write.
99      */

100     public void write(byte b[],int off,int len){
101         if(len==0){
102             return;
103         }
104         int newcount=pos+len;
105         ensureEnoughBuffer(newcount);
106         System.arraycopy(b,off,buf,pos,len);
107         pos=newcount;
108     }
109
110     /**
111      * @return the underlying byte[] buffer
112      */

113     public byte[] getData(){
114         return buf;
115     }
116
117     /**
118      * reset the output stream
119      */

120     public void reset(){
121         pos=0;
122     }
123
124     /**
125      * Set the current position for writing
126      *
127      * @param offset
128      */

129     public void position(int offset){
130         ensureEnoughBuffer(offset);
131         pos=offset;
132     }
133     
134     public int size(){
135         return pos;
136     }
137     
138     
139
140     public void writeBoolean(boolean v){
141         ensureEnoughBuffer(pos + 1);
142         buf[pos++]=(byte) (v?1:0);
143     }
144
145     public void writeByte(int v){
146         ensureEnoughBuffer(pos + 1);
147         buf[pos++]=(byte) (v>>>0);
148     }
149
150     public void writeShort(int v){
151         ensureEnoughBuffer(pos + 2);
152         buf[pos++]=(byte) (v>>>8);
153         buf[pos++]=(byte) (v>>>0);
154     }
155
156     public void writeChar(int v){
157         ensureEnoughBuffer(pos + 2);
158         buf[pos++]=(byte) (v>>>8);
159         buf[pos++]=(byte) (v>>>0);
160     }
161
162     public void writeInt(int v){
163         ensureEnoughBuffer(pos + 4);
164         buf[pos++]=(byte) (v>>>24);
165         buf[pos++]=(byte) (v>>>16);
166         buf[pos++]=(byte) (v>>>8);
167         buf[pos++]=(byte) (v>>>0);
168     }
169
170     public void writeLong(long v){
171         ensureEnoughBuffer(pos + 8);
172         buf[pos++]=(byte) (v>>>56);
173         buf[pos++]=(byte) (v>>>48);
174         buf[pos++]=(byte) (v>>>40);
175         buf[pos++]=(byte) (v>>>32);
176         buf[pos++]=(byte) (v>>>24);
177         buf[pos++]=(byte) (v>>>16);
178         buf[pos++]=(byte) (v>>>8);
179         buf[pos++]=(byte) (v>>>0);
180     }
181
182     public void writeFloat(float v) throws IOException JavaDoc{
183         writeInt(Float.floatToIntBits(v));
184     }
185
186     public void writeDouble(double v) throws IOException JavaDoc{
187         writeLong(Double.doubleToLongBits(v));
188     }
189
190     public void writeBytes(String JavaDoc s){
191         int length=s.length();
192         for(int i=0;i<length;i++){
193             write((byte) s.charAt(i));
194         }
195     }
196
197     public void writeChars(String JavaDoc s){
198         int length=s.length();
199         for(int i=0;i<length;i++){
200             int c=s.charAt(i);
201             write((c>>>8)&0xFF);
202             write((c>>>0)&0xFF);
203         }
204     }
205
206     public void writeUTF(String JavaDoc str) throws IOException JavaDoc{
207         int strlen=str.length();
208         int encodedsize=0;
209         int c;
210         for(int i=0;i<strlen;i++){
211             c=str.charAt(i);
212             if((c>=0x0001)&&(c<=0x007F)){
213                 encodedsize++;
214             }else if(c>0x07FF){
215                 encodedsize+=3;
216             }else{
217                 encodedsize+=2;
218             }
219         }
220         if(encodedsize>65535)
221             throw new UTFDataFormatException JavaDoc("encoded string too long: "+encodedsize+" bytes");
222         ensureEnoughBuffer(pos + encodedsize+2);
223         writeShort(encodedsize);
224         int i=0;
225         for(i=0;i<strlen;i++){
226             c=str.charAt(i);
227             if(!((c>=0x0001)&&(c<=0x007F)))
228                 break;
229             buf[pos++]=(byte) c;
230         }
231         for(;i<strlen;i++){
232             c=str.charAt(i);
233             if((c>=0x0001)&&(c<=0x007F)){
234                 buf[pos++]=(byte) c;
235             }else if(c>0x07FF){
236                 buf[pos++]=(byte) (0xE0|((c>>12)&0x0F));
237                 buf[pos++]=(byte) (0x80|((c>>6)&0x3F));
238                 buf[pos++]=(byte) (0x80|((c>>0)&0x3F));
239             }else{
240                 buf[pos++]=(byte) (0xC0|((c>>6)&0x1F));
241                 buf[pos++]=(byte) (0x80|((c>>0)&0x3F));
242             }
243         }
244     }
245
246     private void ensureEnoughBuffer(int newcount){
247         if(newcount>buf.length){
248             byte newbuf[]=new byte[Math.max(buf.length<<1,newcount)];
249             System.arraycopy(buf,0,newbuf,0,pos);
250             buf=newbuf;
251         }
252     }
253 }
254
Popular Tags