FileDocCategorySizeDatePackage
FiniteInputStream.javaAPI DocExample1550Sat Sep 09 20:53:32 BST 2000com.macfaq.io

FiniteInputStream

public class FiniteInputStream extends FilterInputStream

Fields Summary
private int
limit
private int
bytesRead
Constructors Summary
public FiniteInputStream(InputStream in)

  
     
    this(in, 8192); 
  
public FiniteInputStream(InputStream in, int limit)

    super(in);
    this.limit = limit; 
  
Methods Summary
public intavailable()

    if (bytesRead >= limit) return 1;
    else return in.available();
  
public intread()

    
    if (bytesRead >= limit) return -1;
    int c = in.read();
    bytesRead++;
    return c; 
    
  
public intread(byte[] data)

    return this.read(data, 0, data.length); 
  
public intread(byte[] data, int offset, int length)

    
	if (data == null) throw new NullPointerException();
	else if ((offset < 0) || (offset > data.length) || (length < 0) ||
	 ((offset + length) > data.length) || ((offset + length) < 0)) {
	  throw new IndexOutOfBoundsException();
	} 
	else if (length == 0) {
	  return 0;
	}

    if (bytesRead >= limit) return -1;
    else if (bytesRead + length > limit) {
      int numToRead = bytesRead + length - limit; 
      int numRead = in.read(data, offset, numToRead);  
      if (numRead == -1) return -1;  
      bytesRead += numRead;
      return numRead; 
    }
    else { // will not exceed limit
      int numRead = in.read(data, offset, length);
      if (numRead == -1) return -1;  
      bytesRead += numRead; 
      return numRead; 
    }