Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

FileInputStream.cpp

00001 /*
00002  * NOTE: "zlib/libpng" style License
00003  *
00004  *                 ----=== s t d n e t ===----
00005  *
00006  * Copyright (c) 2002 Warwick Molloy   w-molloy@users.sf.net
00007  *
00008  * Copyright (c) 2002 Stdnet Community
00009  * HTTP://Sourceforge.net/projects/stdnet
00010  *
00011  * All references to "software" refer to the stdnet.
00012  *
00013  * This software is provided 'as-is', without any express or
00014  * implied warranty. In no event will the authors be held liable
00015  * for any damages arising from the use of this software.
00016  *
00017  * Permission is granted to anyone to use this software for any
00018  * purpose, including commercial applications, and to alter it
00019  * and redistribute it freely, subject to the following
00020  * restrictions:
00021  *
00022  * 1.  The origin of this software must not be misrepresented;
00023  *     you must not claim that you wrote the original software.
00024  *     If you use this software in a product, an acknowledgment
00025  *     in the product documentation would be appreciated but
00026  *     is not required.
00027  *
00028  * 2.  Altered source versions must be plainly marked as such,
00029  *     and must not be misrepresented as being the original
00030  *     software.
00031  *
00032  * 3.  This notice may not be removed or altered from any source
00033  *     distribution.
00034  */
00035 
00036 #include <stdnet/config.h>
00037 #include <stdnet/io/FileInputStream>
00038 
00039 #include <memory.h>
00040 
00041 /*
00042  *  $Revision: 1.1 $
00043  *  $Author: w-molloy $
00044  *  $Date: 2002/08/13 04:42:20 $
00045  */
00046 
00047 namespace stdbase {
00048     namespace io {
00049 
00050 //      --- Static Create Functions ---
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 //      --- Static Create Functions ---
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     //  We don't support mark/reset here.
00093     throw IOException();
00094 }
00095 
00096 /*
00097  *  Test to see whether the FileInputStream implementation supports
00098  *  the mark()/reset() methods.
00099  */
00100 bool FileInputStream::
00101     markSupported()
00102 {
00103     return false;
00104 }
00105 
00106 /*
00107  *  Read a buffer of bytes - NEEDS Array.
00108  */
00109 int FileInputStream::
00110     read( byte buffer[], int len)
00111     throw (IOException)
00112 {
00113     //  Use memcpy
00114     //
00115 
00116     int read_len = 0;
00117 
00118     if ( length == 0 ||
00119          position == length &&
00120          ! input.eof() )
00121     {
00122         //  Reload the buffer...
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     //  Ensure next read operation gets a new buffer...
00144     length = position = 0;
00145 
00146     if ( read_len < len )
00147     {
00148         //  Read the rest directly into the provided buffer...
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  *  Read a single byte from the stream and return it as an
00168  *  integer.
00169  *
00170  *  @return the number of bytes read.
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         //  Reload the buffer...
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     //  Still no data in the buffer ?
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  *  Skip the next n bytes from the input stream.
00204  *
00205  *  returns the number of bytes skipped.
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     } // -- end io --
00284 } // -- end stdbase --
00285 

Generated at Tue Aug 13 14:19:39 2002 for stdnet2 by doxygen1.2.9.1 written by Dimitri van Heesch, © 1997-2001