use of i2p.susi.util.LimitInputStream in project i2p.i2p by i2p.
the class MailPart method decode.
/**
* @param offset 2 for sendAttachment, 0 otherwise, probably for \r\n
* @since 0.9.13
*/
public void decode(int offset, Buffer out) throws IOException {
String encg = encoding;
if (encg == null) {
// throw new DecodingException("No encoding specified");
Debug.debug(Debug.DEBUG, "Warning: no transfer encoding found, fallback to 7bit.");
encg = "7bit";
}
Encoding enc = EncodingFactory.getEncoding(encg);
if (enc == null)
throw new DecodingException(_t("No encoder found for encoding \\''{0}\\''.", WebMail.quoteHTML(encg)));
InputStream in = null;
LimitInputStream lin = null;
CountingOutputStream cos = null;
Buffer dout = null;
try {
in = buffer.getInputStream();
DataHelper.skip(in, buffer.getOffset() + beginBody + offset);
lin = new LimitInputStream(in, end - beginBody - offset);
if (decodedLength < 0) {
cos = new CountingOutputStream(out.getOutputStream());
dout = new OutputStreamBuffer(cos);
} else {
dout = out;
}
enc.decode(lin, dout);
// dout.getOutputStream().flush();
} catch (IOException ioe) {
if (lin != null)
Debug.debug(Debug.DEBUG, "Decode IOE at in position " + lin.getRead() + " offset " + offset, ioe);
else if (cos != null)
Debug.debug(Debug.DEBUG, "Decode IOE at out position " + cos.getWritten() + " offset " + offset, ioe);
else
Debug.debug(Debug.DEBUG, "Decode IOE", ioe);
throw ioe;
} finally {
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
;
if (lin != null)
try {
lin.close();
} catch (IOException ioe) {
}
;
buffer.readComplete(true);
// let the servlet do this
// if (cos != null) try { cos.close(); } catch (IOException ioe) {};
// if (dout != null)
// dout.writeComplete(true);
// out.writeComplete(true);
}
if (cos != null)
decodedLength = (int) cos.getWritten();
}
Aggregations