001package com.identityworksllc.iiq.common; 002 003import java.io.IOException; 004import java.io.InputStream; 005 006/** 007 * An input stream primarily for testing purposes that always contains the given 008 * number of zeroes. 009 */ 010public class NullInputStream extends InputStream { 011 private int size; 012 013 /** 014 * Constructs a new NullInputStream with a default input size of 0. The 015 * first read will always produce an EOF. 016 */ 017 public NullInputStream() { 018 this(0); 019 } 020 021 /** 022 * Constructs a new NullInputStream with the specified input size. 023 * @param size The number of bytes to simulate in the input 024 */ 025 public NullInputStream(int size) { 026 this.size = size; 027 } 028 029 /** 030 * "Reads" one byte from the NullInputStream, returning 0 if we have not exhausted the 031 * given pool of bytes (specified by 'size') and -1 (EOF) if we have. 032 * 033 * @return 0 or -1 034 * @throws IOException never, but the interface requires it 035 */ 036 @Override 037 public int read() throws IOException { 038 if (size-- > 0) { 039 return 0; 040 } 041 return -1; 042 } 043}