java.io.StreamCorruptedException: 无效流 header: 48656C6C
java.io.StreamCorruptedException: invalid stream header: 48656C6C
我正在使用 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)
我就是这样转换的..
byte[] ppBytes=pptmp.status;
ObjectInputStream input = null;
input = new ObjectInputStream(new ByteArrayInputStream(ppBytes));
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);
}
输出
// 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
在下面的示例中,缓冲区包含字符串 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);
}
输出
// 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
您可以通过将 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 );
读写(String)对象更靠谱,绕过encoding/decoding赌博:
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();
我正在使用 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)
我就是这样转换的..
byte[] ppBytes=pptmp.status;
ObjectInputStream input = null;
input = new ObjectInputStream(new ByteArrayInputStream(ppBytes));
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);
}
输出
// 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
在下面的示例中,缓冲区包含字符串 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);
}
输出
// 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
您可以通过将 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 );
读写(String)对象更靠谱,绕过encoding/decoding赌博:
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();