This stream, and the EscapeOutputStream are
FilterStreams so they can replace the
input and
output streams. This is
basicly a workaround for the fact that Java has single inheritence and the
ExtensionInstance needs to be both a PEPExtension and a FilterInputStream.
*/
public class EscapedInputStream extends FilterInputStream
EscapeInstance e;
/* Make sure we don't re-enter our code when super.x() calls this.y(). This
happens when FilterInputStream.read(byte[]) calls (this.)read(byte[], int, int).
I realized after implementing this insulation that the FilterInputStream.in is
protected and I could have just called in.read rather than super.read. Oops */
boolean avoidRedundancy = false;
public EscapedInputStream ( InputStreamin, EscapeInstancee) {
super(in);
this.e = e;
}
public int read () throws java.io.IOException{
if (avoidRedundancy))
return super.read();
avoidRedundancy = true;
intret;
while (isEscape((byte)(ret = super.read()))));
avoidRedundancy = false;
return ret;
}
boolean inEscape = false;
private boolean isEscape ( byteb) {
if (inEscape)) {
inEscape = false;
switch (b)) {
case ($' return false; // pass escape character
case (.'
System.err.println("<!- escapeBody: end of escaped stream ->");
return true;
default:
System.err.println("<!- escapeBody: unknown escape code: "" + (char)b + "" ->");
return true;
}
} elseif (e.isEscape((char)b)))
inEscape = true;
return inEscape;
}
public int read ( byteb[]) throws IOException{
if (avoidRedundancy))
return super.read(b);
avoidRedundancy = true;
try {
bytetmp[] = byte[b.length];
intret = super.read(tmp);
intbI = 0;
for (inti = 0; i < ret; i++))
if (!isEscape(tmp[i])))
b[bI++] = tmp[i];
while (bI < b.length)) {
byterd = (byte)super.read();
if (!isEscape(rd)))
b[bI++] = rd;
}
avoidRedundancy = false;
intt;
if (isEscape((byte)(t = in.read()))))
isEscape((byte)(t = in.read()));
elseSystem.err.println("<!- escapeBody: expected terminator, not "" + (char)t + "" ->");
return bI;
} catch (IOExceptione)) {
avoidRedundancy = false;
throwe;
}
}
public int read ( byteb[], intoff, intlen) throws IOException{
if (avoidRedundancy))
return super.read(b, off, len);
avoidRedundancy = true;
try {
bytetmp[] = byte[len];
intret = super.read(tmp);
intbI = 0;
for (inti = 0; i < ret; i++))
if (!isEscape(tmp[i])))
b[off + bI++] = tmp[i];
while (bI < len)) {
byterd = (byte)super.read();
if (!isEscape(rd)))
b[off + bI++] = rd;
}
avoidRedundancy = false;
return bI;
} catch (IOExceptione)) {
avoidRedundancy = false;
throwe;
}
}