org.ajax4jsf.io.CharBuffer Maven / Gradle / Ivy
/**
* 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 char arrays.
* @author glory
*/
public class CharBuffer {
private CharBuffer prev;
private CharBuffer next;
/**
* Stored characters
*/
private char[] chars;
/**
* Length of char array.
*/
private int cacheSize;
/**
* number of characters stored in the array.
*/
private int usedSize;
/**
* Creates instance of CharBuffer with char array of required length.
* @param cacheSize length of char array
*/
public CharBuffer(int cacheSize) {
this.cacheSize = cacheSize;
chars = new char[cacheSize];
usedSize = 0;
}
/**
* Creates instance of CharBuffer already filled by chars.
* @param bytes
*/
public CharBuffer(char[] chars) {
this.chars = chars;
usedSize = chars.length;
cacheSize = usedSize;
}
/**
* Appends character to array chars if there are unfilled positions in it.
* Otherwize creates next link in the chain, and appends the character to it.
* @param c
* @return instance of CharBuffer to which character was appended.
*/
public CharBuffer append(char c) {
if(next != null) {
return next.append(c);
}
if(usedSize < cacheSize) {
chars[usedSize] = c;
usedSize++;
return this;
} else {
next = new CharBuffer(cacheSize * 2);
next.prev = this;
return next.append(c);
}
}
/**
* Appends segment of a char 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 CharBuffer to which char array was appended.
*/
public CharBuffer append(char[] cs, int off, int len) {
if(next != null) {
return append(cs, off, len);
}
if(len + usedSize <= cacheSize) {
System.arraycopy(cs, off, chars, usedSize, len);
usedSize += len;
return this;
}
int av = cacheSize - usedSize;
if(av > 0) {
System.arraycopy(cs, off, chars, usedSize, av);
usedSize += av;
off += av;
len -= av;
}
next = new CharBuffer(cacheSize * 2);
next.prev = this;
return next.append(cs, off, len);
}
/**
* Returns stored char array.
* @return stored char array
*/
public char[] getChars() {
return chars;
}
/**
* Returns character at index. No check is fulfilled to provide high speed.
* @param index
* @return
*/
public char getCharAt(int index) {
return chars[index];
}
/**
* Returns actual number of characters 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 characters 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 CharBuffer getPrevious() {
return prev;
}
/**
* Returns the next link in the chain.
* @return
*/
public CharBuffer getNext() {
return next;
}
/**
* Sets the next link in the chain.
* @param b
*/
public void setNext(CharBuffer b) {
next = b;
if(b != null) b.prev = this;
}
/**
* Transforms this instance to instance of ByteBuffer (a link of chain of byte arrays).
* @param encoding
* @return link of chain of byte arrays
* @throws UnsupportedEncodingException
*/
public ByteBuffer toByteBuffer(String encoding) throws UnsupportedEncodingException {
byte[] bs = new String(chars, 0, usedSize).getBytes(encoding);
return new ByteBuffer(bs);
}
/**
* Transforms this instance to instance of ByteBuffer (a link of chain of byte arrays).
* @return link of chain of byte arrays
*/
public ByteBuffer toByteBuffer() {
byte[] bs = new String(chars, 0, usedSize).getBytes();
return new ByteBuffer(bs);
}
}