From 25ddd763e0331b4a046a8a885dffeac61f74949f Mon Sep 17 00:00:00 2001 From: Jacksgong Date: Fri, 20 Apr 2018 22:14:06 +0800 Subject: [PATCH] fix: fix fd isn't released manually when download finished which may raise oom on case of there are many tasks running one by one --- .../okdownload/core/file/DownloadUriOutputStream.java | 10 +++++++--- .../core/file/DownloadUriOutputStreamTest.java | 5 ++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/okdownload/src/main/java/com/liulishuo/okdownload/core/file/DownloadUriOutputStream.java b/okdownload/src/main/java/com/liulishuo/okdownload/core/file/DownloadUriOutputStream.java index 2fa07b5e..b3df40e2 100644 --- a/okdownload/src/main/java/com/liulishuo/okdownload/core/file/DownloadUriOutputStream.java +++ b/okdownload/src/main/java/com/liulishuo/okdownload/core/file/DownloadUriOutputStream.java @@ -37,6 +37,7 @@ public class DownloadUriOutputStream implements DownloadOutputStream { @NonNull private final FileChannel channel; @NonNull private final ParcelFileDescriptor pdf; @NonNull private final BufferedOutputStream out; + @NonNull private final FileOutputStream fos; public DownloadUriOutputStream(Context context, Uri uri, int bufferSize) throws FileNotFoundException { @@ -44,15 +45,17 @@ public DownloadUriOutputStream(Context context, Uri uri, int bufferSize) throws if (pdf == null) throw new FileNotFoundException("result of " + uri + " is null!"); this.pdf = pdf; - final FileOutputStream fos = new FileOutputStream(pdf.getFileDescriptor()); - channel = fos.getChannel(); - out = new BufferedOutputStream(fos, bufferSize); + this.fos = new FileOutputStream(pdf.getFileDescriptor()); + this.channel = fos.getChannel(); + this.out = new BufferedOutputStream(fos, bufferSize); } DownloadUriOutputStream(@NonNull FileChannel channel, @NonNull ParcelFileDescriptor pdf, + @NonNull FileOutputStream fos, @NonNull BufferedOutputStream out) { this.channel = channel; this.pdf = pdf; + this.fos = fos; this.out = out; } @@ -64,6 +67,7 @@ public void write(byte[] b, int off, int len) throws IOException { @Override public void close() throws IOException { out.close(); + fos.close(); } @Override diff --git a/okdownload/src/test/java/com/liulishuo/okdownload/core/file/DownloadUriOutputStreamTest.java b/okdownload/src/test/java/com/liulishuo/okdownload/core/file/DownloadUriOutputStreamTest.java index 2baa03f9..82ce10d6 100644 --- a/okdownload/src/test/java/com/liulishuo/okdownload/core/file/DownloadUriOutputStreamTest.java +++ b/okdownload/src/test/java/com/liulishuo/okdownload/core/file/DownloadUriOutputStreamTest.java @@ -29,6 +29,7 @@ import java.io.BufferedOutputStream; import java.io.FileDescriptor; +import java.io.FileOutputStream; import java.io.SyncFailedException; import java.nio.channels.FileChannel; @@ -46,6 +47,7 @@ public class DownloadUriOutputStreamTest { @Mock private FileChannel channel; @Mock private ParcelFileDescriptor pdf; @Mock private BufferedOutputStream out; + @Mock private FileOutputStream fos; @Mock private FileDescriptor fd; private DownloadUriOutputStream outputStream; @@ -54,7 +56,7 @@ public class DownloadUriOutputStreamTest { public void setup() { initMocks(this); - outputStream = new DownloadUriOutputStream(channel, pdf, out); + outputStream = new DownloadUriOutputStream(channel, pdf, fos, out); } @Test @@ -68,6 +70,7 @@ public void write() throws Exception { public void close() throws Exception { outputStream.close(); verify(out).close(); + verify(fos).close(); } @Rule