如何读取 DataInputStream 两次或两次以上?
How to read a DataInputStream twice or more than twice?
我有一个到我在别处托管的应用程序的套接字连接。连接后,我制作了 OutputStream
和 DataInputStream
.
建立连接后,我使用 OutputStream
向应用程序发送握手数据包。一旦此握手被批准,它 returns 一个数据包通过 DataInputStream
(1).
此数据包已处理并返回给带有 OutputStream
的应用程序。
如果此返回数据有效,我会从 DataInputStream
(2) 获得另一个数据包。但是,我无法通过 DataInputStream
.
读取此数据包
我曾尝试使用 DataInputStream.markSupported()
和 DataInputStream.mark()
,但这没有给我任何帮助(除了一条空的异常消息)。
是否可以二次读取输入流?如果是这样,有人可以指出我在这里做错了什么吗?
编辑:这是我的解决方案:
// 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);
我不确定这是否是最正确的方法,但这种方法对我有用。
我会使用 ByteArrayInput 流或您可以重置的东西。这将涉及将数据读入另一种类型的输入流,然后创建一个。
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
这里的问题不是重新读取输入。我没有在问题中看到任何需要您阅读输入两次的内容。问题是 BufferedInputStream,
它将读取所有可供读取的内容,包括第二条消息,如果它已经到达的话。
解决方案是在完成握手之前不要使用缓冲流。只需在套接字输入流上发出与第一条消息的长度完全一致的读取,进行握手,然后继续构建和读取缓冲流。
我有一个到我在别处托管的应用程序的套接字连接。连接后,我制作了 OutputStream
和 DataInputStream
.
建立连接后,我使用 OutputStream
向应用程序发送握手数据包。一旦此握手被批准,它 returns 一个数据包通过 DataInputStream
(1).
此数据包已处理并返回给带有 OutputStream
的应用程序。
如果此返回数据有效,我会从 DataInputStream
(2) 获得另一个数据包。但是,我无法通过 DataInputStream
.
我曾尝试使用 DataInputStream.markSupported()
和 DataInputStream.mark()
,但这没有给我任何帮助(除了一条空的异常消息)。
是否可以二次读取输入流?如果是这样,有人可以指出我在这里做错了什么吗?
编辑:这是我的解决方案:
// 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);
我不确定这是否是最正确的方法,但这种方法对我有用。
我会使用 ByteArrayInput 流或您可以重置的东西。这将涉及将数据读入另一种类型的输入流,然后创建一个。
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
这里的问题不是重新读取输入。我没有在问题中看到任何需要您阅读输入两次的内容。问题是 BufferedInputStream,
它将读取所有可供读取的内容,包括第二条消息,如果它已经到达的话。
解决方案是在完成握手之前不要使用缓冲流。只需在套接字输入流上发出与第一条消息的长度完全一致的读取,进行握手,然后继续构建和读取缓冲流。