001 /* 002 * $Id: StringBufferWriter.java 695 2004-01-27 20:55:44Z jstrachan $ 003 * 004 * Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved. 005 * 006 * Redistribution and use of this software and associated documentation 007 * ("Software"), with or without modification, are permitted provided that the 008 * following conditions are met: 1. Redistributions of source code must retain 009 * copyright statements and notices. Redistributions must also contain a copy 010 * of this document. 2. Redistributions in binary form must reproduce the above 011 * copyright notice, this list of conditions and the following disclaimer in 012 * the documentation and/or other materials provided with the distribution. 3. 013 * The name "groovy" must not be used to endorse or promote products derived 014 * from this Software without prior written permission of The Codehaus. For 015 * written permission, please contact info@codehaus.org. 4. Products derived 016 * from this Software may not be called "groovy" nor may "groovy" appear in 017 * their names without prior written permission of The Codehaus. "groovy" is a 018 * registered trademark of The Codehaus. 5. Due credit should be given to The 019 * Codehaus - http://groovy.codehaus.org/ 020 * 021 * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY 022 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 023 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 024 * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR 025 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 027 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 028 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 029 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 030 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 031 * DAMAGE. 032 * 033 */ 034 package org.codehaus.groovy.runtime; 035 036 import java.io.IOException; 037 import java.io.Writer; 038 039 /** 040 * This class codes around a silly limiation of StringWriter which doesn't allow a StringBuffer 041 * to be passed in as a constructor for some bizzare reason. 042 * So we replicate the behaviour of StringWriter here but allow a StringBuffer to be passed in. 043 * 044 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a> 045 * @version $Revision: 695 $ 046 */ 047 public class StringBufferWriter extends Writer { 048 049 private StringBuffer buffer; 050 051 /** 052 * Create a new string writer which will append the text to the given StringBuffer 053 */ 054 public StringBufferWriter(StringBuffer buffer) { 055 this.buffer = buffer; 056 } 057 058 /** 059 * Write a single character. 060 */ 061 public void write(int c) { 062 buffer.append((char) c); 063 } 064 065 /** 066 * Write a portion of an array of characters. 067 * 068 * @param text Array of characters 069 * @param offset Offset from which to start writing characters 070 * @param length Number of characters to write 071 */ 072 public void write(char text[], int offset, int length) { 073 if ((offset < 0) || (offset > text.length) || (length < 0) || ((offset + length) > text.length) || ((offset + length) < 0)) { 074 throw new IndexOutOfBoundsException(); 075 } 076 else if (length == 0) { 077 return; 078 } 079 buffer.append(text, offset, length); 080 } 081 082 /** 083 * Write a string. 084 */ 085 public void write(String text) { 086 buffer.append(text); 087 } 088 089 /** 090 * Write a portion of a string. 091 * 092 * @param text the text to be written 093 * @param offset offset from which to start writing characters 094 * @param length Number of characters to write 095 */ 096 public void write(String text, int offset, int length) { 097 buffer.append(text.substring(offset, offset + length)); 098 } 099 100 /** 101 * Return the buffer's current value as a string. 102 */ 103 public String toString() { 104 return buffer.toString(); 105 } 106 107 /** 108 * Flush the stream. 109 */ 110 public void flush() { 111 } 112 113 /** 114 * Closing a <tt>StringWriter</tt> has no effect. The methods in this 115 * class can be called after the stream has been closed without generating 116 * an <tt>IOException</tt>. 117 */ 118 public void close() throws IOException { 119 } 120 }