user267817
user267817

Reputation:

Reading binary TCP data: the correct stream type

In my readMessage function, I get the error message "OutputStream is abstract; cannot be instantiated." Which data type should I use to store the read data? Please disregard that the return value is String.

private String readMessage() throws IOException {
  byte[] messageChunk = new byte[1024];
  int bytesRead = 0;
  OutputStream messageData = new OutputStream();

  boolean end = false;

  while ( !end ) {
    bytesRead = in.read(messageChunk);
    if ( bytesRead > 2 && messageChunk[bytesRead - 2] == '\r' ) end = true; // TODO: is -2 correct?
    else if ( bytesRead == 1 && messageChunk[bytesRead - 1] == '\n' ) end = true;
    messageData.write(messageChunk, 0, bytesRead);
  }

  return messageData.toString();
}

Upvotes: 1

Views: 216

Answers (2)

chathux
chathux

Reputation: 831

it is because OutputStream class is an abstract class. you cant create instances from abstract classes using new keyword without the implementation.

why dont you use ByteArrayOutputStream instead of OutputStream

Upvotes: 1

user207421
user207421

Reputation: 310916

It looks to me as though what you want isn't any of this, but rather simply a BufferedReader, and this entire method reduces to return reader.readLine(); But I don't know why you're sending length-words and line terminators. Have a think about your application protocol.

Upvotes: 0

Related Questions