Search in sources :

Example 1 with FixedLengthInputStream

use of com.fsck.k9.mail.filter.FixedLengthInputStream in project k-9 by k9mail.

the class ImapResponseParser method parseLiteral.

/**
 * A "{" has been read. Read the rest of the size string, the space and then notify the callback with an
 * {@code InputStream}.
 */
private Object parseLiteral() throws IOException {
    expect('{');
    int size = Integer.parseInt(readStringUntil('}'));
    expect('\r');
    expect('\n');
    if (size == 0) {
        return "";
    }
    if (response.getCallback() != null) {
        FixedLengthInputStream fixed = new FixedLengthInputStream(inputStream, size);
        Exception callbackException = null;
        Object result = null;
        try {
            result = response.getCallback().foundLiteral(response, fixed);
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            callbackException = e;
        }
        boolean someDataWasRead = fixed.available() != size;
        if (someDataWasRead) {
            if (result == null && callbackException == null) {
                throw new AssertionError("Callback consumed some data but returned no result");
            }
            fixed.skipRemaining();
        }
        if (callbackException != null) {
            if (exception == null) {
                exception = callbackException;
            }
            return "EXCEPTION";
        }
        if (result != null) {
            return result;
        }
    }
    byte[] data = new byte[size];
    int read = 0;
    while (read != size) {
        int count = inputStream.read(data, read, size - read);
        if (count == -1) {
            throw new IOException("parseLiteral(): end of stream reached");
        }
        read += count;
    }
    return new String(data, "US-ASCII");
}
Also used : FixedLengthInputStream(com.fsck.k9.mail.filter.FixedLengthInputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

FixedLengthInputStream (com.fsck.k9.mail.filter.FixedLengthInputStream)1 IOException (java.io.IOException)1