Skip to content

Commit

Permalink
improve: Improve BitmapRegionDecoderDecodeHelper. It no longer closes…
Browse files Browse the repository at this point in the history
… the input stream. It will now close the input stream when BitmapRegionDecoderDecodeHelper is destroyed. (#29)
  • Loading branch information
panpf committed Jul 23, 2024
1 parent 4b97ed5 commit 1bd98f2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ subsampling:
1.1.0-alpha02. [#31](https://github.com/panpf/zoomimage/issues/31)
* improve: GlideModeToImageSource and PicassoDataToImageSource are now priority for user
registration
* improve: Improve BitmapRegionDecoderDecodeHelper. It no longer closes the input stream. It will
now close the input stream when BitmapRegionDecoderDecodeHelper is
destroyed. [#29](https://github.com/panpf/zoomimage/issues/29)

## 1.1.0-alpha02

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ subsampling:
* fix: 修复 coil 系列的组件从 1.1.0-alpha02 版本开始无法子采样的
bug。 [#31](https://github.com/panpf/zoomimage/issues/31)
* improve: GlideModeToImageSource 和 PicassoDataToImageSource 现在是用户注册的优先
* improve: 改进 BitmapRegionDecoderDecodeHelper,不再关闭输入流,现在会在销毁
BitmapRegionDecoderDecodeHelper 时关闭输入流。 [#29](https://github.com/panpf/zoomimage/issues/29)

## 1.1.0-alpha02

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.github.panpf.zoomimage.util.IntRectCompat
import com.github.panpf.zoomimage.util.IntSizeCompat
import okio.buffer
import okio.use
import java.io.BufferedInputStream

/**
* Not thread safe
Expand All @@ -26,6 +27,7 @@ class BitmapRegionDecoderDecodeHelper(
) : DecodeHelper {

private var _decoder: BitmapRegionDecoder? = null
private var _inputStream: BufferedInputStream? = null

override fun decodeRegion(
key: String,
Expand All @@ -50,21 +52,21 @@ class BitmapRegionDecoderDecodeHelper(
if (decoder != null) {
return decoder
}
return imageSource.openSource().buffer().inputStream().buffered()
.use {
if (VERSION.SDK_INT >= VERSION_CODES.S) {
BitmapRegionDecoder.newInstance(it)!!
} else {
@Suppress("DEPRECATION")
BitmapRegionDecoder.newInstance(it, false)!!
}
}.apply {
this@BitmapRegionDecoderDecodeHelper._decoder = this
}
val inputStream = imageSource.openSource().buffer().inputStream().buffered()
val newDecoder = if (VERSION.SDK_INT >= VERSION_CODES.S) {
BitmapRegionDecoder.newInstance(inputStream)!!
} else {
@Suppress("DEPRECATION")
BitmapRegionDecoder.newInstance(inputStream, false)!!
}
this._decoder = newDecoder
this._inputStream = inputStream
return newDecoder
}

override fun close() {
_decoder?.recycle()
_inputStream?.close()
}

override fun copy(): DecodeHelper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import com.github.panpf.zoomimage.subsampling.ImageInfo
import com.github.panpf.zoomimage.subsampling.ImageSource
import com.github.panpf.zoomimage.subsampling.TileBitmap
import com.github.panpf.zoomimage.util.IntRectCompat
import okio.Closeable

interface DecodeHelper : Closeable {
interface DecodeHelper : AutoCloseable {

val imageInfo: ImageInfo

Expand Down

0 comments on commit 1bd98f2

Please sign in to comment.