001: /*
002: * StrWriter.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.util;
013:
014: import java.io.IOException;
015: import java.io.Writer;
016:
017: /**
018: * A replacement for a StringWriter which is not synchronized for
019: * performance reasons
020: * @author support@sql-workbench.net
021: */
022:
023: public class StrWriter extends Writer {
024: private StrBuffer buf;
025:
026: /**
027: * Create a new string writer, using the default initial string-buffer
028: * size.
029: */
030: public StrWriter() {
031: buf = new StrBuffer();
032: }
033:
034: /**
035: * Create a new string writer, using the specified initial string-buffer
036: * size.
037: *
038: * @param initialSize an int specifying the initial size of the buffer.
039: */
040: public StrWriter(int initialSize) {
041: if (initialSize < 0) {
042: throw new IllegalArgumentException("Negative buffer size");
043: }
044: buf = new StrBuffer(initialSize);
045: }
046:
047: /**
048: * Write a single character.
049: */
050: public void write(int c) {
051: buf.append((char) c);
052: }
053:
054: /**
055: * Write a portion of an array of characters.
056: *
057: * @param cbuf Array of characters
058: * @param off Offset from which to start writing characters
059: * @param len Number of characters to write
060: */
061: public void write(char cbuf[], int off, int len) {
062: if ((off < 0) || (off > cbuf.length) || (len < 0)
063: || ((off + len) > cbuf.length) || ((off + len) < 0)) {
064: throw new IndexOutOfBoundsException();
065: } else if (len == 0) {
066: return;
067: }
068: buf.append(cbuf, off, len);
069: }
070:
071: /**
072: * Write a string.
073: */
074: public void write(String str) {
075: buf.append(str);
076: }
077:
078: /**
079: * Write a portion of a string.
080: *
081: * @param str String to be written
082: * @param off Offset from which to start writing characters
083: * @param len Number of characters to write
084: */
085: public void write(String str, int off, int len) {
086: buf.append(str.substring(off, off + len));
087: }
088:
089: /**
090: * Return the buffer's current value as a string.
091: */
092: public String toString() {
093: return buf.toString();
094: }
095:
096: /**
097: * Return the string buffer itself.
098: *
099: * @return StringBuilder holding the current buffer value.
100: */
101: public StrBuffer getBuffer() {
102: return buf;
103: }
104:
105: /**
106: * Flush the stream.
107: */
108: public void flush() {
109: }
110:
111: /**
112: * Closing a <tt>StrWriter</tt> has no effect. The methods in this
113: * class can be called after the stream has been closed without generating
114: * an <tt>IOException</tt>.
115: */
116: public void close() throws IOException {
117: }
118:
119: }
|