Skip to content

Commit

Permalink
Remove force unwrap (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
4brunu authored Jan 18, 2025
1 parent ea785ac commit bce95cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
14 changes: 5 additions & 9 deletions pdfViewer/src/main/java/com/rajat/pdfviewer/PdfRendererCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
)

private val openPages = ConcurrentHashMap<Int, PdfRenderer.Page>()
private var pdfRenderer: PdfRenderer? = null
private var pdfRenderer: PdfRenderer = PdfRenderer(fileDescriptor).also { isRendererOpen = true }
private val cacheManager = CacheManager(context)

companion object {
Expand Down Expand Up @@ -69,7 +69,6 @@


init {
pdfRenderer = PdfRenderer(fileDescriptor).also { isRendererOpen = true }
cacheManager.initCache()
}

Expand All @@ -96,9 +95,9 @@
cacheManager.pageExistsInCache(pageNo)

fun prefetchPages(currentPage: Int, width: Int, height: Int) {
val dynamicPrefetchCount = calculateDynamicPrefetchCount(context, pdfRenderer!!)
val dynamicPrefetchCount = calculateDynamicPrefetchCount(context, pdfRenderer)
(currentPage - dynamicPrefetchCount..currentPage + dynamicPrefetchCount)
.filter { it in 0 until pdfRenderer!!.pageCount && !pageExistInCache(it) }
.filter { it in 0 until pdfRenderer.pageCount && !pageExistInCache(it) }
.forEach { pageNo ->
CoroutineScope(Dispatchers.IO).launch {
val bitmap = CommonUtils.Companion.BitmapPool.getBitmap(width, height)
Expand All @@ -109,14 +108,11 @@
}
}
}
private fun openPdfFile(fileDescriptor: ParcelFileDescriptor) {
pdfRenderer = PdfRenderer(fileDescriptor)
}

fun getPageCount(): Int {
synchronized(this) {
if (!isRendererOpen) return 0
return pdfRenderer?.pageCount ?: 0
return pdfRenderer.pageCount
}
}

Expand Down Expand Up @@ -183,7 +179,7 @@
synchronized(this) {
if (!isRendererOpen) return null
closeAllOpenPages()
return pdfRenderer?.openPage(pageNo)?.also { page ->
return pdfRenderer.openPage(pageNo)?.also { page ->
openPages[pageNo] = page
}
}
Expand Down
11 changes: 5 additions & 6 deletions pdfViewer/src/main/java/com/rajat/pdfviewer/util/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@ object FileUtils {
}

@Throws(IOException::class)
fun copy(inputStream: InputStream?, output: File?) {
var outputStream: OutputStream? = null
fun copy(inputStream: InputStream, output: File?) {
val outputStream = FileOutputStream(output)
try {
outputStream = FileOutputStream(output)
var read = 0
val bytes = ByteArray(1024)
while (inputStream!!.read(bytes).also { read = it } != -1) {
while (inputStream.read(bytes).also { read = it } != -1) {
outputStream.write(bytes, 0, read)
}
} finally {
try {
inputStream?.close()
inputStream.close()
} finally {
outputStream?.close()
outputStream.close()
}
}
}
Expand Down

0 comments on commit bce95cb

Please sign in to comment.