Results 1 to 1 of 1
  1. #1
    pbandjay is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default 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.

    Java Code:
    // 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()
    		return bytes;
    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...

    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!
    Last edited by pbandjay; 11-03-2010 at 12:46 AM.

Similar Threads

  1. Replies: 1
    Last Post: 12-28-2008, 10:25 AM
  2. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 04:10 PM
  3. storing and retrieving a file as such
    By anil_manu in forum Advanced Java
    Replies: 0
    Last Post: 03-11-2008, 01:27 PM
  4. Retrieving data from the DB
    By yuchuang in forum JDBC
    Replies: 2
    Last Post: 11-27-2007, 08:59 AM
  5. Problem with storing and retrieving from a textfile
    By Albert in forum Advanced Java
    Replies: 1
    Last Post: 07-13-2007, 03:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts