helper
import java.io.*;
import naccio.library.Check;
public class NaccioNetworkInputStream extends java.io.FilterInputStream {
// This stream is constructed based on the InputStream passed back by
// a Socket object. All normal InputStream methods get passed along
// to the original InputStream unless overriden here. The member
// variable "in" is the original InputStream.
RNetConnection rnc;
// Default constructor overloaded to keep anyone from calling it.
private NaccioNetworkInputStream(InputStream is) {
super (is);
NCheck.violation ("Network Connection resource must be specified when creatting a network input stream.");
}
// This is the constructor to use.
public NaccioNetworkInputStream (InputStream is, RNetConnection r) {
super (is);
rnc = r;
// System.err.println ("Created input stream: " + r.toString ());
}
// Overloaded methods:
public int read() throws IOException {
// System.err.println ("Read 1: " + rnc.toString ());
RNetwork.preReceiveConnection (rnc, 1);
int res = in.read ();
if (res == -1) {
RNetwork.postReceiveConnection (rnc, 0);
} else {
RNetwork.postReceiveConnection (rnc, 1);
}
// System.err.println ("Read: " + (char) res);
return res;
}
public int read(byte b[]) throws IOException {
// System.err.println ("Read up to: " + b.length);
RNetwork.preReceiveConnection (rnc, b.length);
int nbytes = in.read (b);
if (nbytes == -1) {
RNetwork.postReceiveConnection (rnc, 0);
} else {
RNetwork.postReceiveConnection (rnc, nbytes);
}
return nbytes;
}
public int read(byte b[], int off, int len) throws IOException {
RNetwork.preReceiveConnection (rnc, len);
int nbytes = in.read(b,off,len);
if (nbytes == -1) {
RNetwork.postReceiveConnection (rnc, 0);
} else {
RNetwork.postReceiveConnection (rnc, nbytes);
}
return nbytes;
}
public long skip (long n) throws IOException {
RNetwork.preReceiveConnection (rnc, (int) n);
long nbytes = in.skip(n);
RNetwork.postReceiveConnection (rnc, (int) nbytes);
return nbytes;
}
}
Naccio Home Page
University of Virginia, Computer Science