1 8 package org.jivesoftware.util.log.output.io; 9 10 import org.jivesoftware.util.log.format.Formatter; 11 import org.jivesoftware.util.log.output.AbstractOutputTarget; 12 import java.io.IOException ; 13 import java.io.OutputStream ; 14 15 20 public class StreamTarget extends AbstractOutputTarget { 21 private OutputStream m_outputStream; 23 24 30 public StreamTarget(final OutputStream outputStream, final Formatter formatter) { 31 super(formatter); 32 33 if (null != outputStream) { 34 setOutputStream(outputStream); 35 open(); 36 } 37 } 38 39 45 protected synchronized void setOutputStream(final OutputStream outputStream) { 46 if (null == outputStream) { 47 throw new NullPointerException ("outputStream property must not be null"); 48 } 49 50 m_outputStream = outputStream; 51 } 52 53 58 protected synchronized void write(final String data) { 59 final OutputStream outputStream = m_outputStream; 62 63 if (null == outputStream) { 64 final String message = "Attempted to send data '" + data + "' to Null OutputStream"; 65 getErrorHandler().error(message, null, null); 66 return; 67 } 68 69 try { 70 outputStream.write(data.getBytes("UTF-8")); 72 outputStream.flush(); 73 } 74 catch (final IOException ioe) { 75 final String message = "Error writing data '" + data + "' to OutputStream"; 76 getErrorHandler().error(message, ioe, null); 77 } 78 } 79 80 84 public synchronized void close() { 85 super.close(); 86 shutdownStream(); 87 } 88 89 92 protected synchronized void shutdownStream() { 93 final OutputStream outputStream = m_outputStream; 94 m_outputStream = null; 95 96 try { 97 if (null != outputStream) { 98 outputStream.close(); 99 } 100 } 101 catch (final IOException ioe) { 102 getErrorHandler().error("Error closing OutputStream", ioe, null); 103 } 104 } 105 } 106 | Popular Tags |