Save This Page
Home » sitemesh-2.3 » com.opensymphony.module » sitemesh » util » [javadoc | source]
    1   package com.opensymphony.module.sitemesh.util;
    2   
    3   import java.io.Writer;
    4   import java.io.IOException;
    5   
    6   /**
    7    * Unsynced version of the JDK's CharArrayWriter
    8    */
    9   public class CharArrayWriter extends Writer {
   10       /**
   11        * The buffer where data is stored.
   12        */
   13       protected char buf[];
   14   
   15       /**
   16        * The number of chars in the buffer.
   17        */
   18       protected int count;
   19   
   20       /**
   21        * Creates a new CharArrayWriter.
   22        */
   23       public CharArrayWriter() {
   24     this(32);
   25       }
   26   
   27       /**
   28        * Creates a new CharArrayWriter with the specified initial size.
   29        *
   30        * @param initialSize  an int specifying the initial buffer size.
   31        * @exception IllegalArgumentException if initialSize is negative
   32        */
   33       public CharArrayWriter(int initialSize) {
   34           if (initialSize < 0) {
   35               throw new IllegalArgumentException("Negative initial size: "
   36                    + initialSize);
   37           }
   38     buf = new char[initialSize];
   39       }
   40   
   41       /**
   42        * Writes a character to the buffer.
   43        */
   44       public void write(int c) {
   45         int newcount = count + 1;
   46         if (newcount > buf.length) {
   47       char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
   48       System.arraycopy(buf, 0, newbuf, 0, count);
   49       buf = newbuf;
   50         }
   51         buf[count] = (char)c;
   52         count = newcount;
   53       }
   54   
   55       /**
   56        * Writes characters to the buffer.
   57        * @param c	the data to be written
   58        * @param off	the start offset in the data
   59        * @param len	the number of chars that are written
   60        */
   61       public void write(char c[], int off, int len) {
   62     if ((off < 0) || (off > c.length) || (len < 0) ||
   63               ((off + len) > c.length) || ((off + len) < 0)) {
   64         throw new IndexOutOfBoundsException();
   65     } else if (len == 0) {
   66         return;
   67     }
   68         int newcount = count + len;
   69         if (newcount > buf.length) {
   70       char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
   71       System.arraycopy(buf, 0, newbuf, 0, count);
   72       buf = newbuf;
   73         }
   74         System.arraycopy(c, off, buf, count, len);
   75         count = newcount;
   76       }
   77   
   78       /**
   79        * Write a portion of a string to the buffer.
   80        * @param  str  String to be written from
   81        * @param  off  Offset from which to start reading characters
   82        * @param  len  Number of characters to be written
   83        */
   84       public void write(String str, int off, int len) {
   85         int newcount = count + len;
   86         if (newcount > buf.length) {
   87       char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
   88       System.arraycopy(buf, 0, newbuf, 0, count);
   89       buf = newbuf;
   90         }
   91         str.getChars(off, off + len, buf, count);
   92         count = newcount;
   93       }
   94   
   95       /**
   96        * Writes the contents of the buffer to another character stream.
   97        *
   98        * @param out	the output stream to write to
   99        * @throws java.io.IOException If an I/O error occurs.
  100        */
  101       public void writeTo(Writer out) throws IOException {
  102         out.write(buf, 0, count);
  103       }
  104   
  105       /**
  106        * Resets the buffer so that you can use it again without
  107        * throwing away the already allocated buffer.
  108        */
  109       public void reset() {
  110     count = 0;
  111       }
  112   
  113       /**
  114        * Returns a copy of the input data.
  115        *
  116        * @return an array of chars copied from the input data.
  117        */
  118       public char toCharArray()[] {
  119         char newbuf[] = new char[count];
  120         System.arraycopy(buf, 0, newbuf, 0, count);
  121         return newbuf;
  122       }
  123   
  124       /**
  125        * Returns the current size of the buffer.
  126        *
  127        * @return an int representing the current size of the buffer.
  128        */
  129       public int size() {
  130     return count;
  131       }
  132   
  133       /**
  134        * Converts input data to a string.
  135        * @return the string.
  136        */
  137       public String toString() {
  138         return new String(buf, 0, count);
  139       }
  140   
  141       /**
  142        * Flush the stream.
  143        */
  144       public void flush() { }
  145   
  146       /**
  147        * Close the stream.  This method does not release the buffer, since its
  148        * contents might still be required.
  149        */
  150       public void close() { }
  151   
  152   }

Save This Page
Home » sitemesh-2.3 » com.opensymphony.module » sitemesh » util » [javadoc | source]