简体   繁体   English

如何使用ObjectOutputStream和ObjectInputStream正确地(反)序列化为字节数组?

[英]How to (de)serialize correctly to a byte-array using a ObjectOutputStream & ObjectInputStream?

I am aware that an ObjectOutputStream/ObjectInputStream uses headers and this is not really a proper use-case. 我知道ObjectOutputStream / ObjectInputStream使用标头,这实际上不是一个合适的用例。 But anyway I need to wrap some data into it using the interfaces DataInput and DataOutput. 但是无论如何,我需要使用DataInput和DataOutput接口将一些数据包装到其中。

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        byte[] array = serialize("test");
        String deserialized = deserialize(array);

        System.out.println(deserialized);
    }

    private static byte[] serialize(String test) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);

            objectOutputStream.writeUTF(test);

            byteArrayOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return byteArrayOutputStream.toByteArray();
    }

    private static String deserialize(byte[] array) {
        String temp = null;

        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(array));

            temp = objectInputStream.readUTF();

            objectInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return temp;
    }
}

I don't really get how to get that working. 我真的不知道如何使它工作。 Am I right, that the problem are those headers currently? 我是对的,现在的问题是这些标题吗?

You should call objectOutputStream.flush(); 您应该调用objectOutputStream.flush(); before closing byteArrayOutputStream . 在关闭byteArrayOutputStream之前。

ObjectOutputStream have its internal buffer so you got only beginning of string in your byte array. ObjectOutputStream有其内部缓冲区,因此您只能在字节数组中获得字符串的开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ObjectOutputStream和ObjectInputStream - How to use ObjectOutputStream and ObjectInputStream 如何使用ObjectOutputStream和ObjectInputStream在Java中处理夏时制? - How to handle daylight saving time in Java using ObjectOutputStream and ObjectInputStream? ObjectInputStream和ObjectOutputStream - ObjectInputStream and ObjectOutputStream 如何使用 ObjectOutputStream 在字节流中附加对象 - How to appends objects in byte stream using ObjectOutputStream 在客户端和服务器进程之间使用ObjectInputStream和ObjectOutputStream - Using ObjectInputStream and ObjectOutputStream between client and server process GsonFromJson:如何使用数组结构进行序列化和反序列化 - GsonFromJson: how to serialize and de serialize with array structures 从ObjectInputStream中读取不同的byte [],而不是写入ObjectOutputStream - Read different byte[] from ObjectInputStream than written to ObjectOutputStream 使用java套接字通过ObjectOutputStream和ObjectInputStream将数组发送到android时为null指针 - null pointer when using java socket to send array to android with ObjectOutputStream and ObjectInputStream 程序在使用ObjectInputStream,ObjectOutputStream时无法正确加载数据 - Program does not load data correctly while working with ObjectInputStream,ObjectOutputStream 在Java中将二进制字符串转换为字节数组 - Convert binary string to byte-Array in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM