Search in sources :

Example 1 with CountingOutputStream

use of i2p.susi.util.CountingOutputStream 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();
}
Also used : MemoryBuffer(i2p.susi.util.MemoryBuffer) OutputStreamBuffer(i2p.susi.util.OutputStreamBuffer) Buffer(i2p.susi.util.Buffer) ReadBuffer(i2p.susi.util.ReadBuffer) OutputStreamBuffer(i2p.susi.util.OutputStreamBuffer) CountingOutputStream(i2p.susi.util.CountingOutputStream) LimitInputStream(i2p.susi.util.LimitInputStream) EOFOnMatchInputStream(i2p.susi.util.EOFOnMatchInputStream) InputStream(java.io.InputStream) Encoding(i2p.susi.webmail.encoding.Encoding) DecodingException(i2p.susi.webmail.encoding.DecodingException) LimitInputStream(i2p.susi.util.LimitInputStream) IOException(java.io.IOException)

Aggregations

Buffer (i2p.susi.util.Buffer)1 CountingOutputStream (i2p.susi.util.CountingOutputStream)1 EOFOnMatchInputStream (i2p.susi.util.EOFOnMatchInputStream)1 LimitInputStream (i2p.susi.util.LimitInputStream)1 MemoryBuffer (i2p.susi.util.MemoryBuffer)1 OutputStreamBuffer (i2p.susi.util.OutputStreamBuffer)1 ReadBuffer (i2p.susi.util.ReadBuffer)1 DecodingException (i2p.susi.webmail.encoding.DecodingException)1 Encoding (i2p.susi.webmail.encoding.Encoding)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1