001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.util;
006:
007: import java.io.Writer;
008: import java.io.StringWriter;
009: import java.io.IOException;
010:
011: /**
012: * This class is taken from the JDK 1.2.2_11 reference
013: * implementation and modified to be unsynchronized, for
014: * performance reasons. Objects based on this class
015: * must not be used
016: * in a context where they are accessed by multiple
017: * threads.
018: */
019:
020: public class NSStringWriter extends StringWriter {
021:
022: private NSStringBuffer buf;
023:
024: /**
025: * Flag indicating whether the stream has been closed.
026: */
027: private boolean isClosed = false;
028:
029: /** Check to make sure that the stream has not been closed */
030: private void ensureOpen() {
031: /* This method does nothing for now. Once we add throws clauses
032: * to the I/O methods in this class, it will throw an IOException
033: * if the stream has been closed.
034: */
035: }
036:
037: /**
038: * Create a new string writer, using the default initial string-buffer
039: * size.
040: */
041: public NSStringWriter() {
042: buf = new NSStringBuffer();
043: lock = buf;
044: }
045:
046: /**
047: * Create a new string writer, using the specified initial string-buffer
048: * size.
049: *
050: * @param initialSize an int specifying the initial size of the buffer.
051: */
052: public NSStringWriter(int initialSize) {
053: if (initialSize < 0) {
054: throw new IllegalArgumentException("Negative buffer size");
055: }
056: buf = new NSStringBuffer(initialSize);
057: lock = buf;
058: }
059:
060: /**
061: * Write a single character.
062: */
063: public void write(int c) {
064: ensureOpen();
065: buf.append((char) c);
066: }
067:
068: /**
069: * Write a portion of an array of characters.
070: *
071: * @param cbuf Array of characters
072: * @param off Offset from which to start writing characters
073: * @param len Number of characters to write
074: */
075: public void write(char cbuf[], int off, int len) {
076: ensureOpen();
077:
078: if (off < 0) {
079: throw new IndexOutOfBoundsException();
080: }
081:
082: if (len < 0) {
083: throw new IndexOutOfBoundsException();
084: }
085:
086: if (off + len > cbuf.length) {
087: throw new IndexOutOfBoundsException();
088: }
089:
090: buf.append(cbuf, off, len);
091: }
092:
093: /**
094: * Write a string.
095: */
096: public void write(String str) {
097: ensureOpen();
098: buf.append(str);
099: }
100:
101: /**
102: * Write a portion of a string.
103: *
104: * @param str String to be written
105: * @param off Offset from which to start writing characters
106: * @param len Number of characters to write
107: */
108: public void write(String str, int off, int len) {
109: ensureOpen();
110: buf.append(str.substring(off, off + len));
111: }
112:
113: /**
114: * Return the buffer's current value as a string.
115: */
116: public String toString() {
117: return buf.toString();
118: }
119:
120: /**
121: * Return the string buffer itself.
122: *
123: * @return StringBuffer holding the current buffer value.
124: */
125: public StringBuffer getBuffer() {
126: return buf.toStringBuffer();
127: }
128:
129: /**
130: * Flush the stream.
131: */
132: public void flush() {
133: ensureOpen();
134: }
135:
136: /**
137: * Close the stream. This method does not release the buffer, since its
138: * contents might still be required.
139: */
140: public void close() throws IOException {
141: isClosed = true;
142: }
143:
144: }
|