JTextComponentOutputStream.java from DrJava at Krugle
Show JTextComponentOutputStream.java syntax highlighted
/*
* DynamicJava - Copyright (C) 1999-2001
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL DYADE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Dyade shall not be
* used in advertising or otherwise to promote the sale, use or other
* dealings in this Software without prior written authorization from
* Dyade.
*
*/
package koala.dynamicjava.gui;
import java.io.*;
import javax.swing.text.*;
/**
* An output stream that writes to a swing JTextComponent
*
* @author Stephane Hillion
* @version 1.0 - 1999/11/15
*/
public class JTextComponentOutputStream extends OutputStream {
/**
* The text component to write to
*/
protected JTextComponent textComponent;
/**
* Creates a new output stream
* @param tc the text component that will receive the output
*/
public JTextComponentOutputStream(JTextComponent tc) {
textComponent = tc;
}
/**
* Writes the specified byte to this output stream.
* <p>
* Subclasses of <code>OutputStream</code> must provide an
* implementation for this method.
*
* @param b the <code>byte</code>.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public void write(int b) throws IOException {
write(new byte[] { (byte)b }, 0, 1);
}
/**
* Writes <code>len</code> bytes from the specified byte array
* starting at offset <code>off</code> to this output stream.
* <p>
* The <code>write</code> method of <code>OutputStream</code> calls
* the write method of one argument on each of the bytes to be
* written out. Subclasses are encouraged to override this method and
* provide a more efficient implementation.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @exception IOException if an I/O error occurs.
* @since JDK1.0
*/
public void write(byte b[], int off, int len) throws IOException {
try {
Document doc = textComponent.getDocument();
doc.insertString(doc.getLength(), new String(b, off, len), null);
} catch (BadLocationException e) {
throw new IOException(e.getMessage());
}
}
}
See more files for this project here