无效的流头:从字节字符串转换对象时,EFBFBDEF [英] invalid stream header: EFBFBDEF when converting object from byte string
问题描述
我正在尝试将ArrayList对象转换为字节字符串,以便可以通过套接字发送它。当我运行此代码时,它会正确转换为字符串,但是当我尝试将其转换回时,会出现异常 java.io.StreamCorruptedException:无效的流头:EFBFBDEF。我在这里查看的其他答案并没有真正帮助,因为我正在使用匹配的ObjectOutputStream和ObjectInputStream。抱歉,如果有一个简单的修复方法,因为我是流对象的新手。
try {
ArrayList< String> ; text = new ArrayList<>();
text.add( Hello World!);
字符串byteString = Utils.StringUtils.convertToByteString(text);
ArrayList< String> convertSet =(ArrayList< String>)Utils.StringUtils.convertFromByteString(byteString);
VCS.getServiceManager()。addConsoleLog(convertedSet.get(0));
} catch(IOException | ClassNotFoundException e){
e.printStackTrace();
}
公共静态字符串convertToByteString(Object object)引发IOException {
try(ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)){
out.writeObject(object);
最后的byte [] byteArray = bos.toByteArray();
返回新的String(byteArray);
}
}
公共静态对象convertFromByteString(String byteString)抛出IOException,ClassNotFoundException {
最后的byte [] bytes = byteString.getBytes();
try(ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = new ObjectInputStream(bis)){
return in.readObject();
}
}
字符串不是二进制数据的容器。您需要传递原始的字节数组,或者对其进行十六进制或base64编码。
更好的是,直接序列化到套接字并完全摆脱掉。 / p>
I am trying to convert a ArrayList object to a byte string so it can be sent via sockets. When I run this code it converts to a string properly but when I try to convert it back I get the exception "java.io.StreamCorruptedException: invalid stream header: EFBFBDEF". Other answers I looked at on here didn't really help as I am using the matching ObjectOutputStream and ObjectInputStream. Sorry if there is a simple fix as I am new to working with stream objects.
try {
ArrayList<String> text = new ArrayList<>();
text.add("Hello World!");
String byteString = Utils.StringUtils.convertToByteString(text);
ArrayList<String> convertedSet = (ArrayList<String>) Utils.StringUtils.convertFromByteString(byteString);
VCS.getServiceManager().addConsoleLog(convertedSet.get(0));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
public static String convertToByteString(Object object) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(object);
final byte[] byteArray = bos.toByteArray();
return new String(byteArray);
}
}
public static Object convertFromByteString(String byteString) throws IOException, ClassNotFoundException {
final byte[] bytes = byteString.getBytes();
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = new ObjectInputStream(bis)) {
return in.readObject();
}
}
String is not a container for binary data. You need to pass around the original byte array, or hex- or base64-encode it.
Better still, serialize directly to the socket and get rid of this altogether.
这篇关于无效的流头:从字节字符串转换对象时,EFBFBDEF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!