Skip to content

Commit

Permalink
Use the new BufferedOutputStream when encoding Bitmaps.
Browse files Browse the repository at this point in the history
Deprecates the BitmapEncoder constructor that does not take an
ArrayPool.
  • Loading branch information
sjudd committed Jan 9, 2018
1 parent a0f3d30 commit 860f7d0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private static void throwIncorrectGlideModule(Exception e) {
new ResourceLoader.FileDescriptorFactory(resources);
ResourceLoader.AssetFileDescriptorFactory resourceLoaderAssetFileDescriptorFactory =
new ResourceLoader.AssetFileDescriptorFactory(resources);
BitmapEncoder bitmapEncoder = new BitmapEncoder();
BitmapEncoder bitmapEncoder = new BitmapEncoder(arrayPool);

BitmapBytesTranscoder bitmapBytesTranscoder = new BitmapBytesTranscoder();
GifDrawableBytesTranscoder gifDrawableBytesTranscoder = new GifDrawableBytesTranscoder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.os.TraceCompat;
import android.util.Log;
import com.bumptech.glide.load.EncodeStrategy;
import com.bumptech.glide.load.Option;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.ResourceEncoder;
import com.bumptech.glide.load.data.BufferedOutputStream;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool;
import com.bumptech.glide.util.LogTime;
import com.bumptech.glide.util.Util;
import java.io.File;
Expand Down Expand Up @@ -49,6 +52,20 @@ public class BitmapEncoder implements ResourceEncoder<Bitmap> {
"com.bumptech.glide.load.resource.bitmap.BitmapEncoder.CompressionFormat");

private static final String TAG = "BitmapEncoder";
@Nullable
private final ArrayPool arrayPool;

public BitmapEncoder(@NonNull ArrayPool arrayPool) {
this.arrayPool = arrayPool;
}

/**
* @deprecated Use {@link #BitmapEncoder(ArrayPool)} instead.
*/
@Deprecated
public BitmapEncoder() {
arrayPool = null;
}

@Override
public boolean encode(@NonNull Resource<Bitmap> resource, @NonNull File file,
Expand All @@ -65,6 +82,9 @@ public boolean encode(@NonNull Resource<Bitmap> resource, @NonNull File file,
OutputStream os = null;
try {
os = new FileOutputStream(file);
if (arrayPool != null) {
os = new BufferedOutputStream(os, arrayPool);
}
bitmap.compress(format, quality, os);
os.close();
success = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.bumptech.glide.load.EncodeStrategy;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool;
import com.bumptech.glide.load.engine.bitmap_recycle.LruArrayPool;
import com.bumptech.glide.util.ByteBufferUtil;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -86,13 +88,13 @@ public void testEncoderEncodesPngWithNullFormatAndBitmapWithAlpha() throws IOExc

@Test
public void testReturnsTrueFromWrite() {
BitmapEncoder encoder = new BitmapEncoder();
BitmapEncoder encoder = new BitmapEncoder(harness.arrayPool);
assertTrue(encoder.encode(harness.resource, harness.file, harness.options));
}

@Test
public void testEncodeStrategy_alwaysReturnsTransformed() {
BitmapEncoder encoder = new BitmapEncoder();
BitmapEncoder encoder = new BitmapEncoder(harness.arrayPool);
assertEquals(EncodeStrategy.TRANSFORMED, encoder.getEncodeStrategy(harness.options));
}

Expand All @@ -105,6 +107,7 @@ private static class EncoderHarness {
final Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
final Options options = new Options();
final File file = new File(RuntimeEnvironment.application.getCacheDir(), "test");
final ArrayPool arrayPool = new LruArrayPool();

EncoderHarness() {
when(resource.get()).thenReturn(bitmap);
Expand All @@ -119,7 +122,7 @@ void setFormat(Bitmap.CompressFormat format) {
}

String encode() throws IOException {
BitmapEncoder encoder = new BitmapEncoder();
BitmapEncoder encoder = new BitmapEncoder(arrayPool);
encoder.encode(resource, file, options);
byte[] data = ByteBufferUtil.toBytes(ByteBufferUtil.fromFile(file));
return new String(data, "UTF-8");
Expand Down

0 comments on commit 860f7d0

Please sign in to comment.