//============================================================================== // PassThruWriter.java //============================================================================== package tribble.io; // System imports import java.io.IOException; import java.io.OutputStream; import java.io.Writer; import java.lang.String; // Local imports // (None) /******************************************************************************* * Pass-through character writer stream. * *
* Writes characters to an underlying {@link OutputStream} object, writing
* each Unicode character code as a single byte. 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.
*
*
* @version $Revision: 1.1 $ $Date: 2005/04/06 04:36:39 $
* @since 2005-04-05
* @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 PassThruReader
*/
public class PassThruWriter
extends java.io.Writer
{
// Identification
/** Revision information. */
static final String REV =
"@(#)tribble/io/PassThruWriter.java $Revision: 1.1 $ $Date: 2005/04/06 04:36:39 $\n";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Protected variables
/** Underlying byte output stream. */
protected OutputStream m_out;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public constructors
/***************************************************************************
* Constructor.
*
* @param out
* Underlying output stream, which provides a destination for output bytes.
*
* @since 1.1, 2005-04-05
*/
public PassThruWriter(OutputStream out)
{
// Initialize
super(out);
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 stream.
*
* @since 1.1, 2005-04-05
*/
public void close()
throws IOException
//overrides java.io.Writer
{
OutputStream out;
// Close the underlying output stream
out = m_out;
m_out = null;
if (out != null)
{
synchronized (this.lock)
{
out.close();
}
}
}
/***************************************************************************
* Flush any pending output to this output stream.
*
* @throws IOException
* Thrown if an I/O error occurs while closing the underlying output stream.
*
* @since 1.1, 2005-04-05
*/
public void flush()
throws IOException
//overrides java.io.Writer
{
if (m_out == null)
throw new IOException("Stream is closed.");
m_out.flush();
}
/***************************************************************************
* Write a single character.
*
* @param ch
* A Unicode character code. 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 the underlying output stream.
*
* @since 1.1, 2005-04-05
*/
public void write(int ch)
throws IOException
//overrides java.io.Writer
{
if (m_out == null)
throw new IOException("Stream is closed.");
synchronized (this.lock)
{
// Write a character as a single byte
m_out.write(ch & 0x00FF);
}
}
/***************************************************************************
* Write a group of characters.
*
* @param buf
* An array of Unicode characters to be written.
* Only the lower 8 bits of each character are used (yielding characters 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 the underlying output stream.
*
* @since 1.1, 2005-04-05
*/
public void write(char[] buf, int off, int len)
throws IOException
//overrides java.io.Writer
{
int n;
if (m_out == null)
throw new IOException("Stream is closed.");
synchronized (this.lock)
{
// Write characters as bytes to the output stream
len += off;
for (n = off; n < len; n++)
{
// Write the next character as a single byte
m_out.write(buf[n] & 0x00FF);
}
}
}
}
// End PassThruWriter.java