QPEncoderStreampublic class QPEncoderStream extends FilterOutputStream This class implements a Quoted Printable Encoder. It is implemented as
a FilterOutputStream, so one can just wrap this class around
any output stream and write bytes into this filter. The Encoding
is done as the bytes are written out. |
Fields Summary |
---|
private int | count | private int | bytesPerLine | private boolean | gotSpace | private boolean | gotCR | private static final char[] | hex |
Constructors Summary |
---|
public QPEncoderStream(OutputStream out, int bytesPerLine)Create a QP encoder that encodes the specified input stream
super(out);
// Subtract 1 to account for the '=' in the soft-return
// at the end of a line
this.bytesPerLine = bytesPerLine - 1;
| public QPEncoderStream(OutputStream out)Create a QP encoder that encodes the specified input stream.
Inserts the CRLF sequence after outputting 76 bytes.
this(out, 76);
|
Methods Summary |
---|
public void | close()Forces any buffered output bytes to be encoded out to the stream
and closes this output stream
if (gotSpace) {
output(' ", true);
gotSpace = false;
}
out.close();
| public void | flush()Flushes this output stream and forces any buffered output bytes
to be encoded out to the stream.
out.flush();
| protected void | output(int c, boolean encode)
if (encode) {
if ((count += 3) > bytesPerLine) {
out.write('=");
out.write('\r");
out.write('\n");
count = 3; // set the next line's length
}
out.write('=");
out.write(hex[c >> 4]);
out.write(hex[c & 0xf]);
} else {
if (++count > bytesPerLine) {
out.write('=");
out.write('\r");
out.write('\n");
count = 1; // set the next line's length
}
out.write(c);
}
| private void | outputCRLF()
out.write('\r");
out.write('\n");
count = 0;
| public void | write(byte[] b, int off, int len)Encodes len bytes from the specified
byte array starting at offset off to
this output stream.
for (int i = 0; i < len; i++)
write(b[off + i]);
| public void | write(byte[] b)Encodes b.length bytes to this output stream.
write(b, 0, b.length);
| public void | write(int c)Encodes the specified byte to this output stream.
c = c & 0xff; // Turn off the MSB.
if (gotSpace) { // previous character was <SPACE>
if (c == '\r" || c == '\n")
// if CR/LF, we need to encode the <SPACE> char
output(' ", true);
else // no encoding required, just output the char
output(' ", false);
gotSpace = false;
}
if (c == '\r") {
gotCR = true;
outputCRLF();
} else {
if (c == '\n") {
if (gotCR)
// This is a CRLF sequence, we already output the
// corresponding CRLF when we got the CR, so ignore this
;
else
outputCRLF();
} else if (c == ' ") {
gotSpace = true;
} else if (c < 040 || c >= 0177 || c == '=")
// Encoding required.
output(c, true);
else // No encoding required
output(c, false);
// whatever it was, it wasn't a CR
gotCR = false;
}
|
|