comparison base/src/java/io/BufferedInputStream.d @ 27:1bf55a6eb092

Renamed java tree to base
author Frank Benoit <benoit@tionex.de>
date Sat, 21 Mar 2009 11:33:57 +0100
parents java/src/java/io/BufferedInputStream.d@9b96950f2c3c
children 9f4c18c268b2
comparison
equal deleted inserted replaced
26:f589fc20a5f9 27:1bf55a6eb092
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 const 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 public synchronized int read(){
58 if( pos >= count ){
59 fill();
60 if( pos >= count ){
61 return -1;
62 }
63 }
64 return getAndCheckBuf()[pos++] & 0xFF;
65 }
66
67 public synchronized int read( byte[] b, int off, int len ){
68 return super.read( b, off, len );
69 }
70
71 public synchronized long skip( long n ){
72 return this.istr.skip(n);
73 }
74
75 public synchronized int available(){
76 int istr_avail = 0;
77 if( istr !is null ){
78 istr_avail = istr.available();
79 }
80 return istr_avail + (count - pos);
81 }
82
83 public synchronized void mark( int readlimit ){
84 implMissing( __FILE__, __LINE__ );
85 this.istr.mark( readlimit );
86 }
87
88 public synchronized void reset(){
89 implMissing( __FILE__, __LINE__ );
90 this.istr.reset();
91 }
92
93 public bool markSupported(){
94 implMissing( __FILE__, __LINE__ );
95 return false;
96 }
97
98 public void close(){
99 if (this.istr !is null) {
100 this.istr.close();
101 }
102 }
103
104
105 }
106
107