无效的流头:从字节字符串转换对象时的EFBFBDEF

时间:2021-03-23 20:13:33

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.

我正在尝试将ArrayList对象转换为字节字符串,以便可以通过套接字发送它。当我运行这个代码时,它会正确地转换为一个字符串,但是当我试图将它转换回来时,我得到了一个异常“java.io”。流标题:EFBFBDEF”无效。我在这里看到的其他答案并没有真正的帮助,因为我正在使用匹配的ObjectOutputStream和ObjectInputStream。抱歉,如果有一个简单的修复,因为我是使用流对象的新手。

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();
        }
    }

2 个解决方案

#1


5  

String is not a container for binary data. You need to pass around the original byte array, or hex- or base64-encode it.

字符串不是二进制数据的容器。您需要传递原始字节数组,或者十六进制或base64编码。

Better still, serialize directly to the socket and get rid of this altogether.

更好的是,直接序列化到套接字,并完全删除它。

#2


0  

I figured it out. I had to use Base64 encoding. The conversion methods have to be changed to the following:

我想出来。我必须使用Base64编码。转换方法必须改为:

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 Base64.getEncoder().encodeToString(byteArray);
        }
    }

public static Object convertFromByteString(String byteString) throws IOException, ClassNotFoundException {
        final byte[] bytes = Base64.getDecoder().decode(byteString);
        try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = new ObjectInputStream(bis)) {
            return in.readObject();
        }
    }

#1


5  

String is not a container for binary data. You need to pass around the original byte array, or hex- or base64-encode it.

字符串不是二进制数据的容器。您需要传递原始字节数组,或者十六进制或base64编码。

Better still, serialize directly to the socket and get rid of this altogether.

更好的是,直接序列化到套接字,并完全删除它。

#2


0  

I figured it out. I had to use Base64 encoding. The conversion methods have to be changed to the following:

我想出来。我必须使用Base64编码。转换方法必须改为:

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 Base64.getEncoder().encodeToString(byteArray);
        }
    }

public static Object convertFromByteString(String byteString) throws IOException, ClassNotFoundException {
        final byte[] bytes = Base64.getDecoder().decode(byteString);
        try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = new ObjectInputStream(bis)) {
            return in.readObject();
        }
    }