/** * Appends character to array chars if there are unfilled positions in it. Otherwise 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); } }
/** * @since 4.0 */ public void compact() { if (chars.length - usedSize > MAX_WASTE) { char[] cs = new char[usedSize]; System.arraycopy(chars, 0, cs, 0, usedSize); this.chars = cs; this.cacheSize = cs.length; } if (next != null) { next.compact(); } } }
/** * @see java.io.Reader#read() */ public int read() throws IOException { if (current == null) { return -1; } if (current.getUsedSize() <= index) { current = current.getNext(); if (current == null) { return -1; } index = 0; } char c = current.getCharAt(index); index++; return c; }
/** * Writes all data written up to the moment to out. * * @param writer * @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(); } }
/** * Returns the number of chars that may be read from this storage. * * @return the number of chars that may be read from this storag * @throws IOException */ public int available() throws IOException { if (current == null) { return 0; } CharBuffer b = current; int result = -index; while (b != null) { result += b.getUsedSize(); b = b.getNext(); } return result; }
/** * Writes all data written up to the moment to string buffer. * * @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; }
/** * Returns instance of FastBufferOutputStream containing all data written to this writer. * * @return instance of FastBufferOutputStream containing all data written to this writer */ 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); }
/** * Writes all data written up to the moment to out. * * @param out * @throws IOException */ public void writeTo(Writer out, String encoding) throws IOException { ByteBuffer b = getFirstBuffer(); while (b != null) { CharBuffer c = b.toCharBuffer(encoding); out.write(c.getChars(), 0, c.getUsedSize()); b = b.getNext(); } }
/** * @see java.io.Writer#write(int) */ public void write(int c) throws IOException { lastBuffer = lastBuffer.append((char) c); length++; }
/** * Creates instance with required initial capacity. * * @param initialSize */ public FastBufferWriter(int initialSize) { this(new CharBuffer(initialSize)); }
/** * Returns instance of FastBufferWriter containing all data written to this output stream. * * @return instance of FastBufferWriter containing all data written to this output stream */ public FastBufferWriter convertToWriter() { ByteBuffer c = firstBuffer; CharBuffer first = c.toCharBuffer(); CharBuffer b = first; while (c != null) { c = c.getNext(); if (c == null) { break; } CharBuffer n = c.toCharBuffer(); b.setNext(n); b = n; } return new FastBufferWriter(first); }
/** * Returns total number of characters stored in this link and all its predecessors. * * @return total number of characters stored in this link and all its predecessors */ public int getTotalSize() { return (prev == null) ? usedSize : prev.getTotalSize() + usedSize; }
/** * Resets writer to empty state * * @since 3.3.0 */ public void reset() { this.firstBuffer.reset(); this.lastBuffer = this.firstBuffer; this.length = 0; } }
/** * Writes rest of data written up to the moment to out. * * @param writer * @throws IOException */ public void writeTo(Writer writer) throws IOException { if (current == null) { return; } if (current.getUsedSize() > index) { writer.write(current.getChars(), index, current.getUsedSize() - index); current = current.getNext(); } while (current != null) { writer.write(current.getChars(), 0, current.getUsedSize()); current = current.getNext(); } index = 0; } }
/** * Returns instance of FastBufferOutputStream containing all data written to this writer. * * @param encoding * @return instance of FastBufferOutputStream containing all data written to this writer * @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); }
/** * @see java.io.Writer#write(char[]) */ public void write(char[] cbuf) throws IOException { if (cbuf == null) { throw new NullPointerException(); } lastBuffer = lastBuffer.append(cbuf, 0, cbuf.length); length += cbuf.length; }
/** * 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()); }
/** * Returns instance of FastBufferWriter containing all data written to this output stream. * * @param encoding * @return instance of FastBufferWriter containing all data written to this output stream * @throws UnsupportedEncodingException */ public FastBufferWriter convertToWriter(String encoding) throws UnsupportedEncodingException { ByteBuffer c = firstBuffer; CharBuffer first = c.toCharBuffer(encoding); CharBuffer b = first; while (c != null) { c = c.getNext(); if (c == null) { break; } CharBuffer n = c.toCharBuffer(encoding); b.setNext(n); b = n; } return new FastBufferWriter(first); }
int av = current.getUsedSize() - index; current = current.getNext(); index = 0; System.arraycopy(current.getChars(), index, cbuf, off, av); index += av; off += av;