java.io.StreamCorruptedException:无效的流标头:48656C6C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28298366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:21:23  来源:igfitidea点击:

java.io.StreamCorruptedException: invalid stream header: 48656C6C

javabytearraynettyobjectinputstream

提问by Kanmani

I am using netty client server for communication . The message is received successfully as byte array . When I convert byte array to ObjectInputStream I get the exception

我正在使用 netty 客户端服务器进行通信。消息作为字节数组成功接收。当我将字节数组转换为 ObjectInputStream 时,出现异常

java.io.StreamCorruptedException: invalid stream header: 48656C6C
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at nettyClientServer2.PongHandler.messageReceived(PongHandler.java:99)
at org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:88)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
at org.jboss.netty.handler.execution.ChannelUpstreamEventRunnable.doRun(ChannelUpstreamEventRunnable.java:43)
at org.jboss.netty.handler.execution.ChannelEventRunnable.run(ChannelEventRunnable.java:67)
at org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor$ChildExecutor.run(OrderedMemoryAwareThreadPoolExecutor.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

This is how I convert..

这就是我如何转换..

byte[] ppBytes=pptmp.status;
ObjectInputStream input = null;
input = new ObjectInputStream(new ByteArrayInputStream(ppBytes));

回答by SubOptimal

ppBytesmust hold the bytes of an serialized object. See below a short example.

ppBytes必须保存序列化对象的字节。请参阅下面的简短示例。

byte[] buffer;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos)) {
    oos.writeObject("Hello World");
    buffer = bos.toByteArray();
    for (int i : buffer) {
        System.out.printf("%02X ", i & 0xFF);
    }
    System.out.println("");
}

try (ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
        ObjectInputStream ois = new ObjectInputStream(bis)) {
    String input = (String) ois.readObject();
    System.out.println("input: " + input);
}

output

输出

//                   H  e  l  l  o     W  o  r  l  d
AC ED 00 05 74 00 0B 48 65 6C 6C 6F 20 57 6F 72 6C 64 
input: Hello World

In the below example the buffer contains the byte representation of the String Hello World. To read those bytes with an ObjectInputStreamwill fail with java.io.StreamCorruptedException: invalid stream header: 48656C6C. As an serialized Stringobject is expected.

在下面的示例中,缓冲区包含 String 的字节表示Hello World。使用 读取这些字节ObjectInputStream将失败java.io.StreamCorruptedException: invalid stream header: 48656C6C。作为String预期的序列化对象。

byte[] buffer;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
    bos.write("Hello World".getBytes(StandardCharsets.ISO_8859_1));
    buffer = bos.toByteArray();
    for (int i : buffer) {
        System.out.printf("%02X ", i & 0xFF);
    }
    System.out.println("");
}

try (ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
        ObjectInputStream ois = new ObjectInputStream(bis)) {
    String input = (String) ois.readObject();
    System.out.println("input: " + input);
}

output

输出

// H  e  l  l  o     W  o  r  l  d
   48 65 6C 6C 6F 20 57 6F 72 6C 64 
   Exception in thread "main" java.io.StreamCorruptedException: invalid \
      stream header: 48656C6C

回答by laune

You can do this by converting a String to a byte[] and back again, to String:

您可以通过将 String 转换为 byte[] 并再次转换为 String 来实现此目的:

String hello = "Hello world";
byte[] bytes = hello.getBytes( "iso-8859-1" );  // or utf-8
// send
String world = new String( bytes, "iso-8859-1" ); // or utf-8
System.out.println( hello );
System.out.println( world );

It is more reliable to read and write (String) objects, which bypasses the encoding/decoding gamble:

读取和写入(字符串)对象更可靠,绕过编码/解码赌博:

String hello = "Hello world";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeUTF( hello );
oos.flush();
byte[] bytes = baos.toByteArray();
// send
ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
ObjectInputStream ois = new ObjectInputStream( bais );
String world = ois.readUTF();