Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉迷打码的小凳子
glide
提交
2007440c
G
glide
项目概览
沉迷打码的小凳子
/
glide
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2007440c
编写于
11月 03, 2014
作者:
S
Sam Judd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add API to fix mark limit/buffer size.
Fixes #225.
上级
17180f4c
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
58 addition
and
17 deletion
+58
-17
library/src/androidTest/java/com/bumptech/glide/util/ExceptionCatchingInputStreamTest.java
...bumptech/glide/util/ExceptionCatchingInputStreamTest.java
+10
-2
library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java
...ptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java
+4
-0
library/src/main/java/com/bumptech/glide/load/resource/bitmap/Downsampler.java
.../com/bumptech/glide/load/resource/bitmap/Downsampler.java
+13
-7
library/src/main/java/com/bumptech/glide/load/resource/bitmap/RecyclableBufferedInputStream.java
...e/load/resource/bitmap/RecyclableBufferedInputStream.java
+22
-5
library/src/main/java/com/bumptech/glide/util/ExceptionCatchingInputStream.java
...com/bumptech/glide/util/ExceptionCatchingInputStream.java
+9
-3
未找到文件。
library/src/androidTest/java/com/bumptech/glide/util/ExceptionCatchingInputStreamTest.java
浏览文件 @
2007440c
package
com.bumptech.glide.util
;
import
com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
...
...
@@ -24,12 +25,12 @@ import static org.mockito.Mockito.when;
@RunWith
(
JUnit4
.
class
)
public
class
ExceptionCatchingInputStreamTest
{
private
InputStream
wrapped
;
private
RecyclableBuffered
InputStream
wrapped
;
private
ExceptionCatchingInputStream
is
;
@Before
public
void
setUp
()
throws
Exception
{
wrapped
=
mock
(
InputStream
.
class
);
wrapped
=
mock
(
RecyclableBuffered
InputStream
.
class
);
is
=
new
ExceptionCatchingInputStream
();
is
.
setInputStream
(
wrapped
);
}
...
...
@@ -93,6 +94,13 @@ public class ExceptionCatchingInputStreamTest {
verify
(
wrapped
).
reset
();
}
@Test
public
void
testFixMarkLimitCallsFixMarkLimitOnWrappedStream
()
{
is
.
fixMarkLimit
();
verify
(
wrapped
).
fixMarkLimit
();
}
@Test
public
void
testCallsSkipOnWrapped
()
throws
IOException
{
long
toSkip
=
67
;
...
...
library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java
浏览文件 @
2007440c
...
...
@@ -63,6 +63,10 @@ public class LruBitmapPool implements BitmapPool {
@Override
public
synchronized
boolean
put
(
Bitmap
bitmap
)
{
if
(!
bitmap
.
isMutable
()
||
strategy
.
getSize
(
bitmap
)
>
maxSize
)
{
if
(
Log
.
isLoggable
(
TAG
,
Log
.
VERBOSE
))
{
Log
.
v
(
TAG
,
"Reject bitmap from pool="
+
strategy
.
logBitmap
(
bitmap
)
+
" is mutable="
+
bitmap
.
isMutable
());
}
return
false
;
}
...
...
library/src/main/java/com/bumptech/glide/load/resource/bitmap/Downsampler.java
浏览文件 @
2007440c
...
...
@@ -105,6 +105,7 @@ public abstract class Downsampler implements BitmapDecoder<InputStream> {
final
byte
[]
bytesForOptions
=
byteArrayPool
.
getBytes
();
final
byte
[]
bytesForStream
=
byteArrayPool
.
getBytes
();
final
BitmapFactory
.
Options
options
=
getDefaultOptions
();
// TODO(#126): when the framework handles exceptions better, consider removing.
final
ExceptionCatchingInputStream
stream
=
ExceptionCatchingInputStream
.
obtain
(
new
RecyclableBufferedInputStream
(
is
,
bytesForStream
));
...
...
@@ -137,9 +138,10 @@ public abstract class Downsampler implements BitmapDecoder<InputStream> {
final
int
sampleSize
=
getRoundedSampleSize
(
degreesToRotate
,
inWidth
,
inHeight
,
outWidth
,
outHeight
);
final
Bitmap
downsampled
=
downsampleWithSize
(
stream
,
options
,
pool
,
inWidth
,
inHeight
,
sampleSize
,
decodeFormat
);
downsampleWithSize
(
stream
,
options
,
pool
,
inWidth
,
inHeight
,
sampleSize
,
decodeFormat
);
// Bitmap
Decoder
swallows exceptions during decodes and in some cases when inBitmap is non null, may catch
// Bitmap
Factory
swallows exceptions during decodes and in some cases when inBitmap is non null, may catch
// and log a stack trace but still return a non null bitmap. To avoid displaying partially decoded bitmaps,
// we catch exceptions reading from the stream in our ExceptionCatchingInputStream and throw them here.
final
Exception
streamException
=
stream
.
getException
();
...
...
@@ -185,8 +187,8 @@ public abstract class Downsampler implements BitmapDecoder<InputStream> {
return
Math
.
max
(
1
,
powerOfTwoSampleSize
);
}
pr
otected
Bitmap
downsampleWithSize
(
InputStream
is
,
BitmapFactory
.
Options
options
,
BitmapPool
pool
,
int
inWidth
,
int
inHeight
,
int
sampleSize
,
DecodeFormat
decodeFormat
)
{
pr
ivate
Bitmap
downsampleWithSize
(
ExceptionCatchingInputStream
is
,
BitmapFactory
.
Options
options
,
BitmapPool
pool
,
int
inWidth
,
int
inHeight
,
int
sampleSize
,
DecodeFormat
decodeFormat
)
{
// Prior to KitKat, the inBitmap size must exactly match the size of the bitmap we're decoding.
Bitmap
.
Config
config
=
getConfig
(
is
,
decodeFormat
);
options
.
inSampleSize
=
sampleSize
;
...
...
@@ -280,15 +282,14 @@ public abstract class Downsampler implements BitmapDecoder<InputStream> {
* android.graphics.BitmapFactory.Options)}.
* @return an array containing the dimensions of the image in the form {width, height}.
*/
public
int
[]
getDimensions
(
InputStream
is
,
BitmapFactory
.
Options
options
)
{
public
int
[]
getDimensions
(
ExceptionCatching
InputStream
is
,
BitmapFactory
.
Options
options
)
{
options
.
inJustDecodeBounds
=
true
;
decodeStream
(
is
,
options
);
options
.
inJustDecodeBounds
=
false
;
return
new
int
[]
{
options
.
outWidth
,
options
.
outHeight
};
}
private
static
Bitmap
decodeStream
(
InputStream
is
,
BitmapFactory
.
Options
options
)
{
private
static
Bitmap
decodeStream
(
ExceptionCatchingInputStream
is
,
BitmapFactory
.
Options
options
)
{
if
(
options
.
inJustDecodeBounds
)
{
// This is large, but jpeg headers are not size bounded so we need something large enough to minimize
// the possibility of not being able to fit enough of the header in the buffer to get the image size so
...
...
@@ -296,6 +297,11 @@ public abstract class Downsampler implements BitmapDecoder<InputStream> {
// original size each time we use up the buffer space without passing the mark so this is a maximum
// bound on the buffer size, not a default. Most of the time we won't go past our pre-allocated 16kb.
is
.
mark
(
MARK_POSITION
);
}
else
{
// Once we've read the image header, we no longer need to allow the buffer to expand in size. To avoid
// unnecessary allocations reading image data, we fix the mark limit so that it is no larger than our
// current buffer size here. See issue #225.
is
.
fixMarkLimit
();
}
final
Bitmap
result
=
BitmapFactory
.
decodeStream
(
is
,
null
,
options
);
...
...
library/src/main/java/com/bumptech/glide/load/resource/bitmap/RecyclableBufferedInputStream.java
浏览文件 @
2007440c
...
...
@@ -17,6 +17,8 @@ package com.bumptech.glide.load.resource.bitmap;
* limitations under the License.
*/
import
android.util.Log
;
import
java.io.FilterInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
...
...
@@ -36,32 +38,33 @@ import java.io.InputStream;
* </pre>
*/
public
class
RecyclableBufferedInputStream
extends
FilterInputStream
{
private
static
final
String
TAG
=
"BufferedIs"
;
/**
* The buffer containing the current bytes read from the target InputStream.
*/
pr
otected
volatile
byte
[]
buf
;
pr
ivate
volatile
byte
[]
buf
;
/**
* The total number of bytes inside the byte array {@code buf}.
*/
pr
otected
int
count
;
pr
ivate
int
count
;
/**
* The current limit, which when passed, invalidates the current mark.
*/
pr
otected
int
marklimit
;
pr
ivate
int
marklimit
;
/**
* The currently marked position. -1 indicates no mark has been set or the
* mark has been invalidated.
*/
pr
otected
int
markpos
=
-
1
;
pr
ivate
int
markpos
=
-
1
;
/**
* The current position within the byte array {@code buf}.
*/
pr
otected
int
pos
;
pr
ivate
int
pos
;
public
RecyclableBufferedInputStream
(
InputStream
in
,
byte
[]
buffer
)
{
super
(
in
);
...
...
@@ -94,6 +97,17 @@ public class RecyclableBufferedInputStream extends FilterInputStream {
throw
new
IOException
(
"BufferedInputStream is closed"
);
}
/**
* Reduces the mark limit to match the current buffer length to prevent the buffer from
* continuing to increase in size.
*
* <p>Subsequent calls to {@link #mark(int)} will be obeyed and may cause the buffer size
* to increase.
*/
public
synchronized
void
fixMarkLimit
()
{
marklimit
=
buf
.
length
;
}
/**
* Closes this stream. The source stream is closed and any resources
* associated with it are released.
...
...
@@ -134,6 +148,9 @@ public class RecyclableBufferedInputStream extends FilterInputStream {
if
(
newLength
>
marklimit
)
{
newLength
=
marklimit
;
}
if
(
Log
.
isLoggable
(
TAG
,
Log
.
DEBUG
))
{
Log
.
d
(
TAG
,
"allocate buffer of length: "
+
newLength
);
}
byte
[]
newbuf
=
new
byte
[
newLength
];
System
.
arraycopy
(
localBuf
,
0
,
newbuf
,
0
,
localBuf
.
length
);
// Reassign buf, which will invalidate any local references
...
...
library/src/main/java/com/bumptech/glide/util/ExceptionCatchingInputStream.java
浏览文件 @
2007440c
package
com.bumptech.glide.util
;
import
com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Queue
;
...
...
@@ -15,10 +17,10 @@ public class ExceptionCatchingInputStream extends InputStream {
private
static
final
Queue
<
ExceptionCatchingInputStream
>
QUEUE
=
Util
.
createQueue
(
0
);
private
InputStream
wrapped
;
private
RecyclableBuffered
InputStream
wrapped
;
private
IOException
exception
;
public
static
ExceptionCatchingInputStream
obtain
(
InputStream
toWrap
)
{
public
static
ExceptionCatchingInputStream
obtain
(
RecyclableBuffered
InputStream
toWrap
)
{
ExceptionCatchingInputStream
result
;
synchronized
(
QUEUE
)
{
result
=
QUEUE
.
poll
();
...
...
@@ -41,7 +43,7 @@ public class ExceptionCatchingInputStream extends InputStream {
// Do nothing.
}
void
setInputStream
(
InputStream
toWrap
)
{
void
setInputStream
(
RecyclableBuffered
InputStream
toWrap
)
{
wrapped
=
toWrap
;
}
...
...
@@ -118,6 +120,10 @@ public class ExceptionCatchingInputStream extends InputStream {
return
result
;
}
public
void
fixMarkLimit
()
{
wrapped
.
fixMarkLimit
();
}
public
IOException
getException
()
{
return
exception
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录