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

Commit 82852773 authored by Andreas Gampe's avatar Andreas Gampe
Browse files

Frameworks/base: Fix old code in MediaPlayer

InputStream has been replaced with direct fd usage. Remove that
code.

Remove extra final variable, rename duped fd variable.

Close the file descriptor when done to avoid a leak.

Bug: 19797138
Change-Id: I0c7baece9501e67b80083d448c51b329653c90f6
parent d3e6428a
Loading
Loading
Loading
Loading
+8 −12
Original line number Diff line number Diff line
@@ -2187,9 +2187,9 @@ public class MediaPlayer implements SubtitleController.Listener
            throw new IllegalArgumentException("Illegal mimeType for timed text source: " + mime);
        }

        FileDescriptor fd2;
        final FileDescriptor dupedFd;
        try {
            fd2 = Libcore.os.dup(fd);
            dupedFd = Libcore.os.dup(fd);
        } catch (ErrnoException ex) {
            Log.e(TAG, ex.getMessage(), ex);
            throw new RuntimeException(ex);
@@ -2222,7 +2222,6 @@ public class MediaPlayer implements SubtitleController.Listener
        final SubtitleTrack track = mSubtitleController.addTrack(fFormat);
        mOutOfBandSubtitleTracks.add(track);

        final FileDescriptor fd3 = fd2;
        final long offset2 = offset;
        final long length2 = length;
        final HandlerThread thread = new HandlerThread(
@@ -2232,14 +2231,13 @@ public class MediaPlayer implements SubtitleController.Listener
        Handler handler = new Handler(thread.getLooper());
        handler.post(new Runnable() {
            private int addTrack() {
                InputStream is = null;
                final ByteArrayOutputStream bos = new ByteArrayOutputStream();
                try {
                    Libcore.os.lseek(fd3, offset2, OsConstants.SEEK_SET);
                    Libcore.os.lseek(dupedFd, offset2, OsConstants.SEEK_SET);
                    byte[] buffer = new byte[4096];
                    for (long total = 0; total < length2;) {
                        int bytesToRead = (int) Math.min(buffer.length, length2 - total);
                        int bytes = IoBridge.read(fd3, buffer, 0, bytesToRead);
                        int bytes = IoBridge.read(dupedFd, buffer, 0, bytesToRead);
                        if (bytes < 0) {
                            break;
                        } else {
@@ -2253,15 +2251,13 @@ public class MediaPlayer implements SubtitleController.Listener
                    Log.e(TAG, e.getMessage(), e);
                    return MEDIA_INFO_TIMED_TEXT_ERROR;
                } finally {
                    if (is != null) {
                    try {
                            is.close();
                        } catch (IOException e) {
                        Libcore.os.close(dupedFd);
                    } catch (ErrnoException e) {
                        Log.e(TAG, e.getMessage(), e);
                    }
                }
            }
            }

            public void run() {
                int res = addTrack();