From 42ac0d00153cf8cd4c89cb9397c937be4e8f36e3 Mon Sep 17 00:00:00 2001 From: Jaesung Chung Date: Tue, 22 Mar 2016 11:43:11 -0700 Subject: [PATCH] ExifInterface: fix regression of re-writing JPEG Bug: 27583378 Change-Id: I052dfedbb1393705eba378de5eaf3636ed0e65ea --- media/java/android/media/ExifInterface.java | 10 ++-------- .../mediaframeworktest/unit/ExifInterfaceTest.java | 8 +++++--- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/media/java/android/media/ExifInterface.java b/media/java/android/media/ExifInterface.java index dbedf340096ff..83a6c74dc98cb 100644 --- a/media/java/android/media/ExifInterface.java +++ b/media/java/android/media/ExifInterface.java @@ -1191,10 +1191,6 @@ public class ExifInterface { ++bytesRead; while (true) { marker = dataInputStream.readByte(); - if (marker == -1) { - Log.w(TAG, "Reading JPEG has ended unexpectedly"); - break; - } if (marker != MARKER) { throw new IOException("Invalid marker:" + Integer.toHexString(marker & 0xff)); } @@ -1329,10 +1325,6 @@ public class ExifInterface { while (true) { byte marker = dataInputStream.readByte(); - if (marker == -1) { - Log.w(TAG, "Reading JPEG has ended unexpectedly"); - break; - } if (marker != MARKER) { throw new IOException("Invalid marker"); } @@ -1357,6 +1349,8 @@ public class ExifInterface { } } // Copy non-EXIF APP1 segment. + dataOutputStream.writeByte(MARKER); + dataOutputStream.writeByte(marker); dataOutputStream.writeUnsignedShort(length + 2); if (length >= 6) { length -= 6; diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/ExifInterfaceTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/ExifInterfaceTest.java index 5bd607922c813..6207f7d59809a 100644 --- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/ExifInterfaceTest.java +++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/ExifInterfaceTest.java @@ -417,13 +417,15 @@ public class ExifInterfaceTest extends AndroidTestCase { testExifInterfaceForRaw(LG_G4_ISO_800_DNG, R.array.lg_g4_iso_800_dng); } - public void testCorruptedImage() throws Throwable { + public void testDoNotFailOnCorruptedImage() throws Throwable { + // To keep the compatibility with old versions of ExifInterface, even on a corrupted image, + // it shouldn't raise any exceptions except an IOException when unable to open a file. byte[] bytes = new byte[1024]; try { new ExifInterface(new ByteArrayInputStream(bytes)); - fail("Should not reach here!"); + // Always success } catch (IOException e) { - // Success + fail("Should not reach here!"); } }