scala - 线程“main”中的异常 java.lang.IndexOutOfBoundsException Scala gZip 和解码 base64 代码
问题描述
我正在尝试从 gZip 解压缩和解码(base64)字符串。我已经为此编写了一个类和一个测试代码。我正进入(状态
Exception in thread "main" java.lang.IndexOutOfBoundsException
在线的
while ((readByte = gzipIS.read(gzipByteBuffer)) != -1) byteOS.write(
基本代码:
import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStreamReader
import java.io.UnsupportedEncodingException
import java.nio.charset.StandardCharsets
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
import org.apache.commons.codec.binary.Base64
import org.apache.commons.io.IOUtils
class ZipUtilities {
def unzipCCBRsponse(inputB64: String): String = {
val bDecodeBase64: Array[Byte] = Base64.decodeBase64(inputB64)
var zipInputStream: GZIPInputStream = null
try {
zipInputStream = new GZIPInputStream(
new ByteArrayInputStream(bDecodeBase64, 4, bDecodeBase64.length - 4))
val inputStreamReader: InputStreamReader =
new InputStreamReader(zipInputStream, StandardCharsets.UTF_8)
val bufferedReader: BufferedReader = new BufferedReader(
inputStreamReader)
val output: StringBuilder = new StringBuilder()
var line: String = null
while ((line = bufferedReader.readLine()) != null) output.append(line)
println("Output String Length: " + output.length)
bufferedReader.close()
output.toString
} catch {
case e: IOException => e.printStackTrace()
}
null
}
def decodeBase64(b64EncodedString: String): String = {
var bDecodeBase64: Array[Byte] = null
try {
bDecodeBase64 =
Base64.decodeBase64(b64EncodedString.getBytes("ISO-8859-1"))
new String(bDecodeBase64)
} catch {
case e: UnsupportedEncodingException => e.printStackTrace()
}
null
}
def decodeFromGzip(input: String): String = {
var output: String = null
if (input != null && !input.isEmpty) {
try {
var readByte: Int = 0
val gzipByteBuffer: Array[Byte] = Array.ofDim[Byte](2048)
val gzipIS: GZIPInputStream = new GZIPInputStream(
new ByteArrayInputStream(Base64.decodeBase64(input)))
val byteOS: ByteArrayOutputStream = new ByteArrayOutputStream()
while ((readByte = gzipIS.read(gzipByteBuffer)) != -1) byteOS.write(
gzipByteBuffer,
0,
readByte)
byteOS.close()
output = new String(byteOS.toByteArray())
} catch {
case e: IOException => e.printStackTrace()
}
}
output
}
}
测试代码是:
object ZipTest {
def main(args: Array[String]): Unit = {
val b64: String =
"H4sIAAAAAAAAAO2dW1PbOBTH3/spNHkvgXYvLVPoCFlJtNiSR5IT8sRkUxeYJQmThMJ++5Xt3LjNNsdwVprNC8N4fPz/2dblXGTly9f70TX5kU9nV5PxUeNgb79B8vFw8u1qfHHUuJ1"
val util: ZipUtilities = new ZipUtilities()
println(util.decodeFromGzip(b64).replaceAll("\n", ""))
}
}
解决方案
在 Scala 中,赋值 ( readByte = gzipIS.read(gzipByteBuffer)
) 返回Unit
,并且由于类型的值Unit
永远不会等于-1
(或任何Int
值),因此您的while
循环实际上是无限的。
编译器应该已经提醒您:
警告:使用 `!=' 比较 Unit 和 Int 类型的值将始终产生 true
注意:您很少在惯用的 Scala 代码中看到null
或。var
它几乎从来不需要。
推荐阅读
- r - H汇总数据框中在R中具有相同属性的行
- python - 如何获取唯一 ID 并将另一列的每一行转换为 R 和 Python 中的另一列
- global - 状态和全局变量
- flutter - 如何在 Flutter 中创建这样的元素?
- python - /basketball/ 的 NoReverseMatch 未找到带有参数 '('',)' 的“详细信息”。尝试了 1 种模式:
- debian - 如何在 debian 10 上修复此错误,我不确定问题是什么
- javascript - 复选框显示/隐藏未显示在角度页面上的更改
- reactjs - 如何在 WebView (react-native-webview) 中注入 React 组件?
- android - LiveData 不更新
- python - 如何将 Python 类实例方法调用一次限制为一个?