Skip to content

Commit

Permalink
fix: Use method compatible with Android 12 and lower
Browse files Browse the repository at this point in the history
  • Loading branch information
LisoUseInAIKyrios committed Oct 14, 2024
1 parent 4c1c34a commit 7bb1cab
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import lanchon.multidexlib2.BasicDexFileNamer
import lanchon.multidexlib2.DexIO
import lanchon.multidexlib2.MultiDexIO
import lanchon.multidexlib2.RawDexIO
import java.io.ByteArrayOutputStream
import java.io.Closeable
import java.io.FileFilter
import java.io.InputStream
import java.util.*
import java.util.logging.Logger

Expand Down Expand Up @@ -58,6 +60,26 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
*/
internal val lookupMaps by lazy { LookupMaps(classes) }

/**
* Because InputStream.readAllBytes() is not available with Android until 13.0,
* roll our own implementation until this project uses Kotlin multiplatform.
*/
private fun InputStream.readAllBytesBackwardsCompatible(): ByteArray {
val buffer = ByteArrayOutputStream()
val data = ByteArray(1024)

while (true) {
var length = this.read(data)
if (length >= 0) {
buffer.write(data, 0, length)
} else {
break
}
}

return buffer.toByteArray()
}

/**
* Merge the extensions for this set of patches.
*/
Expand All @@ -70,7 +92,7 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
forEachRecursively { patch ->
if (patch is BytecodePatch && patch.extension != null) {

val extension = patch.extension.readAllBytes()
val extension = patch.extension.readAllBytesBackwardsCompatible()

RawDexIO.readRawDexFile(extension, 0, null).classes.forEach { classDef ->
val existingClass = classesByType[classDef.type] ?: run {
Expand Down

0 comments on commit 7bb1cab

Please sign in to comment.