2

I'm looking for a way to read a large number of ints from a DataInputStream and load them into an array. I know exactly how many ints I need to read, but it'd be nice if there was a cleaner way than to simply iterate.

There is already a way to do this with a byte array, just by using the DataInputStream#read(byte[]) method, but there is no equivalent for int[]. I could obviously read the data into a byte array by creating a byte array with the length multiplied by four, but I really need the data in ints.

The way I see it, there are three possibilities I could use:

  1. Somehow load the data directly into an int[].
  2. Somehow convert a byte[] into an int[].
  3. Iterate through the array and just use readInt().

While the first two solutions are much preferred, I currently don't know of a way to do either. Is there any simple way to do this without iteration?

Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • It seems to me that readInt() in a loop should work fine. If properly opened the DataInputStream should be buffered so the overhead would be minimal. – Hot Licks Aug 30 '12 at 00:26
  • @HotLicks That might be what I end up doing. I'd just love for this particular bit of code to be as fast as possible, since my application will probably be doing a lot of File IO. – Alexis King Aug 30 '12 at 00:26
  • Unless you get into the nio stuff (if even there), or maybe native method stuff, there's no way to do memory-mapped I/O such that you could write directly to an array of int. – Hot Licks Aug 30 '12 at 01:08
  • @HotLicks faced the same problem. Do you mean `dataInStream.readInt()` doesn't make I/O call to a file system, but wait till you make another operations. But how does it know that you gonna do another operations? – VB_ Oct 27 '16 at 09:11

2 Answers2

1

It would appear that the only way to do this is a simple for loop and using readInt(). This is what I eventually opted to do.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • haven't you find any more effective way to read lots of integers? I have a binary file which ocnsists only from integers, there are millions of integers. Is `while` loop with `readInt` the best choice? P.S. the binary file is formed by me, if needed I can change file format – VB_ Oct 27 '16 at 09:18
0

You could try using a ByteBuffer, which is able to leave the data in the OS memory and therefore reads pretty quick. It also has a getInt() method, which should allow you to directly read Integers and put them into your array.

Peter Ilfrich
  • 3,727
  • 3
  • 31
  • 35
  • Would just be more overhead vs readInt() from the DataInputStream. – Hot Licks Aug 30 '12 at 00:25
  • 1
    Another problem here is that I'm actually using the `DataInputStream` for much more than just this int array. This is only a portion of the data. I can't really go for another approach without drastically increasing the amount of work I need to do. – Alexis King Aug 30 '12 at 00:26
  • @JakeKing With DataInputStream your best and only option is to use a loop with `readInt()`. I would make sure you are using BufferedInputStream as well. ;) – Peter Lawrey Aug 30 '12 at 07:28