diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwt/dwthelper/FileInputStream.d	Sun Jan 06 19:36:29 2008 +0100
@@ -0,0 +1,77 @@
+/**
+ * Authors: Frank Benoit <keinfarbton@googlemail.com>
+ */
+module dwt.dwthelper.FileInputStream;
+
+import dwt.dwthelper.utils;
+import dwt.dwthelper.File;
+import dwt.dwthelper.InputStream;
+
+import tango.io.FileConduit;
+import tango.io.protocol.Reader;
+import tango.core.Exception;
+import tango.text.convert.Format;
+
+public class FileInputStream : dwt.dwthelper.InputStream.InputStream {
+
+    alias dwt.dwthelper.InputStream.InputStream.read read;
+
+    private FileConduit conduit;
+    private ubyte[] buffer;
+    private int buf_pos;
+    private int buf_size;
+    private const int BUFFER_SIZE = 0x10000;
+    private bool eof;
+
+    public this ( char[] name ){
+        conduit = new FileConduit( name );
+        buffer = new ubyte[]( BUFFER_SIZE );
+    }
+
+    public this ( dwt.dwthelper.File.File file ){
+        implMissing( __FILE__, __LINE__ );
+        conduit = new FileConduit( file.getAbsolutePath(), FileConduit.ReadExisting );
+        buffer = new ubyte[]( BUFFER_SIZE );
+    }
+
+    public override int read(){
+        if( eof ){
+            return -1;
+        }
+        try{
+            if( buf_pos == buf_size ){
+                buf_pos = 0;
+                buf_size = conduit.input.read( buffer );
+            }
+            if( buf_size <= 0 ){
+                eof = true;
+                return -1;
+            }
+            assert( buf_pos < BUFFER_SIZE, Format( "{0} {1}", buf_pos, buf_size ) );
+            assert( buf_size <= BUFFER_SIZE );
+            int res = cast(int) buffer[ buf_pos ];
+            buf_pos++;
+            return res;
+        }
+        catch( IOException e ){
+            eof = true;
+            return -1;
+        }
+    }
+
+    public long skip( long n ){
+        implMissing( __FILE__, __LINE__ );
+        return 0L;
+    }
+
+    public int available(){
+        implMissing( __FILE__, __LINE__ );
+        return 0;
+    }
+
+    public override void close(){
+        conduit.close();
+    }
+}
+
+