All pastes #916909 Raw Edit

Anonymous

public java v1 · immutable
#916909 ·published 2008-02-25 00:21 UTC
rendered paste body
package kingsy101;import java.io.FilterInputStream;import java.io.IOException;import java.io.InputStream;public class BlockInputStream extends FilterInputStream {  public BlockInputStream (InputStream in, int blockSize) {    super (in);    if (blockSize <= 0)      throw new IllegalArgumentException ("blockSize");    this.blockSize = blockSize;  }  /**   * Reads one block out of the underlying input source. If the source ends   * before a complete block can be read, the returned block will be shorter   * than the block size; otherwise, this will read until a complete block is   * read or the input source fails.   *    * @return the read block.   * @throws IOException   *             if the input source fails.   */  public byte[] readBlock () throws IOException {    byte[] buffer = new byte[blockSize];    int totalRead = 0;    while (totalRead < blockSize) {      int read = in.read (buffer, totalRead, blockSize - totalRead);      if (read == -1)        return truncateBlock (buffer, totalRead);      totalRead += read;    }    return buffer;  }  private byte[] truncateBlock (byte[] buffer, int totalRead) {    byte[] truncatedBuffer = new byte[totalRead];    System.arraycopy (buffer, 0, truncatedBuffer, 0, totalRead);    return truncatedBuffer;  }  private final int blockSize;}