1 /** 2 * Authors: Frank Benoit <keinfarbton@googlemail.com> 3 */ 4 module java.io.BufferedInputStream; 5 6 import java.io.InputStream; 7 import java.lang.all; 8 9 public class BufferedInputStream : java.io.InputStream.InputStream { 10 11 alias java.io.InputStream.InputStream.read read; 12 13 private enum int defaultSize = 8192; 14 protected byte[] buf; 15 protected int count = 0; /// The index one greater than the index of the last valid byte in the buffer. 16 protected int pos = 0; /// The current position in the buffer. 17 protected int markpos = (-1); 18 protected int marklimit; 19 java.io.InputStream.InputStream istr; 20 21 public this ( java.io.InputStream.InputStream istr ){ 22 this( istr, defaultSize ); 23 } 24 25 public this ( java.io.InputStream.InputStream istr, int size ){ 26 this.istr = istr; 27 if( size <= 0 ){ 28 throw new IllegalArgumentException( "Buffer size <= 0" ); 29 } 30 buf.length = size; 31 } 32 33 private InputStream getAndCheckIstr(){ 34 InputStream res = istr; 35 if( res is null ){ 36 throw new IOException( "Stream closed" ); 37 } 38 return res; 39 } 40 private byte[] getAndCheckBuf(){ 41 byte[] res = buf; 42 if( res is null ){ 43 throw new IOException( "Stream closed" ); 44 } 45 return res; 46 } 47 private void fill(){ 48 assert( pos == count ); 49 pos = 0; 50 count = 0; 51 count = getAndCheckIstr().read( buf ); 52 if( count < 0 ){ 53 count = 0; 54 istr = null; 55 } 56 } 57 override 58 public int read(){ 59 synchronized { 60 if( pos >= count ){ 61 fill(); 62 if( pos >= count ){ 63 return -1; 64 } 65 } 66 return getAndCheckBuf()[pos++] & 0xFF; 67 } 68 } 69 70 override 71 public ptrdiff_t read( byte[] b, ptrdiff_t off, ptrdiff_t len ){ 72 synchronized return super.read( b, off, len ); 73 } 74 75 override 76 public long skip( long n ){ 77 synchronized return this.istr.skip(n); 78 } 79 80 override 81 public ptrdiff_t available(){ 82 synchronized { 83 ptrdiff_t istr_avail = 0; 84 if( istr !is null ){ 85 istr_avail = istr.available(); 86 } 87 return istr_avail + (count - pos); 88 } 89 } 90 91 override 92 public synchronized void mark( int readlimit ){ 93 implMissing( __FILE__, __LINE__ ); 94 this.istr.mark( readlimit ); 95 } 96 97 override 98 public synchronized void reset(){ 99 implMissing( __FILE__, __LINE__ ); 100 this.istr.reset(); 101 } 102 103 override 104 public bool markSupported(){ 105 implMissing( __FILE__, __LINE__ ); 106 return false; 107 } 108 109 override 110 public void close(){ 111 if (this.istr !is null) { 112 this.istr.close(); 113 } 114 } 115 116 117 } 118 119