我正在尝试使用ObjectOutputStream
而不是DeflaterOutputStream
来将泄露的数据写入基础流。但是当我尝试使用InputStream
对应的数据读取数据时,会抛出异常。值得注意的是,将Deflate{Output,Input}Stream
替换为GZip{Output,Input}Stream
,它会按预期工作。显示此行为的示例代码如下所示:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(ObjectOutputStream oos = new ObjectOutputStream(new DeflaterOutputStream(baos))) {
oos.writeObject("test");
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try(ObjectInputStream oos = new ObjectInputStream(new DeflaterInputStream(bais))) {
System.out.println(oos.readObject());
}
抛出以下异常:
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 789CAB98
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:857)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:349)
at Main.main(Main.java:23)
有谁知道为什么会出现这种情况?
答案 0 :(得分:1)
我已经弄清楚这是一个愚蠢的错误。但是回答我自己的问题,以便未来的人们不再为此而堕落:
05-02 16:16:37.485 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: JNI
CallVoidMethodV called with pending exception 'java.lang.ClassCastException'
thrown in unknown throw location
05-02 16:16:37.486 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] in call to CallVoidMethodV
05-02 16:16:37.486 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] from void
com.badlogic.gdx.physics.box2d.World.jniStep(long, float, int, int)
05-02 16:16:37.486 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] "GLThread 239" prio=5 tid=13 Runnable
05-02 16:16:37.486 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] | group="main" sCount=0 dsCount=0
obj=0x12c22640 self=0xb4033800
05-02 16:16:37.486 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] | sysTid=8656 nice=0 cgrp=default sched=0/0
handle=0xb4054a00
05-02 16:16:37.486 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] | state=R schedstat=( 0 0 0 ) utm=1460
stm=1139 core=1 HZ=100
05-02 16:16:37.486 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] | stack=0xa4da6000-0xa4da8000
stackSize=1036KB
05-02 16:16:37.486 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] | held mutexes= "mutator lock"(shared held)
05-02 16:16:37.486 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] native: #00 pc 00005d03
/system/lib/libbacktrace_libc++.so (UnwindCurrent::Unwind(unsigned int,
ucontext*)+83)
at com.badlogic.gdx.physics.box2d.World.jniStep(Native method)
05-02 16:16:37.487 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] at
com.badlogic.gdx.physics.box2d.World.step(World.java:689)
05-02 16:16:37.487 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] at
com.ana7tasys.kubo.Screens.PlayScreen.update(PlayScreen.java:188)
05-02 16:16:37.487 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] at
com.ana7tasys.kubo.Screens.PlayScreen.render(PlayScreen.java:232)
05-02 16:16:37.487 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] at com.badlogic.gdx.Game.render(Game.java:46)
05-02 16:16:37.487 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] at
com.ana7tasys.kubo.KuboGame.render(KuboGame.java:50)
05-02 16:16:37.487 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:459)
05-02 16:16:37.487 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] at
android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1522)
05-02 16:16:37.487 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65] at
android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)
05-02 16:16:37.487 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/check_jni.cc:65]
05-02 16:16:37.551 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/runtime.cc:289] Runtime aborting...
05-02 16:16:37.551 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/runtime.cc:289] Aborting thread:
05-02 16:16:37.551 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/runtime.cc:289] "GLThread 239" prio=5 tid=13 Native
05-02 16:16:37.551 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/runtime.cc:289] | group="" sCount=0 dsCount=0 obj=0x12c22640
self=0xb4033800
05-02 16:16:37.551 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/runtime.cc:289] | sysTid=8656 nice=0 cgrp=default sched=0/0
handle=0xb4054a00
05-02 16:16:37.551 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/runtime.cc:289] | state=R schedstat=( 0 0 0 ) utm=1460
stm=1139
core=1 HZ=100
05-02 16:16:37.551 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/runtime.cc:289] | stack=0xa4da6000-0xa4da8000 stackSize=1036KB
05-02 16:16:37.551 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/runtime.cc:289] | held mutexes= "abort lock" "mutator lock"
(shared held)
05-02 16:16:37.551 8627-8656/com.ana7tasys.kubo A/art:
art/runtime/runtime.cc:289] native: #00 pc 00005d03
/system/lib/libbacktrace_libc++.so (UnwindCurrent::Unwind(unsigned int,
ucontext*)+83)
的反向类是DeflaterOutputStream
,而不是InflaterInputStream
。所以代码应该如下所示:
DeflaterInputStream