1 9 package org.ozoneDB.xml.util; 10 11 import java.io.InputStream ; 12 import java.io.IOException ; 13 import java.io.Serializable ; 14 15 16 18 public final class ChunkInputStream extends InputStream implements Serializable { 19 20 25 protected byte[] buf; 26 27 42 protected int pos; 43 44 45 53 public ChunkInputStream( byte[] data ) { 54 this.buf = data; 55 this.pos = 0; 56 } 57 58 59 69 public synchronized int read() throws IOException { 70 if (this.pos >= this.buf.length) { 71 return -1; 72 } 73 74 return this.buf[this.pos++] & 0xff; 75 } 76 77 78 89 public synchronized final int read( byte[] b, int off, int len ) throws IOException { 90 if (this.pos >= this.buf.length) { 91 return -1; 92 } 93 94 if (off < 0 || off > b.length || len < 0 || off + len > b.length || off + len < 0) { 95 throw new IndexOutOfBoundsException (); 96 } else { 97 if (len == 0) { 98 return 0; 99 } 100 } 101 102 int toRead = this.buf.length - this.pos; 103 len = toRead <= len ? toRead : len; 104 System.arraycopy( this.buf, this.pos, b, off, len ); 105 106 this.pos += len; 107 108 return len; 109 } 110 111 112 120 public synchronized final long skip( long n ) throws IOException { 121 int toRead = this.buf.length - this.pos; 122 n = toRead <= n ? toRead : n; 123 pos += n; 124 return n; 125 } 126 127 128 136 public synchronized final int available() throws IOException { 137 return this.buf.length - pos; 138 } 139 140 141 149 public final boolean markSupported() { 150 return false; 151 } 152 153 154 160 public synchronized final void close() throws IOException { 161 this.buf = null; 162 } 163 164 165 169 public final void setBuffer( byte[] buffer ) { 170 this.buf = buffer; 171 this.pos = 0; 172 } 173 174 } 175 | Popular Tags |