Mercurial > projects > dwt-win
annotate dwt/dwthelper/BufferedInputStream.d @ 330:f980ea238e72
it is possible for "this.istr" to be null before this method is called, so we should check for it before attempting to close the stream
author | Craig Slusher <cslush@gmail.com> |
---|---|
date | Mon, 26 Jan 2009 10:46:51 -0500 |
parents | 184ab53b7785 |
children |
rev | line source |
---|---|
0 | 1 /** |
2 * Authors: Frank Benoit <keinfarbton@googlemail.com> | |
3 */ | |
4 module dwt.dwthelper.BufferedInputStream; | |
5 | |
6 import dwt.dwthelper.InputStream; | |
7 import dwt.dwthelper.utils; | |
8 | |
9 import tango.core.Exception; | |
10 | |
11 public class BufferedInputStream : dwt.dwthelper.InputStream.InputStream { | |
12 | |
13 alias dwt.dwthelper.InputStream.InputStream.read read; | |
14 | |
15 private const int defaultSize = 8192; | |
16 protected byte[] buf; | |
17 protected int count = 0; /// The index one greater than the index of the last valid byte in the buffer. | |
18 protected int pos = 0; /// The current position in the buffer. | |
19 protected int markpos = (-1); | |
20 protected int marklimit; | |
21 dwt.dwthelper.InputStream.InputStream istr; | |
22 | |
23 public this ( dwt.dwthelper.InputStream.InputStream istr ){ | |
24 this( istr, defaultSize ); | |
25 } | |
26 | |
27 public this ( dwt.dwthelper.InputStream.InputStream istr, int size ){ | |
28 this.istr = istr; | |
29 if( size <= 0 ){ | |
30 throw new IllegalArgumentException( "Buffer size <= 0" ); | |
31 } | |
32 buf.length = size; | |
33 } | |
34 | |
35 private InputStream getAndCheckIstr(){ | |
36 InputStream res = istr; | |
37 if( res is null ){ | |
38 throw new IOException( "Stream closed" ); | |
39 } | |
40 return res; | |
41 } | |
42 private byte[] getAndCheckBuf(){ | |
43 byte[] res = buf; | |
44 if( res is null ){ | |
45 throw new IOException( "Stream closed" ); | |
46 } | |
47 return res; | |
48 } | |
49 private void fill(){ | |
50 assert( pos == count ); | |
51 pos = 0; | |
52 count = 0; | |
197 | 53 count = getAndCheckIstr().read( buf ); |
0 | 54 if( count < 0 ){ |
55 count = 0; | |
56 istr = null; | |
57 } | |
58 } | |
59 public synchronized int read(){ | |
60 if( pos >= count ){ | |
61 fill(); | |
62 if( pos >= count ){ | |
63 return -1; | |
64 } | |
65 } | |
66 return getAndCheckBuf()[pos++] & 0xFF; | |
67 } | |
68 | |
69 public synchronized int read( byte[] b, int off, int len ){ | |
70 return super.read( b, off, len ); | |
71 } | |
72 | |
73 public synchronized long skip( long n ){ | |
74 return this.istr.skip(n); | |
75 } | |
76 | |
77 public synchronized int available(){ | |
78 int istr_avail = 0; | |
79 if( istr !is null ){ | |
80 istr_avail = istr.available(); | |
81 } | |
82 return istr_avail + (count - pos); | |
83 } | |
84 | |
85 public synchronized void mark( int readlimit ){ | |
86 implMissing( __FILE__, __LINE__ ); | |
87 this.istr.mark( readlimit ); | |
88 } | |
89 | |
90 public synchronized void reset(){ | |
91 implMissing( __FILE__, __LINE__ ); | |
92 this.istr.reset(); | |
93 } | |
94 | |
95 public bool markSupported(){ | |
96 implMissing( __FILE__, __LINE__ ); | |
97 return false; | |
98 } | |
99 | |
100 public void close(){ | |
330
f980ea238e72
it is possible for "this.istr" to be null before this method is called, so we should check for it before attempting to close the stream
Craig Slusher <cslush@gmail.com>
parents:
197
diff
changeset
|
101 if (this.istr !is null) { |
f980ea238e72
it is possible for "this.istr" to be null before this method is called, so we should check for it before attempting to close the stream
Craig Slusher <cslush@gmail.com>
parents:
197
diff
changeset
|
102 this.istr.close(); |
f980ea238e72
it is possible for "this.istr" to be null before this method is called, so we should check for it before attempting to close the stream
Craig Slusher <cslush@gmail.com>
parents:
197
diff
changeset
|
103 } |
0 | 104 } |
105 | |
106 | |
107 } | |
108 | |
109 |