1 9 package javolution.xml; 10 11 import j2me.lang.IllegalStateException; 12 import java.io.IOException; 13 import java.io.OutputStream; 14 15 import javolution.lang.Reusable; 16 17 28 public class XmlOutputStream extends OutputStream implements Reusable { 29 30 35 static final byte END_XML = (byte) 0xFE; 36 37 40 private OutputStream _outputStream; 41 42 45 private final ObjectWriter _objectWriter = new ObjectWriter(); 46 47 50 private final OutputStreamProxy _outputStreamProxy = new OutputStreamProxy(); 51 52 55 public XmlOutputStream() { 56 } 57 58 66 public XmlOutputStream setOutputStream(OutputStream out) { 67 if (_outputStream != null) 68 throw new IllegalStateException("Stream not closed or reset"); 69 _outputStream = out; 70 return this; 71 } 72 73 79 public void writeObject(Object obj) throws IOException { 80 if (_outputStream == null) throw new IOException("Stream closed"); 81 _objectWriter.write(obj, _outputStreamProxy); 82 _outputStream.write(END_XML); 83 _outputStream.flush(); 84 } 85 86 88 94 public void write(int b) throws IOException { 95 if (_outputStream == null) throw new IOException("Stream closed"); 96 _outputStream.write(b); 97 } 98 99 105 public void flush() throws IOException { 106 if (_outputStream == null) throw new IOException("Stream closed"); 107 _outputStream.flush(); 108 } 109 110 119 public void write(byte b[], int off, int len) throws IOException { 120 if (_outputStream == null) throw new IOException("Stream closed"); 121 _outputStream.write(b, off, len); 122 } 123 124 129 public void close() throws IOException { 130 if (_outputStream != null) { 131 _outputStream.close(); 132 reset(); 133 } 134 } 135 136 public void reset() { 138 _objectWriter.reset(); 139 _outputStream = null; 140 } 141 142 146 private final class OutputStreamProxy extends OutputStream { 147 public void flush() throws IOException { 148 _outputStream.flush(); 149 } 150 public void write(byte b[], int off, int len) throws IOException { 151 _outputStream.write(b, off, len); 152 } 153 public void write(int b) throws IOException { 154 _outputStream.write(b); 155 } 156 public void close() throws IOException { 157 } 159 } 160 } | Popular Tags |