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 #include "CeylanOutputStream.h"
00028
00029 #include "CeylanStringUtils.h"
00030 #include "CeylanOperators.h"
00031 #include "CeylanLogPlug.h"
00032 #include "CeylanEndianness.h"
00033
00034
00035 #ifdef CEYLAN_USES_CONFIG_H
00036 #include "CeylanConfig.h"
00037 #endif // CEYLAN_USES_CONFIG_H
00038
00039
00040
00041
00042
00043 using namespace Ceylan::System ;
00044 using namespace Ceylan::Log ;
00045
00046
00047 using std::string ;
00048
00049
00050
00051
00052 OutputStream::OutputStream( bool blocking ) :
00053 Stream( blocking )
00054 {
00055
00056 }
00057
00058
00059
00060 OutputStream::~OutputStream() throw()
00061 {
00062
00063 }
00064
00065
00066
00067 Size OutputStream::write( const std::string & message )
00068 {
00069
00070 throw WriteFailedException(
00071 "In OutputStream::write (std::string based) failed: "
00072 "this method should have been subclassed." ) ;
00073
00074 }
00075
00076
00077
00078 Size OutputStream::write( const Ceylan::Byte * buffer, Size length )
00079 {
00080
00081 throw WriteFailedException(
00082 "In OutputStream::write (buffer-based) failed: "
00083 "this method should have been subclassed." ) ;
00084
00085 }
00086
00087
00088
00089 void OutputStream::writeSint8( Ceylan::Sint8 toWrite )
00090 {
00091
00092 const Ceylan::Uint8 TypeSize = 1 ;
00093
00094 Size writeCount = write(
00095 reinterpret_cast<Ceylan::Byte*>( & toWrite ), TypeSize ) ;
00096
00097 if ( writeCount < TypeSize )
00098 throw WriteFailedException( "In OutputStream::writeSint8" ) ;
00099
00100 }
00101
00102
00103
00104 void OutputStream::writeUint8( Ceylan::Uint8 toWrite )
00105 {
00106
00107 const Ceylan::Uint8 TypeSize = 1 ;
00108
00109 Size writeCount = write(
00110 reinterpret_cast<Ceylan::Byte*>( & toWrite ), TypeSize ) ;
00111
00112 if ( writeCount < TypeSize )
00113 throw WriteFailedException( "In OutputStream::writeUint8" ) ;
00114
00115 }
00116
00117
00118
00119 void OutputStream::writeSint16( Ceylan::Sint16 toWrite )
00120 {
00121
00122 const Ceylan::Uint8 TypeSize = 2 ;
00123
00124 #if CEYLAN_RUNS_ON_LITTLE_ENDIAN
00125
00126
00127
00128 #else // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00129
00130 toWrite = ceylan_bswap_16( toWrite ) ;
00131
00132 #endif // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00133
00134 Size writeCount = write(
00135 reinterpret_cast<Ceylan::Byte *>( & toWrite ), TypeSize ) ;
00136
00137 if ( writeCount < TypeSize )
00138 throw WriteFailedException( "In OutputStream::writeSint16" ) ;
00139
00140 }
00141
00142
00143
00144 void OutputStream::writeUint16( Ceylan::Uint16 toWrite )
00145 {
00146
00147 const Ceylan::Uint8 TypeSize = 2 ;
00148
00149 #if CEYLAN_RUNS_ON_LITTLE_ENDIAN
00150
00151
00152
00153 #else // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00154
00155 toWrite = ceylan_bswap_16( toWrite ) ;
00156
00157 #endif // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00158
00159 Size writeCount = write(
00160 reinterpret_cast<Ceylan::Byte *>( & toWrite ), TypeSize ) ;
00161
00162 if ( writeCount < TypeSize )
00163 throw WriteFailedException( "In OutputStream::writeUint16" ) ;
00164
00165 }
00166
00167
00168
00169 void OutputStream::writeSint32( Ceylan::Sint32 toWrite )
00170 {
00171
00172 const Ceylan::Uint8 TypeSize = 4 ;
00173
00174 #if CEYLAN_RUNS_ON_LITTLE_ENDIAN
00175
00176
00177
00178 #else // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00179
00180 toWrite = ceylan_bswap_32( toWrite ) ;
00181
00182 #endif // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00183
00184 Size writeCount = write(
00185 reinterpret_cast<Ceylan::Byte *>( & toWrite ), TypeSize ) ;
00186
00187 if ( writeCount < TypeSize )
00188 throw WriteFailedException( "In OutputStream::writeSint32" ) ;
00189
00190 }
00191
00192
00193
00194 void OutputStream::writeUint32( Ceylan::Uint32 toWrite )
00195 {
00196
00197 const Ceylan::Uint8 TypeSize = 4 ;
00198
00199 #if CEYLAN_RUNS_ON_LITTLE_ENDIAN
00200
00201
00202
00203 #else // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00204
00205 toWrite = ceylan_bswap_32( toWrite ) ;
00206
00207 #endif // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00208
00209 Size writeCount = write(
00210 reinterpret_cast<Ceylan::Byte *>( & toWrite ), TypeSize ) ;
00211
00212 if ( writeCount < TypeSize )
00213 throw WriteFailedException( "In OutputStream::writeUint32" ) ;
00214
00215 }
00216
00217
00218
00219 void OutputStream::writeFloat32( Ceylan::Float32 toWrite )
00220 {
00221
00222 const Ceylan::Uint8 TypeSize = 4 ;
00223
00224 #if CEYLAN_RUNS_ON_LITTLE_ENDIAN
00225
00226
00227
00228 #else // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00229
00230 Ceylan::Uint32 * tmp = reinterpret_cast<Ceylan::Uint32 *>( &toWrite ) ;
00231
00232
00233 *tmp = ceylan_bswap_32( *tmp ) ;
00234
00235 #endif // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00236
00237 Size writeCount = write(
00238 reinterpret_cast<Ceylan::Byte *>( & toWrite ), TypeSize ) ;
00239
00240 if ( writeCount < TypeSize )
00241 throw WriteFailedException( "In OutputStream::writeFloat32" ) ;
00242
00243 }
00244
00245
00246
00247 void OutputStream::writeFloat64( Ceylan::Float64 toWrite )
00248 {
00249
00250 const Ceylan::Uint8 TypeSize = 8 ;
00251
00252 #if CEYLAN_RUNS_ON_LITTLE_ENDIAN
00253
00254
00255
00256 #else // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00257
00258
00259 Ceylan::Uint64 * tmp = reinterpret_cast<Ceylan::Uint64 *>( & toWrite ) ;
00260
00261
00262
00263 #ifdef CEYLAN_FAKES_64_BIT_TYPE
00264
00265 Ceylan::byteswap( *tmp ) ;
00266
00267 #else // CEYLAN_FAKES_64_BIT_TYPE
00268
00269 *tmp = ceylan_bswap_64( *tmp ) ;
00270
00271 #endif // CEYLAN_FAKES_64_BIT_TYPE
00272
00273
00274 #endif // CEYLAN_RUNS_ON_LITTLE_ENDIAN
00275
00276 Size writeCount = write(
00277 reinterpret_cast<Ceylan::Byte *>( & toWrite ), TypeSize ) ;
00278
00279 if ( writeCount < TypeSize )
00280 throw WriteFailedException( "In OutputStream::writeFloat64" ) ;
00281
00282 }
00283
00284
00285
00286 void OutputStream::writeString( const string & toWrite )
00287 {
00288
00289 StringSize stringSize = toWrite.size() ;
00290
00291 if ( stringSize > Ceylan::Uint16Max )
00292 throw WriteFailedException( "OutputStream::writeString: "
00293 "string '" + toWrite + "' is too long." ) ;
00294
00295 #if CEYLAN_DEBUG_LOW_LEVEL_STREAMS
00296 Log::LogPlug::debug( "OutputStream::writeString: string size is "
00297 + Ceylan::toString( static_cast<Ceylan::Uint32>( stringSize ) )
00298 + " characters." ) ;
00299 #endif // CEYLAN_DEBUG_LOW_LEVEL_STREAMS
00300
00301 writeUint16( static_cast<Ceylan::Uint16>( stringSize ) ) ;
00302
00303 Size writeCount = write( toWrite.data(), stringSize ) ;
00304
00305 #if CEYLAN_DEBUG_LOW_LEVEL_STREAMS
00306 LogPlug::debug( "OutputStream::writeString: wrote "
00307 + Ceylan::toString( static_cast<Ceylan::Uint32>( writeCount ) )
00308 + " bytes." ) ;
00309 #endif // CEYLAN_DEBUG_LOW_LEVEL_STREAMS
00310
00311 if ( writeCount < stringSize )
00312 throw WriteFailedException( "In OutputStream::writeString" ) ;
00313
00314 }
00315
00316
00317
00318 const std::string OutputStream::toString( Ceylan::VerbosityLevels level ) const
00319 {
00320
00321 string res = "OutputStream whose ID is "
00322 + Ceylan::toString( getOutputStreamID() ) ;
00323
00324 res += ". This is a " + Stream::toString( level ) ;
00325
00326 return res ;
00327
00328 }
00329