socket leak fix

* fix socket leak in "DownloadRunnable"
* in "DownloadInitializer" close the HTTP body after doing range-request checks
* in "DownloadRunnableFallback" fix typo in comment
* in "DownloadDialog" fix regression, using one thread for audios instead of subtitles
This commit is contained in:
kapodamy 2019-08-01 22:41:09 -03:00
parent e529b16956
commit d9b042d9e3
4 changed files with 31 additions and 20 deletions

View File

@ -773,7 +773,6 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck
// more download logic: select muxer, subtitle converter, etc. // more download logic: select muxer, subtitle converter, etc.
switch (radioStreamsGroup.getCheckedRadioButtonId()) { switch (radioStreamsGroup.getCheckedRadioButtonId()) {
case R.id.audio_button: case R.id.audio_button:
threads = 1;// use unique thread for subtitles due small file size
kind = 'a'; kind = 'a';
selectedStream = audioStreamsAdapter.getItem(selectedAudioIndex); selectedStream = audioStreamsAdapter.getItem(selectedAudioIndex);
@ -808,6 +807,7 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck
} }
break; break;
case R.id.subtitle_button: case R.id.subtitle_button:
threads = 1;// use unique thread for subtitles due small file size
kind = 's'; kind = 's';
selectedStream = subtitleStreamsAdapter.getItem(selectedSubtitleIndex); selectedStream = subtitleStreamsAdapter.getItem(selectedSubtitleIndex);

View File

@ -28,11 +28,21 @@ public class DownloadInitializer extends Thread {
mConn = null; mConn = null;
} }
private static void safeClose(HttpURLConnection con) {
try {
con.getInputStream().close();
} catch (Exception e) {
// nothing to do
}
}
@Override @Override
public void run() { public void run() {
if (mMission.current > 0) mMission.resetState(false, true, DownloadMission.ERROR_NOTHING); if (mMission.current > 0) mMission.resetState(false, true, DownloadMission.ERROR_NOTHING);
int retryCount = 0; int retryCount = 0;
int httpCode = 204;
while (true) { while (true) {
try { try {
if (mMission.blocks == null && mMission.current == 0) { if (mMission.blocks == null && mMission.current == 0) {
@ -43,11 +53,16 @@ public class DownloadInitializer extends Thread {
for (int i = 0; i < mMission.urls.length && mMission.running; i++) { for (int i = 0; i < mMission.urls.length && mMission.running; i++) {
mConn = mMission.openConnection(mMission.urls[i], mId, -1, -1); mConn = mMission.openConnection(mMission.urls[i], mId, -1, -1);
mMission.establishConnection(mId, mConn); mMission.establishConnection(mId, mConn);
safeClose(mConn);
if (Thread.interrupted()) return; if (Thread.interrupted()) return;
long length = Utility.getContentLength(mConn); long length = Utility.getContentLength(mConn);
if (i == 0) mMission.length = length; if (i == 0) {
httpCode = mConn.getResponseCode();
mMission.length = length;
}
if (length > 0) finalLength += length; if (length > 0) finalLength += length;
if (length < lowestSize) lowestSize = length; if (length < lowestSize) lowestSize = length;
} }
@ -68,13 +83,15 @@ public class DownloadInitializer extends Thread {
// ask for the current resource length // ask for the current resource length
mConn = mMission.openConnection(mId, -1, -1); mConn = mMission.openConnection(mId, -1, -1);
mMission.establishConnection(mId, mConn); mMission.establishConnection(mId, mConn);
safeClose(mConn);
if (!mMission.running || Thread.interrupted()) return; if (!mMission.running || Thread.interrupted()) return;
httpCode = mConn.getResponseCode();
mMission.length = Utility.getContentLength(mConn); mMission.length = Utility.getContentLength(mConn);
} }
if (mMission.length == 0 || mConn.getResponseCode() == 204) { if (mMission.length == 0 || httpCode == 204) {
mMission.notifyError(DownloadMission.ERROR_HTTP_NO_CONTENT, null); mMission.notifyError(DownloadMission.ERROR_HTTP_NO_CONTENT, null);
return; return;
} }
@ -92,6 +109,7 @@ public class DownloadInitializer extends Thread {
// Open again // Open again
mConn = mMission.openConnection(mId, mMission.length - 10, mMission.length); mConn = mMission.openConnection(mId, mMission.length - 10, mMission.length);
mMission.establishConnection(mId, mConn); mMission.establishConnection(mId, mConn);
safeClose(mConn);
if (!mMission.running || Thread.interrupted()) return; if (!mMission.running || Thread.interrupted()) return;

View File

@ -49,7 +49,6 @@ public class DownloadRunnable extends Thread {
} }
SharpStream f; SharpStream f;
InputStream is = null;
try { try {
f = mMission.storage.getStream(); f = mMission.storage.getStream();
@ -114,16 +113,16 @@ public class DownloadRunnable extends Thread {
f.seek(mMission.offsets[mMission.current] + start); f.seek(mMission.offsets[mMission.current] + start);
is = mConn.getInputStream(); try (InputStream is = mConn.getInputStream()) {
byte[] buf = new byte[DownloadMission.BUFFER_SIZE];
int len;
byte[] buf = new byte[DownloadMission.BUFFER_SIZE]; while (start < end && mMission.running && (len = is.read(buf, 0, buf.length)) != -1) {
int len; f.write(buf, 0, len);
start += len;
while (start < end && mMission.running && (len = is.read(buf, 0, buf.length)) != -1) { block.done += len;
f.write(buf, 0, len); mMission.notifyProgress(len);
start += len; }
block.done += len;
mMission.notifyProgress(len);
} }
if (DEBUG && mMission.running) { if (DEBUG && mMission.running) {
@ -143,12 +142,6 @@ public class DownloadRunnable extends Thread {
} }
} }
try {
if (is != null) is.close();
} catch (Exception err) {
// nothing to do
}
try { try {
f.close(); f.close();
} catch (Exception err) { } catch (Exception err) {

View File

@ -94,7 +94,7 @@ public class DownloadRunnableFallback extends Thread {
mMission.notifyProgress(len); mMission.notifyProgress(len);
} }
// if thread goes interrupted check if the last part mIs written. This avoid re-download the whole file // if thread goes interrupted check if the last part is written. This avoid re-download the whole file
done = len == -1; done = len == -1;
} catch (Exception e) { } catch (Exception e) {
dispose(); dispose();