Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Budiyev committed Oct 30, 2017
1 parent d92c421 commit 950264c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class FileBitmapLoader implements BitmapLoader<File> {
@Nullable
@Override
public Bitmap load(@NonNull Context context, @NonNull File data) throws Throwable {
try (InputStream inputStream = new FileInputStream(data)) {
try (InputStream inputStream = InternalUtils.buffer(new FileInputStream(data))) {
return BitmapFactory.decodeStream(inputStream);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class FileDescriptorBitmapLoader implements BitmapLoader<FileDescriptor> {
@Nullable
@Override
public Bitmap load(@NonNull Context context, @NonNull FileDescriptor data) throws Throwable {
try (InputStream inputStream = new FileInputStream(data)) {
try (InputStream inputStream = InternalUtils.buffer(new FileInputStream(data))) {
return BitmapFactory.decodeStream(inputStream);
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/budiyev/android/imageloader/InternalUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
*/
package com.budiyev.android.imageloader;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import android.content.Context;
Expand All @@ -40,10 +43,21 @@ final class InternalUtils {
private static final String URI_SCHEME_HTTP = "http";
private static final String URI_SCHEME_HTTPS = "https";
private static final String URI_SCHEME_FTP = "ftp";
private static final int BUFFER_SIZE = 32768;

private InternalUtils() {
}

@NonNull
public static InputStream buffer(@NonNull InputStream inputStream) {
return new BufferedInputStream(inputStream, BUFFER_SIZE);
}

@NonNull
public static OutputStream buffer(@NonNull OutputStream outputStream) {
return new BufferedOutputStream(outputStream, BUFFER_SIZE);
}

@Nullable
@MainThread
public static LoadImageAction<?> getLoadImageAction(@Nullable ImageView view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Bitmap load(@NonNull Context context, @NonNull Integer data) throws Throw
} else if (typedValue.density != TypedValue.DENSITY_NONE) {
options.inDensity = typedValue.density;
}
InputStream inputStream = resources.openRawResource(data, typedValue);
InputStream inputStream = InternalUtils.buffer(resources.openRawResource(data, typedValue));
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
InternalUtils.close(inputStream);
return bitmap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void put(@NonNull String key, @NonNull Bitmap value) {
if (file.exists()) {
file.delete();
}
try (OutputStream outputStream = new FileOutputStream(file)) {
try (OutputStream outputStream = InternalUtils.buffer(new FileOutputStream(file))) {
value.compress(mCompressMode.getFormat(), mCompressMode.getQuality(), outputStream);
} catch (IOException e) {
if (file.exists()) {
Expand All @@ -117,7 +117,7 @@ public void put(@NonNull String key, @NonNull Bitmap value) {
public Bitmap get(@NonNull String key) {
File file = new File(mDirectory, key);
file.setLastModified(System.currentTimeMillis());
try (InputStream inputStream = new FileInputStream(file)) {
try (InputStream inputStream = InternalUtils.buffer(new FileInputStream(file))) {
return BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ final class UriBitmapLoader implements BitmapLoader<Uri> {
@Nullable
@Override
public Bitmap load(@NonNull Context context, @NonNull Uri data) throws Throwable {
try (InputStream inputStream = InternalUtils.getDataStreamFromUri(context, data)) {
return BitmapFactory.decodeStream(inputStream);
InputStream dataStream = InternalUtils.getDataStreamFromUri(context, data);
if (dataStream == null) {
return null;
}
try (InputStream stream = InternalUtils.buffer(dataStream)) {
return BitmapFactory.decodeStream(stream);
}
}
}

0 comments on commit 950264c

Please sign in to comment.