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

org.ajax4jsf.io.ByteBuffer 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.UnsupportedEncodingException;

/**
 * A single link in chain of byte arrays. 
 * @author glory
 */
public class ByteBuffer {
	private ByteBuffer prev;
	private ByteBuffer next;

	/**
	 * Stored bytes
	 */
	private byte[] bytes;

	/**
	 * Length of byte array.
	 */
	private int cacheSize;

	/**
	 * Number of bytes stored in the array.
	 */
	private int usedSize;

	/**
	 * Creates instance of ByteBuffer with byte array of required length.
	 * @param cacheSize length of byte array
	 */	
	public ByteBuffer(int cacheSize) {
		bytes = new byte[cacheSize];
		this.cacheSize = cacheSize;
		usedSize = 0;
	}
	
	/**
	 * Creates instance of ByteBuffer already filled by bytes.
	 * @param bytes
	 */
	public ByteBuffer(byte[] bytes) {
		this.bytes = bytes;
		usedSize = bytes.length;
		cacheSize = usedSize;
	}

	/**
	 * Appends byte to array if there are unfilled positions in it.
	 * Otherwize creates next link in the chain, and appends the byte to it.
	 * @param c
	 * @return instance of ByteBuffer to which byte was appended.
	 */	
	public ByteBuffer append(byte c) {
		if(next != null) {
			return next.append(c);
		}
		if(usedSize < cacheSize) {
			bytes[usedSize] = c;
			usedSize++;
			return this;
		} else {
			next = new ByteBuffer(cacheSize * 2);
			next.prev = this;
			return next.append(c);
		}
	}

	/**
	 * Appends segment of a byte array to array if there are unfilled positions in it.
	 * Otherwize creates next link in the chain, and appends data to it.
	 * @param c
	 * @return instance of ByteBuffer to which byte array was appended.
	 */	
	public ByteBuffer append(byte[] bs, int off, int len) {
		if(next != null) {
			return append(bs, off, len);
		}
		if(len + usedSize <= cacheSize) {
			System.arraycopy(bs, off, bytes, usedSize, len);
			usedSize += len;
			return this;
		}
		int av = cacheSize - usedSize;
		if(av > 0) {
			System.arraycopy(bs, off, bytes, usedSize, av);
			usedSize += av;
			off += av;
			len -= av;
		}
		next = new ByteBuffer(cacheSize * 2);
		next.prev = this;
		return next.append(bs, off, len);
	}

	/**
	 * Returns stored byte array.
	 * @return stored byte array
	 */	
	public byte[] getBytes() {
		return bytes;
	}
	
	/**
	 * Returns byte at index. No check is fulfilled to provide high speed.
	 * @param index
	 * @return
	 */
	public byte getByteAt(int index) {
		return bytes[index];
	}
	
	/**
	 * Returns actual number of byte stored in this link.
	 * @return
	 */	
	public int getUsedSize() {
		return usedSize;
	}
	
	/**
	 * Returns capacity of this link.
	 * @return
	 */	
	public int getCacheSize() {
		return cacheSize;
	}
	
	/**
	 * Returns total number of bytes stored in this link and all its predecessors. 
	 * @return
	 */	
	public int getTotalSize() {
		return (prev == null) ? usedSize : prev.getTotalSize() + usedSize;
	}
	
	/**
	 * Returns the previous link in the chain.
	 * @return
	 */	
	public ByteBuffer getPrevious() {
		return prev;
	}
	
	/**
	 * Returns the next link in the chain.
	 * @return
	 */	
	public ByteBuffer getNext() {
		return next;
	}
	
	/**
	 * Sets the next link in the chain.
	 * @param b
	 */	
	public void setNext(ByteBuffer b) {
		next = b;
		if(b != null) b.prev = this;
	}
	
	/**
	 * Transforms this instance to instance of CharBuffer (a link of chain of char arrays).
	 * @param encoding
	 * @return link of chain of char arrays
	 * @throws UnsupportedEncodingException
	 */
	public CharBuffer toCharBuffer(String encoding) throws UnsupportedEncodingException {
		String s;
		if (null != encoding) {
			s = new String(bytes, 0, usedSize, encoding);
		} else {
			s = new String(bytes, 0, usedSize);			
		}
		return new CharBuffer(s.toCharArray());
	}

	/**
	 * Transforms this instance to instance of CharBuffer (a link of chain of char arrays).
	 * @return link of chain of char arrays
	 */
	public CharBuffer toCharBuffer() {
		String s = new String(bytes, 0, usedSize);
		return new CharBuffer(s.toCharArray());
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy