/** * 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); } }
public void complete() { buffer.compact(); } }
private byte[] getBufferBytes() { byte[] bs = new byte[buffer.getLast().getTotalSize()]; int pos = 0; ByteBuffer currentBuffer = buffer; while (currentBuffer != null) { System.arraycopy(currentBuffer.getBytes(), 0, bs, pos, currentBuffer.getUsedSize()); pos += currentBuffer.getUsedSize(); currentBuffer = currentBuffer.getNext(); } return bs; }
@Override public long getSize() { return buffer.getLast().getTotalSize(); }
/** * @see java.io.InputStream#mark(int) */ public void mark(int readAheadLimit) { if (current == null) { mark = 0; } else { mark = index; ByteBuffer b = current.getPrevious(); if (b != null) { mark += b.getTotalSize(); } } }
/** * Writes all data written up to the moment to out. * * @param out * @throws IOException */ public void writeTo(OutputStream out) throws IOException { ByteBuffer b = getFirstBuffer(); while (b != null) { out.write(b.getBytes(), 0, b.getUsedSize()); b = b.getNext(); } }
/** * @see java.io.InputStream#reset() */ public void reset() { if (current == null) { current = firstLink; } int m = 0; while ((current != null) && (current.getUsedSize() + m <= mark)) { m += current.getUsedSize(); current = current.getNext(); } if (current != null) { index = mark - m; } }
/** * @see java.io.InputStream#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; } byte c = current.getByteAt(index); index++; return c; }
/** * Creates instance with required initial capacity. * * @param initialSize */ public FastBufferOutputStream(int initialSize) { this(new ByteBuffer(initialSize)); }
/** * @see java.io.OutputStream#write(int) */ public void write(int c) throws IOException { lastBuffer = lastBuffer.append((byte) c); length++; }
/** * 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 bytes stored in this link and all its predecessors. * * @return total number of bytes stored in this link and all its predecessors */ public int getTotalSize() { return (prev == null) ? usedSize : prev.getTotalSize() + usedSize; }
int av = current.getUsedSize() - index; current = current.getNext(); index = 0; System.arraycopy(current.getBytes(), index, b, off, av); index += av; off += av;
/** * @see java.io.InputStream#available() */ public int available() throws IOException { if (current == null) { return 0; } ByteBuffer b = current; int result = -index; while (b != null) { result += b.getUsedSize(); b = b.getNext(); } return result; }
public void create() { buffer = new ByteBuffer(128); }
public void handle(byte[] bytes, int length) throws IOException { buffer.append(bytes, 0, length); }
@Override public long getSize() { return buffer.getLast().getTotalSize(); }
/** * 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); }
private byte[] getBufferBytes() { byte[] bs = new byte[buffer.getLast().getTotalSize()]; int pos = 0; ByteBuffer currentBuffer = buffer; while (currentBuffer != null) { System.arraycopy(currentBuffer.getBytes(), 0, bs, pos, currentBuffer.getUsedSize()); pos += currentBuffer.getUsedSize(); currentBuffer = currentBuffer.getNext(); } return bs; }
int av = current.getUsedSize() - index; current = current.getNext(); index = 0; System.arraycopy(current.getBytes(), index, b, off, av); index += av; off += av;