简体   繁体   English

如何读取DataInputStream两次或两次以上?

[英]How to read a DataInputStream twice or more than twice?

I have a Socket connection to an application that I hosted elsewhere. 我有一个Socket连接到我在其他地方托管的应用程序。 Once I connected I made a OutputStream and DataInputStream . 连接后,我创建了一个OutputStreamDataInputStream

Once the connection has been made, I use the OutputStream to send out a handshake packet to the application. 建立连接后,我使用OutputStream向应用程序发送握手数据包。 Once this handshake has been approved, it returns a packet through the DataInputStream (1). 一旦此握手获得批准,它将通过DataInputStream (1)返回一个数据包。

This packet is processed and is returned to the application with the OutputStream . 处理此数据包并使用OutputStream将其返回给应用程序。

If this returned data is valid, I get another packet from the DataInputStream (2). 如果返回的数据有效,我从DataInputStream (2)获取另一个数据包。 However, I have not been able to read this packet through the DataInputStream . 但是,我无法通过DataInputStream读取此数据包。

I have tried to use DataInputStream.markSupported() and DataInputStream.mark() but this gave me nothing (except for an empty Exception message). 我曾尝试使用DataInputStream.markSupported()DataInputStream.mark()但这没有给我任何东西(除了一个空的Exception消息)。

Is it possible to read the input stream for a second time? 是否可以再次读取输入流? And if so, can someone please point me out what I'm doing wrong here? 如果是这样,有人可以指出我在这里做错了什么吗?

EDIT: Here is my solution: 编辑:这是我的解决方案:

// First define the Output and Input streams.
OutputStream output = socket.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());

// Send the first packet to the application.
output.write("test"); // (not actual data that I sent)

// Make an empty byte array and fill it with the first response from the application.
byte[] incoming = new byte[200];
bis.read(incoming); //First packet receive

//Send a second packet to the application.
output.write("test2"); // (not actual data that I sent)

// Mark the Input stream to the length of the first response and reset the stream.
bis.mark(incoming.length);
bis.reset();

// Create a second empty byte array and fill it with the second response from the application.
byte[] incoming2 = new byte[200];
bis.read(incoming2);

I'm not sure if this is the most correct way to do this, but this way it worked for me. 我不确定这是否是最正确的方法,但这种方式对我有用。

I would use ByteArrayInput stream or something that you can reset. 我会使用ByteArrayInput流或你可以重置的东西。 That would involve reading the data into another type of input stream and then creating one. 这将涉及将数据读入另一种类型的输入流,然后创建一个。

InputStream has a markSupported() method that you could check on the original and the byte array one to find one that the mark will work with: InputStream有一个markSupported()方法,你可以检查原始和字节数组,找到一个标记将使用的方法:

https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#markSupported() https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#markSupported() https://docs.oracle.com/javase/7/docs/api/java/io /ByteArrayInputStream.html

The problem here is not re-reading the input. 这里的问题不是重新读取输入。 I don't see anything in the question that requires you to read the input twice. 我没有在问题中看到任何要求您阅读输入两次的内容。 The problem is the BufferedInputStream, which will read everything that is available to be read, including the second message, if it has already arrived. 问题是BufferedInputStream,它将读取可读取的所有内容,包括第二条消息(如果已经到达)。

The solution is not to use a buffered stream until you have completed the handshake. 解决方案是在完成握手之前不使用缓冲流。 Just issue a read on the socket input stream for exactly the length of the first message, do the handshake, and then proceed to construct and read the buffered stream. 只需在套接字输入流上发出一个读取,确切地知道第一条消息的长度,进行握手,然后继续构造和读取缓冲流。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM