001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.swing;
038:
039: import java.io.OutputStream;
040: import javax.swing.text.Document;
041: import javax.swing.text.BadLocationException;
042: import javax.swing.text.AttributeSet;
043:
044: /**
045: * An extension of {@link OutputStream} that writes its output to
046: * an implementation of {@link Document}.
047: *
048: * @version $Id: DocumentOutputStream.java 4255 2007-08-28 19:17:37Z mgricken $
049: */
050: public class DocumentOutputStream extends OutputStream {
051: private Document _doc;
052: private AttributeSet _attributes;
053:
054: /**
055: * Constructs an {@link OutputStream} that writes its output to a
056: * {@link Document}.
057: *
058: * When this constructor is used, all insertions to the Document will
059: * be done with the attributes set to <code>null</code>.
060: *
061: * @param doc Document to write output to.
062: */
063: public DocumentOutputStream(Document doc) {
064: this (doc, null);
065: }
066:
067: /**
068: * Constructs an {@link OutputStream} that writes its output to a
069: * {@link Document}.
070: *
071: * @param doc Document to write output to.
072: * @param attributes Attributes to use for inserting text into the document
073: * that is sent to this stream.
074: */
075: public DocumentOutputStream(Document doc, AttributeSet attributes) {
076: _doc = doc;
077: _attributes = attributes;
078: }
079:
080: /**
081: * Write a character to the stream.
082: * @param c the ASCII value of the character to write.
083: */
084: public void write(int c) {
085: try {
086: _doc.insertString(_doc.getLength(), String
087: .valueOf((char) c), _attributes);
088: } catch (BadLocationException canNeverHappen) {
089: throw new RuntimeException(
090: "Internal error: bad location in OutputWindowStream");
091: }
092: }
093:
094: /**
095: * Write an array of characters (bytes) to the stream at a particular offset.
096: * @param b characters to write to stream
097: * @param off start of writing
098: * @param len number of characters to write from b
099: */
100: public void write(byte[] b, int off, int len) {
101: try {
102: _doc.insertString(_doc.getLength(),
103: new String(b, off, len), _attributes);
104: } catch (BadLocationException canNevenHappen) {
105: throw new RuntimeException(
106: "Internal error: bad location in OutputWindowStream");
107: }
108: }
109: }
|