I'm working on a multi threaded client/server application which transfers data over sockets. I store the byte[]
fragments in a ConcurrentHashMap<Integer, byte[]>
and all seems to work fine until the last .put()
overrides all the entries with that last one.
...
tf.putFragment(i, readBuffer.array());
System.out.println("check added frag " + i + " : " + Arrays.toString(tf.getFragment(i)));
}
barrier.await();
System.out.println("check added frag " + 5 + " : " + Arrays.toString(tf.getFragment(5)));
System.out.println("check added frag " + 10 + " : " + Arrays.toString(tf.getFragment(10)));
The first .put()
call from the snippet is from a loop where I add each fragment and check it afterwards to see if everything is fine (and it is). Afterwards I select 3 random entries to check them and now everything is overridden with the data from the last put, each entry.
What could cause this ? At the moment I am testing with only 1 thread.
Output:
downloaded frag 821 : [48, 48, 48, 53, 48, 55, 49, 49, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 53, 49, 52, 50, 55, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 53, 49, 54, 54, 54, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 50, 51, 56, 51, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 51, 48, 51, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 51, 50, 55, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 56, 53, 52, 49]
check added frag 821 : [48, 48, 48, 53, 48, 55, 49, 49, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 53, 49, 52, 50, 55, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 53, 49, 54, 54, 54, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 50, 51, 56, 51, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 51, 48, 51, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 51, 50, 55, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 56, 53, 52, 49]
downloaded frag 822 : [32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 57, 49, 56, 50, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 57, 52, 50, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 48, 54, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 50, 56, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 55, 57, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 116, 114, 97, 105, 108, 101, 114, 10, 60, 60, 32, 47, 83, 105, 122, 101, 32, 53]
check added frag 822 : [32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 57, 49, 56, 50, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 57, 52, 50, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 48, 54, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 50, 56, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 55, 57, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 116, 114, 97, 105, 108, 101, 114, 10, 60, 60, 32, 47, 83, 105, 122, 101, 32, 53]
downloaded frag 823 : [54, 32, 47, 82, 111, 111, 116, 32, 51, 49, 32, 48, 32, 82, 32, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 32, 47, 73, 68, 32, 91, 32, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 10, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 32, 93, 32, 62, 62, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 48, 52, 49, 57, 54, 10, 37, 37, 69]
check added frag 823 : [54, 32, 47, 82, 111, 111, 116, 32, 51, 49, 32, 48, 32, 82, 32, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 32, 47, 73, 68, 32, 91, 32, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 10, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 32, 93, 32, 62, 62, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 48, 52, 49, 57, 54, 10, 37, 37, 69]
check added frag 5 : [54, 32, 47, 82, 111, 111, 116, 32, 51, 49, 32, 48, 32, 82, 32, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 32, 47, 73, 68, 32, 91, 32, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 10, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 32, 93, 32, 62, 62, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 48, 52, 49, 57, 54, 10, 37, 37, 69]
check added frag 10 : [54, 32, 47, 82, 111, 111, 116, 32, 51, 49, 32, 48, 32, 82, 32, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 32, 47, 73, 68, 32, 91, 32, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 10, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 32, 93, 32, 62, 62, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 48, 52, 49, 57, 54, 10, 37, 37, 69]
check added frag 15 : [54, 32, 47, 82, 111, 111, 116, 32, 51, 49, 32, 48, 32, 82, 32, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 32, 47, 73, 68, 32, 91, 32, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 10, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 32, 93, 32, 62, 62, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 48, 52, 49, 57, 54, 10, 37, 37, 69]
EDIT: putFragment() code :
public void putFragment(int i,byte[] f) {
fragments.put(i, f);
}
it looks like you using a ByteBuffer and are putting the actual byte array into the value of the map.
From the ByteBuffer#array() javadoc :
Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.
So anytime you modify the Buffer later, all the values will get updated too.
You have to make a copy of the buffer using java.util.Arrays.copyOf(byte[] src, int length)