/** * @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 byte[] toByteArray() { ByteBuffer b = getFirstBuffer(); if (b == null) { return new byte[0]; } ByteBuffer l = b; while (l.getNext() != null) { l = l.getNext(); } byte[] result = new byte[l.getTotalSize()]; int index = 0; while (b != null) { int s = b.getUsedSize(); System.arraycopy(b.getBytes(), 0, result, index, s); index += s; b = b.getNext(); } return result; }
/** * @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; }
/** * 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(); } }
current = current.getNext(); index = 0;
current = current.getNext(); index = 0;
/** * @see java.io.InputStream#skip(long) */ public long skip(long n) throws IOException { long skipped = 0; while (n > 0) { if (current == null) { return 0; } if (current.getUsedSize() - index <= n) { index += n; skipped += n; break; } else { int dn = current.getUsedSize() - index; skipped += dn; n -= dn; current = current.getNext(); index = 0; } } return skipped; } }
/** * 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(); } }
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; }
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; }
/** * 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); }
/** * 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); }