How to deal with different packet sizes?

I’ve been working on an MORPG for a while, and I’ve pretty much completed the entire structure for the networking. The current issue I’m dealing with is packet size. Up until now, when receiving packets, I’ve just been using a byte array for the data at a set amount and trimming all the excess 0-bytes that aren’t used.

Here’s part of the packet receiving code:


byte[] data = new byte[1024];
					
int readInt = input.read(data);   // (input instanceof DataInputStream) == true
if(readInt == -1) throw new EOFException();

However, the issue arises when larger packets come into play. The game does everything but rendering and player input on the server-side, and one such instance of a large packet sent to the client is a packet containing heightmap data for the terrain. Now, the heightmap is only 256x256. The data sent in a packet, however, is much larger. As each value of the heightmap is a Float, there will be 256x256x4 bytes sent to the client, approximately 262kb. I can’t just up the length of the byte array in the packet-receiving-code to 263000 bytes, because allocating that much data for every single packet received would be insanely slow and inefficient. Is there a way to figure out the size of the packet received from a DataInputStream before reading all the data? My best guess would be to have the first three bytes of the packet contain the length of the packet or something. I really have no idea what to do here, I started this project as a learning experience. :slight_smile: