1 21 22 package org.apache.derby.iapi.services.io; 23 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 27 32 public final class NewByteArrayInputStream extends InputStream { 33 34 private byte[] data; 35 private int offset; 36 private int length; 37 38 public NewByteArrayInputStream(byte[] data) { 39 this(data, 0, data.length); 40 } 41 42 public NewByteArrayInputStream(byte[] data, int offset, int length) { 43 this.data = data; 44 this.offset = offset; 45 this.length = length; 46 } 47 48 51 public int read() throws IOException { 52 if (data == null) 53 throw new IOException (); 54 55 if (length == 0) 56 return -1; 58 length--; 59 60 return data[offset++] & 0xff ; 61 62 } 63 64 public int read(byte b[], int off, int len) throws IOException { 65 if (data == null) 66 throw new IOException (); 67 68 if (length == 0) 69 return -1; 70 71 if (len > length) 72 len = length; 73 74 System.arraycopy(data, offset, b, off, len); 75 offset += len; 76 length -= len; 77 return len; 78 } 79 80 public long skip(long count) throws IOException { 81 if (data == null) 82 throw new IOException (); 83 84 if (length == 0) 85 return -1; 86 87 if (count > length) 88 count = length; 89 90 offset += (int) count; 91 length -= (int) count; 92 93 return count; 94 95 } 96 97 public int available() throws IOException 98 { 99 if (data == null) 100 throw new IOException (); 101 102 return length; 103 } 104 105 public byte[] getData() { return data; } 106 107 public void close() 108 { 109 data = null; 110 } 111 112 } 113 | Popular Tags |