comparison dwt/dwthelper/FileInputStream.d @ 12:0c78fa47d476

helper classes
author Frank Benoit <benoit@tionex.de>
date Sun, 06 Jan 2008 19:36:29 +0100
parents
children 380bad9f6852
comparison
equal deleted inserted replaced
11:5f725d09c076 12:0c78fa47d476
1 /**
2 * Authors: Frank Benoit <keinfarbton@googlemail.com>
3 */
4 module dwt.dwthelper.FileInputStream;
5
6 import dwt.dwthelper.utils;
7 import dwt.dwthelper.File;
8 import dwt.dwthelper.InputStream;
9
10 import tango.io.FileConduit;
11 import tango.io.protocol.Reader;
12 import tango.core.Exception;
13 import tango.text.convert.Format;
14
15 public class FileInputStream : dwt.dwthelper.InputStream.InputStream {
16
17 alias dwt.dwthelper.InputStream.InputStream.read read;
18
19 private FileConduit conduit;
20 private ubyte[] buffer;
21 private int buf_pos;
22 private int buf_size;
23 private const int BUFFER_SIZE = 0x10000;
24 private bool eof;
25
26 public this ( char[] name ){
27 conduit = new FileConduit( name );
28 buffer = new ubyte[]( BUFFER_SIZE );
29 }
30
31 public this ( dwt.dwthelper.File.File file ){
32 implMissing( __FILE__, __LINE__ );
33 conduit = new FileConduit( file.getAbsolutePath(), FileConduit.ReadExisting );
34 buffer = new ubyte[]( BUFFER_SIZE );
35 }
36
37 public override int read(){
38 if( eof ){
39 return -1;
40 }
41 try{
42 if( buf_pos == buf_size ){
43 buf_pos = 0;
44 buf_size = conduit.input.read( buffer );
45 }
46 if( buf_size <= 0 ){
47 eof = true;
48 return -1;
49 }
50 assert( buf_pos < BUFFER_SIZE, Format( "{0} {1}", buf_pos, buf_size ) );
51 assert( buf_size <= BUFFER_SIZE );
52 int res = cast(int) buffer[ buf_pos ];
53 buf_pos++;
54 return res;
55 }
56 catch( IOException e ){
57 eof = true;
58 return -1;
59 }
60 }
61
62 public long skip( long n ){
63 implMissing( __FILE__, __LINE__ );
64 return 0L;
65 }
66
67 public int available(){
68 implMissing( __FILE__, __LINE__ );
69 return 0;
70 }
71
72 public override void close(){
73 conduit.close();
74 }
75 }
76
77