@@ -20,6 +20,47 @@ private const val bufferSize = 8192
20
20
21
21
private const val header = " z|zlib base64|"
22
22
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
+
23
64
internal class TextCompressionModule (reactContext : ReactApplicationContext ? ) :
24
65
ReactContextBaseJavaModule (reactContext) {
25
66
override fun getName (): String = " TextCompressionModule"
@@ -29,26 +70,7 @@ internal class TextCompressionModule(reactContext: ReactApplicationContext?) :
29
70
@ReactMethod
30
71
fun compress (input : String , promise : Promise ) {
31
72
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))
52
74
} catch (e: UnsupportedEncodingException ) {
53
75
promise.reject(" UNSUPPORTED_ENCODING_EXCEPTION" , e)
54
76
} catch (e: IOException ) {
@@ -59,21 +81,7 @@ internal class TextCompressionModule(reactContext: ReactApplicationContext?) :
59
81
@ReactMethod
60
82
fun decompress (input : String , promise : Promise ) {
61
83
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))
77
85
} catch (e: UnsupportedEncodingException ) {
78
86
promise.reject(" UNSUPPORTED_ENCODING_EXCEPTION" , e)
79
87
} catch (e: IOException ) {
@@ -82,4 +90,4 @@ internal class TextCompressionModule(reactContext: ReactApplicationContext?) :
82
90
promise.reject(" DATA_FORMAT_EXCEPTION" , e)
83
91
}
84
92
}
85
- }
93
+ }
0 commit comments