1 package org.andromda.cartridges.jsf.portlet.myfaces.tomahawk.support; 2 3 import java.io.CharConversionException; 4 import java.io.IOException; 5 import java.io.OutputStream; 6 import java.text.MessageFormat; 7 import javax.servlet.ServletOutputStream; 8 9 /** 10 * This class is a dummy ServletOutputStream. 11 * 12 * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a> 13 * 14 */ 15 public class ServletOutputStreamWrapper extends ServletOutputStream { 16 private OutputStream outputStream; 17 18 /** 19 * @param outputStream 20 */ 21 public ServletOutputStreamWrapper(OutputStream outputStream) { 22 this.outputStream = outputStream; 23 } 24 25 /** 26 * Writes a <code>String</code> to the client, without a carriage 27 * return-line feed (CRLF) character at the end. 28 * 29 * 30 * @param s 31 * the <code>String</code> to send to the client 32 * 33 * @exception IOException 34 * if an input or output exception occurred 35 * 36 */ 37 public void print(String s) throws IOException { 38 if (s == null) 39 s = "null"; 40 int len = s.length(); 41 for (int i = 0; i < len; i++) { 42 char c = s.charAt(i); 43 44 // 45 // XXX NOTE: This is clearly incorrect for many strings, 46 // but is the only consistent approach within the current 47 // servlet framework. It must suffice until servlet output 48 // streams properly encode their output. 49 // 50 if ((c & 0xff00) != 0) { // high order byte must be zero 51 String errMsg = "Not an ISO 8859-1 character: {0}"; 52 Object[] errArgs = new Object[1]; 53 errArgs[0] = new Character(c); 54 errMsg = MessageFormat.format(errMsg, errArgs); 55 throw new CharConversionException(errMsg); 56 } 57 write(c); 58 } 59 } 60 61 /** 62 * Writes a <code>boolean</code> value to the client, with no carriage 63 * return-line feed (CRLF) character at the end. 64 * 65 * @param b 66 * the <code>boolean</code> value to send to the client 67 * 68 * @exception IOException 69 * if an input or output exception occurred 70 * 71 */ 72 public void print(boolean b) throws IOException { 73 String msg; 74 if (b) { 75 msg = "true"; 76 } else { 77 msg = "false"; 78 } 79 print(msg); 80 } 81 82 /** 83 * Writes a character to the client, with no carriage return-line feed 84 * (CRLF) at the end. 85 * 86 * @param c 87 * the character to send to the client 88 * 89 * @exception IOException 90 * if an input or output exception occurred 91 * 92 */ 93 public void print(char c) throws IOException { 94 print(String.valueOf(c)); 95 } 96 97 /** 98 * 99 * Writes an int to the client, with no carriage return-line feed (CRLF) at 100 * the end. 101 * 102 * @param i 103 * the int to send to the client 104 * 105 * @exception IOException 106 * if an input or output exception occurred 107 * 108 */ 109 public void print(int i) throws IOException { 110 print(String.valueOf(i)); 111 } 112 113 /** 114 * 115 * Writes a <code>long</code> value to the client, with no carriage 116 * return-line feed (CRLF) at the end. 117 * 118 * @param l 119 * the <code>long</code> value to send to the client 120 * 121 * @exception IOException 122 * if an input or output exception occurred 123 * 124 */ 125 public void print(long l) throws IOException { 126 print(String.valueOf(l)); 127 } 128 129 /** 130 * 131 * Writes a <code>float</code> value to the client, with no carriage 132 * return-line feed (CRLF) at the end. 133 * 134 * @param f 135 * the <code>float</code> value to send to the client 136 * 137 * @exception IOException 138 * if an input or output exception occurred 139 */ 140 public void print(float f) throws IOException { 141 print(String.valueOf(f)); 142 } 143 144 /** 145 * 146 * Writes a <code>double</code> value to the client, with no carriage 147 * return-line feed (CRLF) at the end. 148 * 149 * @param d 150 * the <code>double</code> value to send to the client 151 * 152 * @exception IOException 153 * if an input or output exception occurred 154 */ 155 public void print(double d) throws IOException { 156 print(String.valueOf(d)); 157 } 158 159 /** 160 * Writes a carriage return-line feed (CRLF) to the client. 161 * 162 * @exception IOException if an input or output exception occurred 163 */ 164 public void println() throws IOException { 165 print("\r\n"); 166 } 167 168 /** 169 * Writes a <code>String</code> to the client, followed by a carriage 170 * return-line feed (CRLF). 171 * 172 * 173 * @param s the <code>String</code> to write to the client 174 * 175 * @exception IOException if an input or output exception occurred 176 * 177 */ 178 public void println(String s) throws IOException { 179 print(s); 180 println(); 181 } 182 183 /** 184 * 185 * Writes a <code>boolean</code> value to the client, followed by a 186 * carriage return-line feed (CRLF). 187 * 188 * @param b the <code>boolean</code> value to write to the client 189 * @exception IOException if an input or output exception occurred 190 * 191 */ 192 public void println(boolean b) throws IOException { 193 print(b); 194 println(); 195 } 196 197 /** 198 * 199 * Writes a character to the client, followed by a carriage return-line feed 200 * (CRLF). 201 * 202 * @param c 203 * the character to write to the client 204 * 205 * @exception IOException 206 * if an input or output exception occurred 207 * 208 */ 209 public void println(char c) throws IOException { 210 print(c); 211 println(); 212 } 213 214 /** 215 * 216 * Writes an int to the client, followed by a carriage return-line feed 217 * (CRLF) character. 218 * 219 * 220 * @param i 221 * the int to write to the client 222 * 223 * @exception IOException 224 * if an input or output exception occurred 225 * 226 */ 227 public void println(int i) throws IOException { 228 print(i); 229 println(); 230 } 231 232 /** 233 * 234 * Writes a <code>long</code> value to the client, followed by a carriage 235 * return-line feed (CRLF). 236 * 237 * 238 * @param l 239 * the <code>long</code> value to write to the client 240 * 241 * @exception IOException 242 * if an input or output exception occurred 243 * 244 */ 245 public void println(long l) throws IOException { 246 print(l); 247 println(); 248 } 249 250 /** 251 * 252 * Writes a <code>float</code> value to the client, followed by a carriage 253 * return-line feed (CRLF). 254 * 255 * @param f 256 * the <code>float</code> value to write to the client 257 * 258 * 259 * @exception IOException 260 * if an input or output exception occurred 261 * 262 */ 263 public void println(float f) throws IOException { 264 print(f); 265 println(); 266 } 267 268 /** 269 * 270 * Writes a <code>double</code> value to the client, followed by a 271 * carriage return-line feed (CRLF). 272 * 273 * 274 * @param d 275 * the <code>double</code> value to write to the client 276 * 277 * @exception IOException 278 * if an input or output exception occurred 279 * 280 */ 281 public void println(double d) throws IOException { 282 print(d); 283 println(); 284 } 285 286 /** 287 * @see java.io.OutputStream#write(int) 288 */ 289 public void write(int b) throws IOException { 290 outputStream.write(b); 291 } 292 293 /** 294 * @see java.io.OutputStream#close() 295 */ 296 public void close() throws IOException { 297 outputStream.close(); 298 } 299 300 /** 301 * @see Object#equals(Object) 302 */ 303 public boolean equals(Object obj) { 304 return outputStream.equals(obj); 305 } 306 307 /** 308 * @see java.io.OutputStream#flush() 309 */ 310 public void flush() throws IOException { 311 outputStream.flush(); 312 } 313 314 /** 315 * @see Object#hashCode() 316 */ 317 public int hashCode() { 318 return outputStream.hashCode(); 319 } 320 321 /** 322 * @see Object#toString() 323 */ 324 public String toString() { 325 return outputStream.toString(); 326 } 327 328 /** 329 * @see java.io.OutputStream#write(byte[], int, int) 330 */ 331 public void write(byte[] b, int off, int len) throws IOException { 332 outputStream.write(b, off, len); 333 } 334 335 /** 336 * @see java.io.OutputStream#write(byte[]) 337 */ 338 public void write(byte[] b) throws IOException { 339 outputStream.write(b); 340 } 341 }