1 18 package org.apache.activemq.util; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 23 24 28 public class ByteArrayInputStream extends InputStream { 29 30 byte buffer[]; 31 int limit; 32 int pos; 33 int mark; 34 35 public ByteArrayInputStream(byte data[]) { 36 this(data, 0, data.length); 37 } 38 39 public ByteArrayInputStream(ByteSequence sequence) { 40 this(sequence.getData(), sequence.getOffset(), sequence.getLength()); 41 } 42 43 public ByteArrayInputStream(byte data[], int offset, int size) { 44 this.buffer = data; 45 this.mark= this.pos = offset; 46 this.limit = offset+size; 47 } 48 49 public int read() throws IOException { 50 if( pos < limit ) 51 return buffer[pos++] & 0xff; 52 else 53 return -1; 54 } 55 56 public int read(byte[] b) throws IOException { 57 return read(b, 0, b.length); 58 } 59 60 public int read(byte b[], int off, int len) { 61 if (pos < limit) { 62 len = Math.min(len, limit-pos); 63 if (len > 0) { 64 System.arraycopy(buffer, pos, b, off, len); 65 pos += len; 66 } 67 return len; 68 } else { 69 return -1; 70 } 71 } 72 73 public long skip(long len) throws IOException { 74 if (pos < limit) { 75 len = Math.min(len, limit-pos); 76 if (len > 0) { 77 pos += len; 78 } 79 return len; 80 } else { 81 return -1; 82 } 83 } 84 85 public int available() { 86 return limit - pos; 87 } 88 89 public boolean markSupported() { 90 return true; 91 } 92 93 public void mark(int markpos) { 94 mark = pos; 95 } 96 97 public void reset() { 98 pos = mark; 99 } 100 } 101 | Popular Tags |