Storing/retrieving arrays in a data structure...
Hello, I have a problem with my code that I have been trying to fix for quite a few days now, and I can't seem to find an answer.
Basically, part of my program is set to receive datagram packets and write the bytes received into a file. However, I am trying to program it to handle delivery of out of order packets. The way I designed the byte arrays sent in each packet, each byte array is identified by a unique integer which I have encoded in the first four bytes of the byte array. So the receiving end of my program just checks that number to know which order the byte arrays are to be written to the file. I am having a lot of trouble "buffering" the byte arrays which are received but not ready to be written to the file yet...
I have tried this using two-dimensional byte arrays, ArrayLists, HashMaps, Vectors and LinkedLists and I seem to always get the same problem.
The problem is that, suppose I have 5 byte arrays stored in a LinkedList, and I am ready to write all of them to the file (they are already in the proper order to be written to the file). I will get the first byte array (removing it from the list, and decreasing the size of the list by 1), then after writing that byte array to the file, I get the next one, but it grabs the original byte array that I removed from the list! It does this for all five of them, it repeatedly gets the first one grabbed.
This is literally driving me crazy... I have tested the frame itself and printed out the specific bytes (before adding them to the linkedlist), and the frames are all different (with different sequence numbers), so they all get added to different spots in the list. But then when I keep grabbing (and removing) the lowest element from the list, it's ALWAYS the initial one that I grabbed... I've tried everything, like declaring the nextFrame type in another section. Even setting nextFrame to null after writing it does the same thing...
// Buffer for which to temporarily place out-of-order frames that might be received
LinkedList<ByteArray> bufferedFrames = new LinkedList<ByteArray>();
for (int i = 0; i < windowSize; i++) bufferedFrames.add(i, null);
// stuff here
// Place the frame into the buffer, frameNum-sequenceNum is just a
// number from 0 to some maximum value, all frames will have a unique
// integer for it, repeated frames are filtered out previously
bufferedFrames.set(frameNum - sequenceNum, new ByteArray(frame));
// Perform the set method a few more times here, adding different frames to
// different spots...
while (bufferedFrames.get(0) != null)
byte nextFrame = bufferedFrames.removeFirst().getBytes();
bufferedFrames.add(windowSize - 1, null);
writeFrame(nextFrame); // Just goes through the bytes and writes to file
// The wrapper class
private class ByteArray
private byte bytes;
public ByteArray(byte bytes)
this.bytes = bytes;
public byte getBytes()
It's like they're just disappearing... is there something that I am seriously overlooking? Or if you have any better ideas for temporarily storing byte arrays, please help!