-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid wrapping byte[] and ByteBuffer in streams.
The streams use a buffered stream where the limited size buffer can cause failures if parsing metadata requries reading past the buffer size. This is unnecessary for these two types because both already have all data in memory or otherwise handle buffering internally. Avoiding the buffer also saves some memory overhead.
- Loading branch information
Showing
8 changed files
with
166 additions
and
9 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
instrumentation/src/androidTest/java/com/bumptech/glide/LargeImageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.bumptech.glide; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import android.content.ContentResolver; | ||
import android.content.Context; | ||
import android.content.res.Resources; | ||
import android.graphics.Bitmap; | ||
import android.net.Uri; | ||
import androidx.test.core.app.ApplicationProvider; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import com.bumptech.glide.load.model.UnitModelLoader; | ||
import com.bumptech.glide.test.ConcurrencyHelper; | ||
import com.bumptech.glide.test.ResourceIds; | ||
import com.bumptech.glide.test.TearDownGlide; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.ByteBuffer; | ||
import java.util.Objects; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TestRule; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class LargeImageTest { | ||
@Rule public final TestRule tearDownGlide = new TearDownGlide(); | ||
private final ConcurrencyHelper concurrency = new ConcurrencyHelper(); | ||
private final Context context = ApplicationProvider.getApplicationContext(); | ||
|
||
@Test | ||
public void loadLargeJpeg_asByteArray_succeeds() throws IOException { | ||
byte[] data = getLargeImageBytes(); | ||
Bitmap bitmap = concurrency.get(Glide.with(context).asBitmap().load(data).submit()); | ||
assertThat(bitmap).isNotNull(); | ||
} | ||
|
||
@Test | ||
public void loadLargeJpeg_asByteBuffer_succeeds() throws IOException { | ||
// Using UnitModelLoader lets us mimic loading the ByteBuffer from some other data type, which | ||
// reduces the scope of our test. | ||
Glide.get(context) | ||
.getRegistry() | ||
.append( | ||
ByteBuffer.class, ByteBuffer.class, UnitModelLoader.Factory.<ByteBuffer>getInstance()); | ||
|
||
ByteBuffer buffer = ByteBuffer.wrap(getLargeImageBytes()); | ||
Bitmap bitmap = concurrency.get(Glide.with(context).asBitmap().load(buffer).submit()); | ||
assertThat(bitmap).isNotNull(); | ||
} | ||
|
||
private byte[] getLargeImageBytes() throws IOException { | ||
Resources resources = context.getResources(); | ||
int resourceId = ResourceIds.raw.canonical_large; | ||
Uri uri = | ||
new Uri.Builder() | ||
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) | ||
.authority(resources.getResourcePackageName(resourceId)) | ||
.appendPath(resources.getResourceTypeName(resourceId)) | ||
.appendPath(resources.getResourceEntryName(resourceId)) | ||
.build(); | ||
|
||
InputStream is = Objects.requireNonNull(context.getContentResolver().openInputStream(uri)); | ||
ByteArrayOutputStream os = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[1024 * 1024 * 5]; | ||
int read; | ||
while ((read = is.read(buffer, 0, buffer.length)) != -1) { | ||
os.write(buffer, 0, read); | ||
} | ||
return os.toByteArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters