Skip to content

Instantly share code, notes, and snippets.

@prassee
Created February 1, 2013 04:57
Show Gist options
  • Save prassee/4689366 to your computer and use it in GitHub Desktop.
Save prassee/4689366 to your computer and use it in GitHub Desktop.
LimitedInputStream , this reads chunks of data from a file rather than a whole file.
/**
* The Class LimitedInputStream.
*
* @author sysadmin
*/
public class LimitedInputStream extends FileInputStream {
/** The length. */
private long length;
/** The counter. */
private long counter = 0;
/** The input stream. */
private InputStream inputStream;
/**
* Instantiates a new limited input stream.
*
* @param length the length
* @param inputStream the input stream
* @throws Exception the exception
*/
public LimitedInputStream(long length, FileInputStream inputStream)
throws Exception {
this(length, inputStream, inputStream.getFD());
}
/**
* Instantiates a new limited input stream.
*
* @param length the length
* @param inputStream the input stream
* @param file the file
* @throws FileNotFoundException the file not found exception
*/
protected LimitedInputStream(long length, InputStream inputStream, File file)
throws FileNotFoundException {
super(file);
this.length = length;
this.inputStream = inputStream;
}
/**
* Instantiates a new limited input stream.
*
* @param file the file
* @throws FileNotFoundException the file not found exception
*/
protected LimitedInputStream(File file) throws FileNotFoundException {
super(file);
}
/**
* Instantiates a new limited input stream.
*
* @param fdObj the fd obj
*/
protected LimitedInputStream(FileDescriptor fdObj) {
super(fdObj);
}
/**
* Instantiates a new limited input stream.
*
* @param name the name
* @throws FileNotFoundException the file not found exception
*/
protected LimitedInputStream(String name) throws FileNotFoundException {
super(name);
}
/**
* Instantiates a new limited input stream.
*
* @param length the length
* @param inputStream the input stream
* @param fdObj the fd obj
*/
protected LimitedInputStream(long length, InputStream inputStream,
FileDescriptor fdObj) {
super(fdObj);
this.length = length;
this.inputStream = inputStream;
}
/**
* Instantiates a new limited input stream.
*
* @param length the length
* @param inputStream the input stream
* @param name the name
* @throws FileNotFoundException the file not found exception
*/
protected LimitedInputStream(long length, InputStream inputStream,
String name) throws FileNotFoundException {
super(name);
this.length = length;
this.inputStream = inputStream;
}
/* (non-Javadoc)
* @see java.io.FileInputStream#read()
*/
@Override
public int read() throws IOException {
if (counter < length) {
int read = inputStream.read();
counter = counter + read;
return read;
} else {
return -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment