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/IOException>
00038 #include <stdnet/io/PrintStream>
00039
00040 #include <stdio.h>
00041 #include <string.h>
00042
00043 namespace stdbase {
00044 namespace io {
00045
00046 const system::reference<PrintStream> PrintStream::
00047 create( const OutputStreamRef & ref)
00048 {
00049 const system::reference<PrintStream> object = new PrintStream( ref );
00050 return object;
00051 }
00052
00053 void PrintStream::
00054 flush()
00055 throw (IOException)
00056 {
00057 try
00058 {
00059 output->flush();
00060 }
00061 catch( IOException e)
00062 {
00063 e = e;
00064 error = true;
00065 }
00066 }
00067
00068 void PrintStream::
00069 close()
00070 throw (IOException)
00071 {
00072 try
00073 {
00074 output->close();
00075 }
00076 catch( IOException e)
00077 {
00078 e = e;
00079 error = true;
00080 }
00081 }
00082
00083 void PrintStream::
00084 write( int b )
00085 throw (IOException)
00086 {
00087 try
00088 { output->write(b); }
00089 catch( IOException e)
00090 {
00091 e = e;
00092 error = true;
00093 }
00094 }
00095
00096 void PrintStream::
00097 write( byte b[], int len)
00098 throw (IOException)
00099 {
00100 try
00101 { output->write(b, len); }
00102 catch( IOException e)
00103 {
00104 e = e;
00105 error = true;
00106 }
00107 }
00108
00109 void PrintStream::
00110 println( system::StringRef str)
00111 {
00112 int len = str->length();
00113 char * buffer = new char[ len + 2];
00114 str-> getChars( 0, len, buffer, 0);
00115 buffer[ len ] = '\n';
00116 buffer[ len + 1] = 0;
00117
00118 try
00119 { output->write( (byte *)(buffer), len + 1 ); }
00120 catch( IOException e)
00121 {
00122 e = e;
00123 error = true;
00124 }
00125
00126 delete [] buffer;
00127 }
00128
00129 void PrintStream::
00130 println( const char text[] )
00131 {
00132 int len = strlen( text );
00133 try
00134 {
00135 output->write( (byte *)(text), len );
00136 output->write( (byte) '\n' );
00137 }
00138 catch( IOException e)
00139 {
00140 e = e;
00141 error = true;
00142 }
00143 }
00144
00145 void PrintStream::
00146 println( int i )
00147 {
00148 int len;
00149 const int max = 64;
00150 char buffer[ max ];
00151
00152 len = snprintf( buffer, max, "%d\n", i );
00153
00154 try
00155 { output->write( (byte *)(buffer), len ); }
00156 catch( IOException e)
00157 {
00158 e = e;
00159 error = true;
00160 }
00161 }
00162
00163 void PrintStream::
00164 print( system::StringRef str)
00165 {
00166 int len = str->length();
00167 char * buffer = new char[ len + 2];
00168 str-> getChars( 0, len, buffer, 0);
00169 buffer[ len ] = 0;
00170
00171 try
00172 { output->write( (byte *)(buffer), len ); }
00173 catch( IOException e)
00174 {
00175 e = e;
00176 error = true;
00177 }
00178
00179 delete [] buffer;
00180 }
00181
00182 void PrintStream::
00183 print( const char text[] )
00184 {
00185 int len = strlen( text );
00186 try
00187 {
00188 output->write( (byte *)(text), len );
00189 }
00190 catch( IOException e)
00191 {
00192 e = e;
00193 error = true;
00194 }
00195 }
00196
00197 void PrintStream::
00198 print( int i )
00199 {
00200 int len;
00201 const int max = 64;
00202 char buffer[ max ];
00203
00204 len = snprintf( buffer, max, "%d", i );
00205
00206 try
00207 { output->write( (byte *)(buffer), len ); }
00208 catch( IOException e)
00209 {
00210 e = e;
00211 error = true;
00212 }
00213 }
00214
00215 bool PrintStream::
00216 checkError()
00217 {
00218 return error;
00219 }
00220
00221 bool PrintStream::
00222 equals( system::ObjectRef o) const
00223 {
00224 return const_cast<const stdbase::system::Object *>(o.get()) == this;
00225 }
00226
00227 PrintStream::
00228 PrintStream( const OutputStreamRef & ref)
00229 : Object(),
00230 output( ref ),
00231 error(false)
00232 {}
00233
00234 PrintStream::
00235 ~PrintStream()
00236 {
00237 try {
00238 output->close();
00239 }
00240 catch( IOException e)
00241 {
00242 e = e;
00243 }
00244 }
00245
00246 }
00247 }
00248
00249