How to read DataInputStream until the end without needing to catch an EOFException?
问题 Suppose we have some binary data byte[] data that only contains Integers. If I wanted to read this data utilizing a DataInputStream , the only approach I can come up with is the following: DataInputStream in = new DataInputStream(new ByteArrayInputStream(data)); try { while (true){ int i = in.readInt(); } } catch (EOFException e) { // we're done! } catch (IOException e){ throw new RuntimeException(e); } What bugs me about this is that reaching the end of the stream is expected and it would