1 18 package org.apache.activemq.util; 19 20 import java.io.DataInput ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.UTFDataFormatException ; 24 import org.apache.activemq.util.ByteSequence; 25 30 public final class DataByteArrayInputStream extends InputStream implements DataInput { 31 private byte[] buf; 32 private int pos; 33 private int offset; 34 35 40 public DataByteArrayInputStream(byte buf[]){ 41 this.buf=buf; 42 this.pos=0; 43 this.offset = 0; 44 } 45 46 51 public DataByteArrayInputStream(ByteSequence sequence){ 52 this.buf=sequence.getData(); 53 this.offset=this.pos=sequence.getOffset(); 54 } 55 56 59 public DataByteArrayInputStream(){ 60 this(new byte[0]); 61 } 62 63 67 public int size(){ 68 return pos-offset; 69 } 70 71 74 public byte[] getRawData(){ 75 return buf; 76 } 77 78 83 public void restart(byte[] newBuff){ 84 buf=newBuff; 85 pos=0; 86 } 87 88 93 public void restart(ByteSequence sequence){ 94 this.buf=sequence.getData(); 95 this.pos=sequence.getOffset(); 96 } 97 98 103 public void restart(int size){ 104 if(buf==null||buf.length<size){ 105 buf=new byte[size]; 106 } 107 restart(buf); 108 } 109 110 119 public int read(){ 120 return (pos<buf.length)?(buf[pos++]&0xff):-1; 121 } 122 123 132 public int read(byte b[],int off,int len){ 133 if(b==null){ 134 throw new NullPointerException (); 135 } 136 if(pos>=buf.length){ 137 return -1; 138 } 139 if(pos+len>buf.length){ 140 len=buf.length-pos; 141 } 142 if(len<=0){ 143 return 0; 144 } 145 System.arraycopy(buf,pos,b,off,len); 146 pos+=len; 147 return len; 148 } 149 150 153 public int available(){ 154 return buf.length-pos; 155 } 156 157 public void readFully(byte[] b){ 158 read(b,0,b.length); 159 } 160 161 public void readFully(byte[] b,int off,int len){ 162 read(b,off,len); 163 } 164 165 public int skipBytes(int n){ 166 if(pos+n>buf.length){ 167 n=buf.length-pos; 168 } 169 if(n<0){ 170 return 0; 171 } 172 pos+=n; 173 return n; 174 } 175 176 public boolean readBoolean(){ 177 return read()!=0; 178 } 179 180 public byte readByte(){ 181 return (byte) read(); 182 } 183 184 public int readUnsignedByte(){ 185 return read(); 186 } 187 188 public short readShort(){ 189 int ch1=read(); 190 int ch2=read(); 191 return (short) ((ch1<<8)+(ch2<<0)); 192 } 193 194 public int readUnsignedShort(){ 195 int ch1=read(); 196 int ch2=read(); 197 return ((ch1<<8)+(ch2<<0)); 198 } 199 200 public char readChar(){ 201 int ch1=read(); 202 int ch2=read(); 203 return (char) ((ch1<<8)+(ch2<<0)); 204 } 205 206 public int readInt(){ 207 int ch1=read(); 208 int ch2=read(); 209 int ch3=read(); 210 int ch4=read(); 211 return ((ch1<<24)+(ch2<<16)+(ch3<<8)+(ch4<<0)); 212 } 213 214 public long readLong(){ 215 return (((long) buf[pos++]<<56)+((long) (buf[pos++]&255)<<48)+((long) (buf[pos++]&255)<<40) 216 +((long) (buf[pos++]&255)<<32)+((long) (buf[pos++]&255)<<24)+((buf[pos++]&255)<<16) 217 +((buf[pos++]&255)<<8)+((buf[pos++]&255)<<0)); 218 } 219 220 public float readFloat() throws IOException { 221 return Float.intBitsToFloat(readInt()); 222 } 223 224 public double readDouble() throws IOException { 225 return Double.longBitsToDouble(readLong()); 226 } 227 228 public String readLine(){ 229 int start=pos; 230 while(pos<buf.length){ 231 int c=read(); 232 if(c=='\n'){ 233 break; 234 } 235 if(c=='\r'){ 236 c=read(); 237 if(c!='\n'&&c!=-1){ 238 pos--; 239 } 240 break; 241 } 242 } 243 return new String (buf,start,pos); 244 } 245 246 public String readUTF() throws IOException { 247 int length=readUnsignedShort(); 248 char[] characters=new char[length]; 249 int c,c2,c3; 250 int count=0; 251 int total=pos+length; 252 while(pos<total){ 253 c=(int) buf[pos]&0xff; 254 if(c>127) 255 break; 256 pos++; 257 characters[count++]=(char) c; 258 } 259 while(pos<total){ 260 c=(int) buf[pos]&0xff; 261 switch(c>>4){ 262 case 0: 263 case 1: 264 case 2: 265 case 3: 266 case 4: 267 case 5: 268 case 6: 269 case 7: 270 pos++; 271 characters[count++]=(char) c; 272 break; 273 case 12: 274 case 13: 275 pos+=2; 276 if(pos>length) 277 throw new UTFDataFormatException ("bad string"); 278 c2=(int) buf[pos-1]; 279 if((c2&0xC0)!=0x80) 280 throw new UTFDataFormatException ("bad string"); 281 characters[count++]=(char) (((c&0x1F)<<6)|(c2&0x3F)); 282 break; 283 case 14: 284 pos+=3; 285 if(pos>length) 286 throw new UTFDataFormatException ("bad string"); 287 c2=(int) buf[pos-2]; 288 c3=(int) buf[pos-1]; 289 if(((c2&0xC0)!=0x80)||((c3&0xC0)!=0x80)) 290 throw new UTFDataFormatException ("bad string"); 291 characters[count++]=(char) (((c&0x0F)<<12)|((c2&0x3F)<<6)|((c3&0x3F)<<0)); 292 break; 293 default: 294 throw new UTFDataFormatException ("bad string"); 295 } 296 } 297 return new String (characters,0,count); 298 } 299 } 300 | Popular Tags |