All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javolution.io.AppendableWriter Maven / Gradle / Ivy

Go to download

Only the Java Core part of Javolution library, with slight modifications for use in MSFTBX.

There is a newer version: 6.11.8
Show newest version
/*
 * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
 * Copyright (C) 2012 - Javolution (http://javolution.org/)
 * All rights reserved.
 * 
 * Permission to use, copy, modify, and distribute this software is
 * freely granted, provided that this notice is preserved.
 */
package javolution.io;

import java.io.IOException;
import java.io.Writer;

/**
 * 

This class allows any Appendable to be used as * a writer.

* * @author Jean-Marie Dautelle * @version 3.8, May 8, 2006 */ public final class AppendableWriter extends Writer { /** * Holds the current appendable output or null if closed. */ private Appendable _output; /** * Creates a new appendable writer for which the appendable output * is not set. * * @see #setOutput(Appendable) */ public AppendableWriter() {} /** * Creates a new appendable writer for which the output is set. * * @param output the appendable written to. */ public AppendableWriter(Appendable output) { _output = output; } /** * Sets the appendable output being written to. * * @param output the appendable written to. * @throws IllegalStateException if this writer is being reused and * it has not been {@link #close closed} or {@link #reset reset}. */ public void setOutput(Appendable output) { if (_output != null) throw new IllegalStateException("Writer not closed or reset"); this._output = output; } /** * Returns the output of this writer. */ public Appendable getOutput() { return _output; } /** * Writes a single character. * * @param c char the character to be written. * @throws IOException if an I/O error occurs. */ public void write(char c) throws IOException { if (_output == null) throw new IOException("Writer closed"); _output.append(c); } /** * Writes the 16 low-order bits of the given integer value; * the 16 high-order bits are ignored. * * @param c the value of the character to be written. * @throws IOException if an I/O error occurs. */ public void write(int c) throws IOException { if (_output == null) throw new IOException("Writer closed"); _output.append((char) c); } /** * Writes a portion of an array of characters. * * @param cbuf the array of characters. * @param off the offset from which to start writing characters. * @param len the number of characters to write. * @throws IOException if an I/O error occurs. */ public void write(char cbuf[], int off, int len) throws IOException { if (_output == null) throw new IOException("Writer closed"); _tmpBuffer = cbuf; _output.append(_tmpBufferAsCharSequence, off, off + len); _tmpBuffer = null; // Removes temporary references. } private char[] _tmpBuffer; private final CharSequence _tmpBufferAsCharSequence = new CharSequence() { public int length() { return _tmpBuffer.length; } public char charAt(int index) { return _tmpBuffer[index]; } public CharSequence subSequence(int start, int end) { throw new UnsupportedOperationException(); } }; /** * Writes a portion of a string. * * @param str a String. * @param off the offset from which to start writing characters. * @param len the number of characters to write. * @throws IOException if an I/O error occurs */ public void write(String str, int off, int len) throws IOException { if (_output == null) throw new IOException("Writer closed"); _output.append(str, off, off + len); } /** * Writes the specified character sequence. * * @param csq the character sequence. * @throws IOException if an I/O error occurs */ public void write(CharSequence csq) throws IOException { if (_output == null) throw new IOException("Writer closed"); _output.append(csq); } /** * Flushes the stream. */ public void flush() { // Do nothing (no buffer). } /** * Closes and {@link #reset resets} this writer for reuse. */ public void close() { if (_output != null) { reset(); } } public void reset() { _output = null; _tmpBuffer = null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy