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/FileOutputStream>
00038
00039 namespace stdbase {
00040 namespace io {
00041
00042 using ::stdbase::system::reference;
00043 using ::stdbase::system::StringRef;
00044
00045 typedef ::stdbase::system::platform::FileDescriptor
00046 FileDescriptor;
00047
00048 typedef FileDescriptor::Flags Flags;
00049
00050 void FileOutputStream::
00051 write( int b)
00052 throw (IOException)
00053 {
00054 char aByte = static_cast<char>( b );
00055
00056 if ( file == 0 || ! file->isOpen() )
00057 throw IOException();
00058
00059 if ( ! file->write( &aByte, 1 ) )
00060 throw IOException();
00061 }
00062
00063 void FileOutputStream::
00064 write( byte b[], int len)
00065 throw (IOException)
00066 {
00067 if ( file == 0 || ! file->isOpen() )
00068 throw IOException();
00069
00070 if ( len <= 0 )
00071 throw IOException();
00072
00073 char * aBuffer = (char*)( b );
00074
00075 if ( ! file->write( aBuffer, len ) )
00076 throw IOException();
00077 }
00078
00079
00080 void FileOutputStream::
00081 flush()
00082 throw (IOException)
00083 {
00084 if ( file == 0 || ! file->isOpen() )
00085 throw IOException();
00086 }
00087
00088 void FileOutputStream::
00089 close()
00090 throw (IOException)
00091 {
00092 if ( file == 0 || ! file->isOpen() )
00093 throw IOException();
00094 if ( ! file->close() )
00095 throw IOException();
00096 }
00097
00098
00099
00100 reference< FileOutputStream > FileOutputStream::
00101 create( system::StringRef name )
00102 {
00103 return FileOutputStreamRef( new FileOutputStream( name ) );
00104 }
00105
00106 reference< FileOutputStream > FileOutputStream::
00107 create( const FileDescriptor & _fd)
00108 {
00109 return FileOutputStreamRef( new FileOutputStream( _fd ) );
00110 }
00111
00112
00113
00114 FileOutputStream::
00115 FileOutputStream( StringRef name )
00116 {
00117 int len = name->length();
00118 char * MyName = new char[ len + 2];
00119 name->getChars( 0, len, MyName, 0);
00120 MyName[ len ] = 0;
00121 file = new system::platform::FileDescriptor();
00122
00123 file-> open( MyName, FileDescriptor::CREATE_WRITE_BIN);
00124 }
00125
00126 FileOutputStream::
00127 FileOutputStream( const system::platform::FileDescriptor & fd)
00128 : file( const_cast< system::platform::FileDescriptor *>(& fd ) )
00129 {
00130 }
00131
00132 FileOutputStream::
00133 ~FileOutputStream()
00134 {
00135 try { close(); }
00136 catch( IOException e )
00137 { e = e; }
00138 if ( file != 0 )
00139 {
00140 delete file;
00141 file = 0;
00142 }
00143 }
00144
00145 }
00146 }
00147