All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.io.FastBufferWriter Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

The newest version!
/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package org.ajax4jsf.io;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import javax.servlet.ServletOutputStream;

/**
 * Class for writing to chain of char arrays extending Writer.
 *  
 * @author glory
 *
 */
public class FastBufferWriter extends Writer {
	/**
	 * The beginning of the chain of char arrays.
	 */
	CharBuffer firstBuffer;

	/**
	 * Currently filled link of the chain of char arrays.
	 */
	CharBuffer lastBuffer;
	
	/**
	 * Total number of written chars.
	 */	
	int length;
	
	/**
	 * Creates instance of default initial capacity.
	 */	
	public FastBufferWriter() {
		this(256);
	}
	
	/**
	 * Creates instance with required initial capacity.
	 * @param initialSize
	 */
	public FastBufferWriter(int initialSize) {
		this(new CharBuffer(initialSize));
	}
	
	/**
	 * Creates instance for an already existing chain of char arrays.
	 * @param firstBuffer
	 */	
	public FastBufferWriter(CharBuffer firstBuffer) {
		this.firstBuffer = firstBuffer;
		lastBuffer = firstBuffer;
	}
	
	/**
	 * @see java.io.Writer.write(int c)
	 */
	public void write(int c) throws IOException {
		lastBuffer = lastBuffer.append((char)c);
	}

	/**
	 * @see java.io.Writer.write(char cbuf[])
	 */
	public void write(char cbuf[]) throws IOException {
    	if(cbuf == null) {
    		throw new NullPointerException();
    	}

    	lastBuffer = lastBuffer.append(cbuf, 0, cbuf.length);
    	length += cbuf.length;
    	
	}

	/**
	 * @see java.io.Writer.write(char cbuf[], int off, int len)
	 */
    public void write(char[] cbuf, int off, int len) throws IOException {
    	if(cbuf == null) {
    		throw new NullPointerException();
    	}
		if ((off < 0) || (off > cbuf.length) || (len < 0) ||
				((off + len) > cbuf.length) || ((off + len) < 0)) {
				throw new IndexOutOfBoundsException();
		} else if (len == 0) {
			return;
		}
    	lastBuffer = lastBuffer.append(cbuf, off, len);
    	length += len;

	}
	
    /**
     * Returns the total number of written chars.
     * @return
     */
	public int getLength() {
		return length;
	}
	
	/**
	 * Returns the first link of the chain of char arrays.
	 * @return
	 */	
	public CharBuffer getFirstBuffer() {
		return firstBuffer;
	}

	public void close() throws IOException {
	}

	public void flush() throws IOException {
	}

	/**
	 * Writes all data written up to the moment to string buffer.
	 * @param out
	 * @throws IOException
	 */
	public char[] toCharArray() {
		CharBuffer b = firstBuffer;
		if(b == null) return new char[0];
		CharBuffer l = b;
		while(l.getNext() != null) l = l.getNext();
		char[] result = new char[l.getTotalSize()];
		int index = 0;
		while(b != null) {
			int s = b.getUsedSize();
			System.arraycopy(b.getChars(), 0, result, index, s);
			index += s;
			b = b.getNext();
		}		
		return result;
	}

	/**
	 * Writes all data written up to the moment to out.
	 * @param out
	 * @throws IOException
	 */
	public void writeTo(Writer writer) throws IOException {
		CharBuffer b = firstBuffer;
		while(b != null) {
			writer.write(b.getChars(), 0, b.getUsedSize());
			b = b.getNext();
		}
	}
	
	public void printTo(ServletOutputStream outputStream) throws IOException {
		CharBuffer b = firstBuffer;
		while(b != null) {
			outputStream.print(new String(b.getChars()));
			b = b.getNext();
		}
	}

	/**
     * Returns instance of FastBufferOutputStream containing all data written to this writer.
	 * @param encoding
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public FastBufferOutputStream convertToOutputStream(String encoding) throws UnsupportedEncodingException {
		CharBuffer c = firstBuffer;
		ByteBuffer first = c.toByteBuffer(encoding);
		ByteBuffer b = first;
		while(c != null) {
			c = c.getNext();
			if(c == null) break;
			ByteBuffer n = c.toByteBuffer(encoding);
			b.setNext(n);
			b = n;
		}
		return new FastBufferOutputStream(first);
	}

	/**
     * Returns instance of FastBufferOutputStream containing all data written to this writer.
	 * @return
	 */
	public FastBufferOutputStream convertToOutputStream() {
		CharBuffer c = firstBuffer;
		ByteBuffer first = c.toByteBuffer();
		ByteBuffer b = first;
		while(c != null) {
			c = c.getNext();
			if(c == null) break;
			ByteBuffer n = c.toByteBuffer();
			b.setNext(n);
			b = n;
		}
		return new FastBufferOutputStream(first);
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy