1 16 package org.mortbay.util; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.io.Writer ; 21 22 23 24 31 public class WriterOutputStream extends OutputStream 32 { 33 protected Writer _writer; 34 protected String _encoding; 35 private byte[] _buf=new byte[1]; 36 37 38 public WriterOutputStream(Writer writer, String encoding) 39 { 40 _writer=writer; 41 _encoding=encoding; 42 } 43 44 45 public WriterOutputStream(Writer writer) 46 { 47 _writer=writer; 48 } 49 50 51 public void close() 52 throws IOException 53 { 54 _writer.close(); 55 _writer=null; 56 _encoding=null; 57 } 58 59 60 public void flush() 61 throws IOException 62 { 63 _writer.flush(); 64 } 65 66 67 public void write(byte[] b) 68 throws IOException 69 { 70 if (_encoding==null) 71 _writer.write(new String (b)); 72 else 73 _writer.write(new String (b,_encoding)); 74 } 75 76 77 public void write(byte[] b, int off, int len) 78 throws IOException 79 { 80 if (_encoding==null) 81 _writer.write(new String (b,off,len)); 82 else 83 _writer.write(new String (b,off,len,_encoding)); 84 } 85 86 87 public synchronized void write(int b) 88 throws IOException 89 { 90 _buf[0]=(byte)b; 91 write(_buf); 92 } 93 } 94 95 | Popular Tags |