00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 #include <stdnet/config.h>
00037 #include <stdnet/io/FileInputStream>
00038 
00039 #include <memory.h>
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 namespace stdbase {
00048     namespace io {
00049 
00050 
00051 
00052 system::reference< FileInputStream > FileInputStream::
00053     create( system::StringRef name )
00054 {
00055     return FileInputStreamRef( new FileInputStream( name ) );
00056 }
00057     
00058 system::reference< FileInputStream > FileInputStream::
00059     create( const system::platform::FileDescriptor & fd)
00060 {
00061     return FileInputStreamRef( new FileInputStream( fd ) );
00062 }
00063 
00064 
00065 
00066 
00067 
00068 
00069 int FileInputStream::
00070     available() throw (IOException)
00071 {
00072     return length - position;
00073 }
00074     
00075 void FileInputStream::
00076     close() throw (IOException)
00077 {
00078     if ( input.isOpen() )
00079     {
00080         input.close();
00081     }
00082 }
00083 
00084 void FileInputStream::
00085     mark( int readlimit)
00086 {
00087 }
00088 
00089 void FileInputStream::
00090     reset() throw (IOException)
00091 {
00092     
00093     throw IOException();
00094 }
00095 
00096 
00097 
00098 
00099 
00100 bool FileInputStream::
00101     markSupported()
00102 {
00103     return false;
00104 }
00105 
00106 
00107 
00108 
00109 int FileInputStream::
00110     read( byte buffer[], int len)
00111     throw (IOException)
00112 {
00113     
00114     
00115 
00116     int read_len = 0;
00117 
00118     if ( length == 0 ||
00119          position == length &&
00120          ! input.eof() )
00121     {
00122         
00123         
00124         length = input.read(
00125                     (char*) data_buffer,
00126                     DATA_BUFFER_SIZE
00127                  );
00128         if ( length < 0 )
00129             throw IOException();
00130 
00131         position = 0;
00132     }
00133     
00134     if ( position == length &&
00135          input.eof() )
00136     {
00137         return 0;
00138     }
00139 
00140     memcpy( buffer, & data_buffer[ position ], ( length - position ) );
00141     read_len += length - position;
00142 
00143     
00144     length = position = 0;
00145 
00146     if ( read_len < len )
00147     {
00148         
00149         
00150         int result;
00151 
00152         result =    input.read(
00153                         (char*) & buffer[ read_len ],
00154                         len - read_len
00155                     );
00156 
00157         if ( result < 0 )
00158             throw IOException();
00159 
00160         read_len += result;
00161     }
00162 
00163     return read_len;
00164 }
00165         
00166 
00167 
00168 
00169 
00170 
00171 
00172 int FileInputStream::
00173     read() throw (IOException)
00174 {
00175     int val;
00176     
00177     if ( length == 0 ||
00178          position == length &&
00179          ! input.eof() )
00180     {
00181         
00182         
00183         length = input.read( (char*) data_buffer, DATA_BUFFER_SIZE);
00184         if ( length < 0 )
00185             throw IOException();
00186 
00187         position = 0;
00188     }
00189     
00190     
00191     
00192     if ( position == length ||
00193          input.eof() )
00194     {
00195         throw IOException();
00196     }
00197 
00198     val = (int) data_buffer[ position++ ]; 
00199     return val;
00200 }
00201 
00202 
00203 
00204 
00205 
00206 
00207 long FileInputStream::
00208     skip( long n ) throw (IOException)
00209 {
00210     long    skipped = 0;
00211 
00212     if ( ! input.isOpen() )
00213     {
00214         throw IOException();
00215     }
00216 
00217     try
00218     {
00219         while( skipped < n )
00220         {
00221             read();
00222             skipped++;
00223         }
00224     }
00225     catch( IOException & e)
00226     { e =  e;}
00227     
00228     return skipped;
00229 }
00230 
00234 
00239 FileInputStream::
00240 FileInputStream( ::stdbase::system::StringRef name)
00241     :   InputStream(),
00242         input(),
00243         position( 0 ),
00244         length( 0 )
00245 {
00246     int    len = name->length();
00247     char * n = new char [ len + 1];
00248     name->getChars( 0, len, n, 0);
00249 
00250     input.open( n, system::platform::FileDescriptor:: OPEN_READ_TEXT);
00251 
00252     delete [] n;
00253 }
00254 
00259 FileInputStream::
00260 FileInputStream( const ::stdbase::system::platform::FileDescriptor &fd)
00261     :   InputStream(),
00262         input( fd ),
00263         position( 0 ),
00264         length( 0 )
00265 {
00266 }
00267 
00268 FileInputStream::
00269 ~FileInputStream()
00270 {
00271     try {
00272         if ( input.isOpen() )
00273         {
00274             close();
00275         }
00276     }
00277     catch ( IOException &e)
00278     {
00279         e = e;
00280     }
00281 }
00282 
00283     } 
00284 } 
00285