java - How to read a DataInputStream twice or more than twice? -
i have socket connection application hosted elsewhere. once connected made outputstream
, datainputstream
.
once connection has been made, use outputstream
send out handshake packet application. once handshake has been approved, returns packet through datainputstream
(1).
this packet processed , returned application outputstream
.
if returned data valid, packet datainputstream
(2). however, have not been able read packet through datainputstream
.
i have tried use datainputstream.marksupported()
, datainputstream.mark()
gave me nothing (except empty exception message).
is possible read input stream second time? , if so, can please point me out i'm doing wrong here?
edit: here solution:
// first define output , input streams. outputstream output = socket.getoutputstream(); bufferedinputstream bis = new bufferedinputstream(socket.getinputstream()); // send first packet application. output.write("test"); // (not actual data sent) // make empty byte array , fill first response application. byte[] incoming = new byte[200]; bis.read(incoming); //first packet receive //send second packet application. output.write("test2"); // (not actual data sent) // mark input stream length of first response , reset stream. bis.mark(incoming.length); bis.reset(); // create second empty byte array , fill second response application. byte[] incoming2 = new byte[200]; bis.read(incoming2);
i'm not sure if correct way this, way worked me.
i use bytearrayinput stream or can reset. involve reading data type of input stream , creating one.
inputstream has marksupported()
method check on original , byte array 1 find 1 mark work with:
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
Comments
Post a Comment