简体   繁体   English

Java 的 ObjectOutputStream 的 writeObject() 方法如何处理数组?

[英]How does Java's ObjectOutputStream's writeObject() method work for array?

File file = new File(path);
if (file.exists())
{
    int count;
    byte[] buffer = new byte[8192];

    BufferedInputStream fileReader = new BufferedInputStream(new FileInputStream(file));
    while ((count = fileReader.read(buffer)) >= 0)
    {
        fileOut.writeObject(buffer);
        fileOut.writeObject(count);
    }
    fileOut.writeObject("EOF");
    fileReader.close();
}

Above is the server-side codes I used to transfer files to socket originally.以上是我原来用来传输文件到socket的服务器端代码。 I first send a byte array called "buffer", then a int variable called "count" to tell client how many bytes of "buffer" array are needed to be written into file, and when file finished transferring, I send a string contained "EOF" to let client know that file transferring has done to prevent further reading on client-side.我首先发送一个名为“buffer”的字节数组,然后发送一个名为“count”的int变量来告诉客户端需要将多少字节的“buffer”数组写入文件,当文件传输完成时,我发送一个包含“的字符串” EOF”让客户端知道文件传输已经完成,以防止客户端进一步读取。 However, when I ran this program, weird things appeared, the elements of "buffer" array that server is sending did not change after first loop, which means after first loop, server kept sending the "buffer" array that contains the same elements as the one in the first loop, but I did check the values of array before fileOut.writeObject(buffer) is called by print them to the screen, and elements' values actually changed.然而,当我运行这个程序时,奇怪的事情出现了,服务器发送的“缓冲区”数组的元素在第一次循环后没有改变,这意味着在第一次循环之后,服务器一直发送包含与以下相同元素的“缓冲区”数组第一个循环中的那个,但我确实在fileOut.writeObject(buffer)被调用之前检查了数组的值,方法是将它们打印到屏幕上,并且元素的值实际上发生了变化。 Then this problem was solved by adding another line of code buffer = new byte[8192] after fileOut.writeObject(count) , which the codes become:然后通过在fileOut.writeObject(count)之后添加另一行代码buffer = new byte[8192]解决了这个问题,代码变为:

File file = new File(path);
if (file.exists())
{
    int count;
    byte[] buffer = new byte[8192];

    BufferedInputStream fileReader = new BufferedInputStream(new FileInputStream(file));
    while ((count = fileReader.read(buffer)) >= 0)
    {
        fileOut.writeObject(buffer);
        fileOut.writeObject(count);
        buffer = new byte[8192];
    }
    fileOut.writeObject("EOF");
    fileReader.close();
}

Therefore I am wondering why I need to initialize "buffer" array each time before bytes of file are read into "buffer" array, but the elements of "buffer" array were changing without initializing array every time at the end of loop.因此,我想知道为什么每次都需要在将文件字节读入“缓冲区”数组之前初始化“缓冲区”数组,但是每次循环结束时,“缓冲区”数组的元素都在更改而不初始化数组。 Does it have something to do with the mechanics of writeObject() method of ObjectOutputStream?它与 ObjectOutputStream 的 writeObject() 方法的机制有关吗? I am urged to know, thanks for any help!我急于知道,谢谢您的帮助!

PS: fileOut is a instance variable that is initialized by the following code: PS:fileOut 是一个实例变量,由以下代码初始化:

fileOut = new ObjectOutputStream(socket.getOutputStream());

It's not an issue with arrays specifically.这不是数组的问题。 Java serialization numbers objects by ID and then only transmits each distinct object once (this prevents problems with cyclic graphs). Java 序列化按 ID 为对象编号,然后只传输每个不同的对象一次(这可以防止循环图出现问题)。 Each time you try to send the array again, the stream just repeats the ID for the array object.每次尝试再次发送数组时,流只会重复数组对象的 ID。 On the other end, if you compare the references, you'll find they're == .另一方面,如果您比较引用,您会发现它们是== (You should also be able to tell that writing an extra 8k only increases your output size by a few bytes.) (您还应该知道写入额外的 8k 只会使您的输出大小增加几个字节。)

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

相关问题 ObjectOutputStream的writeObject方法使用什么字符编码? - What character encoding does ObjectOutputStream 's writeObject method use? 通过ObjectOutputStream的writeObject方法进行Java ArrayList序列化将不起作用 - Java ArrayList serialization through ObjectOutputStream's writeObject method won't work 反序列化使用ObjectOutputStream的writeObject方法序列化的对象 - Deserializing an Object that has been serialized using ObjectOutputStream's writeObject method java.io.NotSerializableException :: ObjectOutputStream:writeObject方法 - java.io.NotSerializableException:: ObjectOutputStream: writeObject method Java ObjectOutputStream的方法flush() - Java ObjectOutputStream's Method flush() ObjectOutputStream方法writeObject挂在android上 - ObjectOutputStream method writeObject hangs on android ObjectOutputStream的对象如何调用Serializable对象的私有writeObject方法 - How private writeObject method of Serializable object called by object of ObjectOutputStream 如何模拟objectOutputStream.writeObject()? - How to mock objectOutputStream.writeObject()? 数组的相等方法是如何工作的? - How does an array's equal method work? ObjectOutputStream .writeObject - ObjectOutputStream .writeObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM