Skip to content

Commit c13b15a

Browse files
gnpricechrisbobbe
authored andcommitted
android [nfc]: Pull guts off React module in TextCompression
This will let us straightforwardly re-use this logic from within the Android-side code.
1 parent 2ac3171 commit c13b15a

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

android/app/src/main/java/com/zulipmobile/TextCompression.kt

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,47 @@ private const val bufferSize = 8192
2020

2121
private const val header = "z|zlib base64|"
2222

23+
internal fun compress(input: String): String {
24+
val outputStream = ByteArrayOutputStream()
25+
val deflater = Deflater()
26+
deflater.setInput(input.toByteArray(charset("UTF-8")))
27+
deflater.finish()
28+
val buffer = ByteArray(bufferSize)
29+
while (!deflater.finished()) {
30+
val byteCount = deflater.deflate(buffer)
31+
outputStream.write(buffer, 0, byteCount)
32+
}
33+
deflater.end()
34+
outputStream.close()
35+
// The RN bridge currently doesn't support sending byte strings, so we
36+
// have to encode the compressed output as a `String`. To avoid any
37+
// trouble, we use base64 to keep things inside ASCII.
38+
//
39+
// Ultimately our ASCII data seems to end up going to SQLite with size
40+
// no more than about 1 byte/char (presumably the string gets encoded
41+
// as UTF-8 and it's exactly 1 byte/char), so this is pretty OK.
42+
return header + Base64.encodeToString(outputStream.toByteArray(),
43+
Base64.DEFAULT)
44+
}
45+
46+
internal fun decompress(input: String): String {
47+
val inflater = Inflater()
48+
val inputBytes = input.toByteArray(charset("ISO-8859-1"))
49+
inflater.setInput(Base64.decode(inputBytes,
50+
header.length,
51+
inputBytes.size - header.length,
52+
Base64.DEFAULT))
53+
val outputStream = ByteArrayOutputStream()
54+
val buffer = ByteArray(bufferSize)
55+
while (inflater.remaining != 0) {
56+
val byteCount = inflater.inflate(buffer)
57+
outputStream.write(buffer, 0, byteCount)
58+
}
59+
inflater.end()
60+
outputStream.close()
61+
return outputStream.toString("UTF-8")
62+
}
63+
2364
internal class TextCompressionModule(reactContext: ReactApplicationContext?) :
2465
ReactContextBaseJavaModule(reactContext) {
2566
override fun getName(): String = "TextCompressionModule"
@@ -29,26 +70,7 @@ internal class TextCompressionModule(reactContext: ReactApplicationContext?) :
2970
@ReactMethod
3071
fun compress(input: String, promise: Promise) {
3172
try {
32-
val outputStream = ByteArrayOutputStream()
33-
val deflater = Deflater()
34-
deflater.setInput(input.toByteArray(charset("UTF-8")))
35-
deflater.finish()
36-
val buffer = ByteArray(bufferSize)
37-
while (!deflater.finished()) {
38-
val byteCount = deflater.deflate(buffer)
39-
outputStream.write(buffer, 0, byteCount)
40-
}
41-
deflater.end()
42-
outputStream.close()
43-
// The RN bridge currently doesn't support sending byte strings, so we
44-
// have to encode the compressed output as a `String`. To avoid any
45-
// trouble, we use base64 to keep things inside ASCII.
46-
//
47-
// Ultimately our ASCII data seems to end up going to SQLite with size
48-
// no more than about 1 byte/char (presumably the string gets encoded
49-
// as UTF-8 and it's exactly 1 byte/char), so this is pretty OK.
50-
promise.resolve(header + Base64.encodeToString(outputStream.toByteArray(),
51-
Base64.DEFAULT))
73+
promise.resolve(compress(input))
5274
} catch (e: UnsupportedEncodingException) {
5375
promise.reject("UNSUPPORTED_ENCODING_EXCEPTION", e)
5476
} catch (e: IOException) {
@@ -59,21 +81,7 @@ internal class TextCompressionModule(reactContext: ReactApplicationContext?) :
5981
@ReactMethod
6082
fun decompress(input: String, promise: Promise) {
6183
try {
62-
val inflater = Inflater()
63-
val inputBytes = input.toByteArray(charset("ISO-8859-1"))
64-
inflater.setInput(Base64.decode(inputBytes,
65-
header.length,
66-
inputBytes.size - header.length,
67-
Base64.DEFAULT))
68-
val outputStream = ByteArrayOutputStream()
69-
val buffer = ByteArray(bufferSize)
70-
while (inflater.remaining != 0) {
71-
val byteCount = inflater.inflate(buffer)
72-
outputStream.write(buffer, 0, byteCount)
73-
}
74-
inflater.end()
75-
outputStream.close()
76-
promise.resolve(outputStream.toString("UTF-8"))
84+
promise.resolve(decompress(input))
7785
} catch (e: UnsupportedEncodingException) {
7886
promise.reject("UNSUPPORTED_ENCODING_EXCEPTION", e)
7987
} catch (e: IOException) {
@@ -82,4 +90,4 @@ internal class TextCompressionModule(reactContext: ReactApplicationContext?) :
8290
promise.reject("DATA_FORMAT_EXCEPTION", e)
8391
}
8492
}
85-
}
93+
}

0 commit comments

Comments
 (0)