//============================================================================== // WriterOutputStream.java //============================================================================== package tribble.io; // System imports import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.lang.String; // Local imports // (None) /******************************************************************************* * Pass-through character writer stream. * *
* Writes characters to an underlying {@link Writer} object, writing individual * output bytes as 8-bit character codes. This is effectively a character stream * that writes ISO 8859-1 character codes as uninterpreted single-byte * sequences, i.e., each character code is composed of a single 8-bit octet * written to the output stream. * *
* This class is essentially the inverse of class
* {@link java.io.OutputStreamWriter}.
*
*
* @version $Revision: 1.1 $ $Date: 2005/04/09 04:18:20 $
* @since 2005-04-08
* @author
* David R. Tribble
* (david@tribble.com).
*
*
* Copyright ©2005 by David R. Tribble, all rights reserved.
*
* Permission is granted to freely use and distribute this source
* code provided that the original copyright and authorship notices
* remain intact.
*
* @see ReaderInputStream
* @see PassThruWriter
* @see java.io.OutputStreamWriter
*/
public class WriterOutputStream
extends java.io.OutputStream
{
// Identification
/** Revision information. */
static final String REV =
"@(#)tribble/io/WriterOutputStream.java $Revision: 1.1 $ $Date: 2005/04/09 04:18:20 $\n";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Protected constants
/** Platform-specific newline character sequence. */
protected static final String NEWLINE =
System.getProperty("line.separator");
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Protected variables
/** Underlying output stream writer. */
protected Writer m_out;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public constructors
/***************************************************************************
* Constructor.
*
* @param out
* Underlying output stream, which provides a destination for output bytes.
*
* @since 1.1, 2005-04-08
*/
public WriterOutputStream(Writer out)
{
// Initialize
m_out = out;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public methods
/***************************************************************************
* Flush and close this output stream.
*
* @throws IOException
* Thrown if an I/O error occurs while closing the underlying output writer.
*
* @since 1.1, 2005-04-08
*/
public void close()
throws IOException
//overrides java.io.OutputStream
{
Writer out;
// Close the underlying output writer
out = m_out;
m_out = null;
if (out != null)
out.close();
}
/***************************************************************************
* Flush any pending output to this output stream.
*
* @throws IOException
* Thrown if an I/O error occurs while closing the underlying output writer.
*
* @since 1.1, 2005-04-08
*/
public void flush()
throws IOException
//overrides java.io.OutputStream
{
if (m_out == null)
throw new IOException("Stream is closed.");
m_out.flush();
}
/***************************************************************************
* Write a single character.
*
* @param ch
* A character byte. Only the lower 8 bits of the character are used
* (yielding characters in the range [0x0000,0x00FF]).
*
* @throws IOException
* Thrown if an I/O error occurs while writing to the underlying output
* writer.
*
* @since 1.1, 2005-04-08
*/
public void write(int ch)
throws IOException
//overrides java.io.OutputStream
{
if (m_out == null)
throw new IOException("Stream is closed.");
// Write a character as a single byte
m_out.write(ch & 0x00FF);
}
/***************************************************************************
* Write a group of characters.
*
*
* This method has exactly the same effect as the call: * write(buf, 0, buf.length). * * @param buf (const) * An array of character bytes to be written. * Each byte represents a single 8-bit character code in the range * [0x0000,0x00FF]. * * @throws IOException * Thrown if an I/O error occurs while writing to the underlying output * writer. * * @since 1.1, 2005-04-08 */ public void write(/*const*/ byte[] buf) throws IOException //overrides java.io.OutputStream { write(buf, 0, buf.length); } /*************************************************************************** * Write a group of characters. * * @param buf (const) * An array of character bytes to be written. * Each byte represents a single 8-bit character code in the range * [0x0000,0x00FF]. * * @param off * Index of the first character in buf to write. * * @param len * Number of characters in buf to write. * * @throws IOException * Thrown if an I/O error occurs while writing to the underlying output * writer. * * @since 1.1, 2005-04-08 */ public void write(/*const*/ byte[] buf, int off, int len) throws IOException //overrides java.io.OutputStream { int n; if (m_out == null) throw new IOException("Stream is closed."); // Write characters as bytes to the output writer len += off; for (n = off; n < len; n++) { // Write the next character as a single byte m_out.write(buf[n] & 0x00FF); } } /*************************************************************************** * Write a newline sequence to this output stream. * *
* If the underlying stream is a {@link java.io.PrintWriter}, its
* println() method is called;
* if the stream is a {@link java.io.BufferedWriter}, its newLine()
* method is called;
* otherwise, the native newline character sequence is written to it by
* calling its write() method.
*
* @throws IOException
* Thrown if an I/O error occurs while writing to the underlying output
* writer.
*
* @since 1.1, 2005-04-08
*/
public void newLine()
throws IOException
{
if (m_out == null)
throw new IOException("Stream is closed.");
// Write a newline sequence to the output writer
if (m_out instanceof PrintWriter)
((PrintWriter) m_out).println();
else if (m_out instanceof BufferedWriter)
((BufferedWriter) m_out).newLine();
else
m_out.write(NEWLINE);
}
}
// End WriterOutputStream.java