1 18 package org.apache.activemq.util; 19 20 import java.io.DataOutput ; 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.io.UTFDataFormatException ; 24 import org.apache.activemq.util.ByteSequence; 25 30 public final class DataByteArrayOutputStream extends OutputStream implements DataOutput { 31 private static final int DEFAULT_SIZE = 2048; 32 private byte buf[]; 33 private int pos; 34 35 41 public DataByteArrayOutputStream(int size){ 42 if(size<0){ 43 throw new IllegalArgumentException ("Invalid size: "+size); 44 } 45 buf=new byte[size]; 46 } 47 48 51 public DataByteArrayOutputStream(){ 52 this(DEFAULT_SIZE); 53 } 54 55 60 public void restart(int size){ 61 buf=new byte[size]; 62 pos=0; 63 } 64 65 68 public void restart(){ 69 restart(DEFAULT_SIZE); 70 } 71 72 76 public ByteSequence toByteSequence() { 77 return new ByteSequence(buf, 0, pos); 78 } 79 80 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 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 113 public byte[] getData(){ 114 return buf; 115 } 116 117 120 public void reset(){ 121 pos=0; 122 } 123 124 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 { 183 writeInt(Float.floatToIntBits(v)); 184 } 185 186 public void writeDouble(double v) throws IOException { 187 writeLong(Double.doubleToLongBits(v)); 188 } 189 190 public void writeBytes(String 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 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 str) throws IOException { 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 ("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 |