“线程中的异常”;“主要”;java.lang.IndexOutOfBoundsException Scala gZip和解码base64代码
我正在尝试从gZip解压和解码(base64)一个字符串。 我已经为同一个类编写了一个类和一个测试代码。 我越来越“线程中的异常”;“主要”;java.lang.IndexOutOfBoundsException Scala gZip和解码base64代码,scala,base64,gzip,Scala,Base64,Gzip,我正在尝试从gZip解压和解码(base64)一个字符串。 我已经为同一个类编写了一个类和一个测试代码。 我越来越 Exception in thread "main" java.lang.IndexOutOfBoundsException 在线 while ((readByte = gzipIS.read(gzipByteBuffer)) != -1) byteOS.write( 基本代码: import java.io.BufferedReader import java.io.Byt
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类型的值永远不会屈服于现实
注意:在惯用的Scala代码中很少看到
null
或var
。几乎从来都不需要它。我做了一些更改,将-1替换为null。仍然抛出相同的异常。那么我需要在代码段中做哪些更改而不是-1?所有赋值都返回相同的内容:Unit
(notnull
,notInt
,没有任何有用的内容)。所以你不能测试作业。无需重构整个代码库,只需在每次read()
之后测试readByte
的值即可。但是请注意,您目前正在尝试使用Scala语言编写C代码。结果是Scala非常差。