1 16 package org.directwebremoting.util; 17 18 import java.io.IOException ; 19 import java.io.Writer ; 20 21 import javax.servlet.ServletOutputStream ; 22 23 27 public final class WriterOutputStream extends ServletOutputStream 28 { 29 33 public WriterOutputStream(Writer writer) 34 { 35 this.writer = writer; 36 } 37 38 43 public WriterOutputStream(Writer writer, String encoding) 44 { 45 this.writer = writer; 46 this.encoding = encoding; 47 } 48 49 52 public void print(String s) throws IOException 53 { 54 writer.write(s); 55 } 56 57 60 public void write(byte[] ba) throws IOException 61 { 62 if (encoding == null) 63 { 64 writer.write(new String (ba)); 65 } 66 else 67 { 68 writer.write(new String (ba, encoding)); 69 } 70 } 71 72 75 public void write(byte[] ba, int off, int len) throws IOException 76 { 77 if (encoding == null) 78 { 79 writer.write(new String (ba, off, len)); 80 } 81 else 82 { 83 writer.write(new String (ba, off, len, encoding)); 84 } 85 } 86 87 90 public synchronized void write(int bite) throws IOException 91 { 92 buffer[0] = (byte) bite; 93 write(buffer); 94 } 95 96 99 public void close() throws IOException 100 { 101 if (writer != null) 102 { 103 writer.close(); 104 writer = null; 105 encoding = null; 106 } 107 } 108 109 112 public void flush() throws IOException 113 { 114 writer.flush(); 115 } 116 117 120 private Writer writer; 121 122 125 private String encoding = null; 126 127 130 private byte[] buffer = new byte[1]; 131 } 132 | Popular Tags |