1 /** 2 * Authors: Frank Benoit <keinfarbton@googlemail.com> 3 */ 4 module java.io.FileInputStream; 5 6 import java.lang.all; 7 import java.io.File; 8 import java.io.InputStream; 9 10 version(Tango){ 11 import TangoFile = tango.io.device.File; 12 } else { // Phobos 13 static import std.stdio; 14 } 15 16 public class FileInputStream : java.io.InputStream.InputStream { 17 18 alias java.io.InputStream.InputStream.read read; 19 20 version(Tango){ 21 private TangoFile.File conduit; 22 } else { // Phobos 23 private std.stdio.File conduit; 24 } 25 private ubyte[] buffer; 26 private int buf_pos; 27 private int buf_size; 28 private enum int BUFFER_SIZE = 0x10000; 29 private bool eof; 30 31 public this ( String name ){ 32 version(Tango){ 33 conduit = new TangoFile.File( name ); 34 } else { // Phobos 35 conduit = std.stdio.File( name, "rb" ); 36 } 37 buffer = new ubyte[]( BUFFER_SIZE ); 38 } 39 40 public this ( java.io.File.File file ){ 41 implMissing( __FILE__, __LINE__ ); 42 version(Tango){ 43 conduit = new TangoFile.File( file.getAbsolutePath(), TangoFile.File.ReadExisting ); 44 } else { // Phobos 45 conduit = std.stdio.File( file.getAbsolutePath(), "rb" ); 46 } 47 buffer = new ubyte[]( BUFFER_SIZE ); 48 } 49 50 public override int read(){ 51 version(Tango){ 52 if( eof ){ 53 return -1; 54 } 55 try{ 56 if( buf_pos == buf_size ){ 57 buf_pos = 0; 58 buf_size = conduit.input.read( buffer ); 59 } 60 if( buf_size <= 0 ){ 61 eof = true; 62 return -1; 63 } 64 assert( buf_pos < BUFFER_SIZE, Format( "{0} {1}", buf_pos, buf_size ) ); 65 assert( buf_size <= BUFFER_SIZE ); 66 int res = cast(int) buffer[ buf_pos ]; 67 buf_pos++; 68 return res; 69 } 70 catch( IOException e ){ 71 eof = true; 72 return -1; 73 } 74 } else { // Phobos 75 if( eof ){ 76 return -1; 77 } 78 try{ 79 if( buf_pos == buf_size ){ 80 buf_pos = 0; 81 buf_size = cast(int)/*64bit*/conduit.rawRead( buffer ).length; 82 } 83 if( buf_size <= 0 ){ 84 eof = true; 85 return -1; 86 } 87 assert( buf_pos < BUFFER_SIZE, Format( "{} {}", buf_pos, buf_size ) ); 88 assert( buf_size <= BUFFER_SIZE ); 89 int res = cast(int) buffer[ buf_pos ]; 90 buf_pos++; 91 return res; 92 } 93 catch( IOException e ){ 94 eof = true; 95 return -1; 96 } 97 } 98 } 99 100 override 101 public long skip( long n ){ 102 implMissing( __FILE__, __LINE__ ); 103 return 0L; 104 } 105 106 override 107 public ptrdiff_t available(){ 108 implMissing( __FILE__, __LINE__ ); 109 return 0; 110 } 111 112 public override void close(){ 113 conduit.close(); 114 } 115 } 116 117