Best practice to read limited length from an input stream

user1772660

I have packets that are sent over sockets which are preceded by length headers. So every transmission comes with 4 bytes of length information followed by the packet. Therefore, I must limit my read() to never exceed the length, so that I don't accidentally read the next packet in line.

InputStream in is the input stream, ByteArrayOutputStream byteStream is the stream I write incoming packets into and int len is the length of an incoming transmission.

Here is what I came up with:

for (int i = 0; i < len; i++) {
    byteStream.write(in.read());
}

This is absolutely horrific; reading bytes one by one. I was hoping to see if there is a better way of doing this using buffers (kind of similar to the canonical way of copying streams).

rolfl

Wrap the input stream in a DataInputStream and then use the readInt() and readFully() methods to get the size and bytes of data out.

int len = dis.readInt();
// assuming relatively small byte arrays, use smarter logic for huge streams....
byte[] buffer = new byte[len];
dis.readFully(buffer);
byteStream.write(buffer);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What's idiom/best practice with UncheckedIOException and Stream API?

From Dev

Hibernate 4.2.2 create blob from unknown-length input stream

From Dev

Best practice for shell input parameters?

From Dev

Best practice to store/check read state

From Dev

Java read JSON input stream

From Dev

Read next character (full unicode code point) from Java input stream

From Dev

How to read input of unknown length using fgets

From Dev

String from input is limited?

From Dev

How to read from std.in if the length of the input is not define

From Dev

Is there a more elegant way to read the numbers from the input stream?

From Dev

Java 8 read from an input stream to an output stream, while aggregating items

From Dev

Best Practice: Input Validation (Android)

From Dev

Erlang: Read from an input stream in a efficient way

From Dev

Best practice for storing an array of struct of undefined length

From Dev

java read from network input stream with buffer(need to exclude emty symbols)

From Dev

Hibernate 4.2.2 create blob from unknown-length input stream

From Dev

Best practice for shell input parameters?

From Dev

Read float from input stream without trailing "E"

From Dev

Best practice to store/check read state

From Dev

What's idiom/best practice with UncheckedIOException and Stream API?

From Dev

Read from a file or stream

From Dev

Read next character (full unicode code point) from Java input stream

From Dev

Max character length for Read command (input)

From Dev

How to read from std.in if the length of the input is not define

From Dev

mvc best practice for storing user input

From Dev

Nodejs infinitive stream from file read stream

From Dev

What is the best practice for when to do input validation?

From Dev

Very slow read from the input stream fetched from the item - Commons FileUpload API

From Dev

Read a data from an input Stream line by line in Smalltalk

Related Related

  1. 1

    What's idiom/best practice with UncheckedIOException and Stream API?

  2. 2

    Hibernate 4.2.2 create blob from unknown-length input stream

  3. 3

    Best practice for shell input parameters?

  4. 4

    Best practice to store/check read state

  5. 5

    Java read JSON input stream

  6. 6

    Read next character (full unicode code point) from Java input stream

  7. 7

    How to read input of unknown length using fgets

  8. 8

    String from input is limited?

  9. 9

    How to read from std.in if the length of the input is not define

  10. 10

    Is there a more elegant way to read the numbers from the input stream?

  11. 11

    Java 8 read from an input stream to an output stream, while aggregating items

  12. 12

    Best Practice: Input Validation (Android)

  13. 13

    Erlang: Read from an input stream in a efficient way

  14. 14

    Best practice for storing an array of struct of undefined length

  15. 15

    java read from network input stream with buffer(need to exclude emty symbols)

  16. 16

    Hibernate 4.2.2 create blob from unknown-length input stream

  17. 17

    Best practice for shell input parameters?

  18. 18

    Read float from input stream without trailing "E"

  19. 19

    Best practice to store/check read state

  20. 20

    What's idiom/best practice with UncheckedIOException and Stream API?

  21. 21

    Read from a file or stream

  22. 22

    Read next character (full unicode code point) from Java input stream

  23. 23

    Max character length for Read command (input)

  24. 24

    How to read from std.in if the length of the input is not define

  25. 25

    mvc best practice for storing user input

  26. 26

    Nodejs infinitive stream from file read stream

  27. 27

    What is the best practice for when to do input validation?

  28. 28

    Very slow read from the input stream fetched from the item - Commons FileUpload API

  29. 29

    Read a data from an input Stream line by line in Smalltalk

HotTag

Archive