Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 3c96aef7 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #5779 from johnjohndoe/CountingOutputStream

Avoid leaking CountingOutputStream.
parents c0c0e05a f0d3f371
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -21,10 +21,8 @@ class ProgressBodyFactory extends DefaultBodyFactory {

    @Override
    protected void copyData(InputStream inputStream, OutputStream outputStream) throws IOException {
        final CountingOutputStream countingOutputStream = new CountingOutputStream(outputStream);

        Timer timer = new Timer();
        try {
        try (CountingOutputStream countingOutputStream = new CountingOutputStream(outputStream)) {
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
+6 −11
Original line number Diff line number Diff line
@@ -785,21 +785,16 @@ public class LocalFolder {
    }

    private long decodeAndCountBytes(InputStream rawInputStream, String encoding, long fallbackValue) {
        InputStream decodingInputStream = localStore.getDecodingInputStream(rawInputStream, encoding);
        try {
            CountingOutputStream countingOutputStream = new CountingOutputStream();
            try {
                IOUtils.copy(decodingInputStream, countingOutputStream);
        try (InputStream decodingInputStream = localStore.getDecodingInputStream(rawInputStream, encoding)) {

            try (CountingOutputStream countingOutputStream = new CountingOutputStream()) {
                IOUtils.copy(decodingInputStream, countingOutputStream);
                return countingOutputStream.getCount();
            }

        } catch (IOException e) {
            return fallbackValue;
        }
        } finally {
            try {
                decodingInputStream.close();
            } catch (IOException e) { /* ignore */ }
        }
    }

    private byte[] getHeaderBytes(Part part) throws IOException, MessagingException {
+1 −2
Original line number Diff line number Diff line
@@ -99,8 +99,7 @@ public class RawMessageProvider extends ContentProvider {

    private long computeMessageSize(LocalMessage message) {
        // TODO: Store message size in database when saving message so this can be a simple lookup instead.
        try {
            CountingOutputStream countingOutputStream = new CountingOutputStream();
        try (CountingOutputStream countingOutputStream = new CountingOutputStream()) {
            message.writeTo(countingOutputStream);
            return countingOutputStream.getCount();
        } catch (IOException | MessagingException e) {
+4 −3
Original line number Diff line number Diff line
@@ -305,10 +305,11 @@ internal class SaveMessageOperations(
    private fun decodeAndCountBytes(rawInputStream: InputStream, encoding: String, fallbackValue: Long): Long {
        return try {
            getDecodingInputStream(rawInputStream, encoding).use { decodingInputStream ->
                val countingOutputStream = CountingOutputStream()
                CountingOutputStream().use { countingOutputStream ->
                    IOUtils.copy(decodingInputStream, countingOutputStream)
                    countingOutputStream.count
                }
            }
        } catch (e: IOException) {
            fallbackValue
        }
+2 −6
Original line number Diff line number Diff line
@@ -159,16 +159,12 @@ public abstract class Message implements Part, Body {
    public abstract void setEncoding(String encoding) throws MessagingException;

    public long calculateSize() {
        try {

            CountingOutputStream out = new CountingOutputStream();
        try (CountingOutputStream out = new CountingOutputStream()) {
            EOLConvertingOutputStream eolOut = new EOLConvertingOutputStream(out);
            writeTo(eolOut);
            eolOut.flush();
            return out.getCount();
        } catch (IOException e) {
            Timber.e(e, "Failed to calculate a message size");
        } catch (MessagingException e) {
        } catch (IOException | MessagingException e) {
            Timber.e(e, "Failed to calculate a message size");
        }
        return 0;
Loading