HashMap reterival shows same value multiple time

I have a class "RadiusAttribute" it stores few values but when I retrieve values stored in it, I get one value repeated three times "panpanpan". Below is my code and the return statement in getAttributeBytes() gives only one value repeated three times panpanpan but when I try to log using "logger.debug(key + "=" + this.attributes.get(key));" it gives correct value pan, abc, xyz.

public class RadiusAttribute {
    

private static final int HEADER_LENGTH = 2;
private static byte[] packetBytes;

public final byte[] getBytes(){
        return this.packetBytes;
  }


public RadiusAttribute(final int type, final byte[] value) {

int length = HEADER_LENGTH + value.length;
ByteArrayOutputStream temp = new ByteArrayOutputStream(length);
temp.write(type);
temp.write(length);
temp.write(value);
temp.flush();

this.packetBytes = temp.toByteArray();

}

}

accessRequest.setAttribute(new RadiusAttribute(RadiusAttributeValues.USER_PASSWORD,userPass.getBytes()));

accessRequest.setAttribute(new RadiusAttribute(RadiusAttributeValues.NAS_IDENTIFIER,nas_value.getBytes()));

accessRequest.setAttribute(new RadiusAttribute(RadiusAttributeValues.USER_NAME,userName.getBytes()));
               

protected final byte[] getAttributeBytes() throws IOException {


ByteArrayOutputStream bytees = new  ByteArrayOutputStream();
synchronized (this.attributes){

logger.debug("printKeySet");
System.out.println(this.attributes.keySet());

int i=0;
for (Object key : this.attributes.keySet()) {
    logger.debug(i);
    i++;
    logger.debug(key + "=" + this.attributes.get(key));
    logger.debug((RadiusAttribute) this.attributes.get(key));
    bytees.write(((RadiusAttribute) this.attributes.get(key)).getBytes());
    logger.debug(((RadiusAttribute) this.attributes.get(key)).getBytes());
}

    logger.debug(String.valueOf(bytees));
    logger.debug(bytees.size());
    return bytees.toByteArray();
}
}

2021-02-22 22:01:31,369 DEBUG - 0 - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:245) 2021-02-22 22:01:31,369 DEBUG - 32=attributedata.RadiusAttribute@7bfcd12c - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:247) 2021-02-22 22:01:31,369 DEBUG - attributedata.RadiusAttribute@7bfcd12c - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:248) 2021-02-22 22:01:31,369 DEBUG - [B@42f30e0a - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:250) 2021-02-22 22:01:31,370 DEBUG - 1 - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:245) 2021-02-22 22:01:31,370 DEBUG - 1=attributedata.RadiusAttribute@24273305 - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:247) 2021-02-22 22:01:31,370 DEBUG - attributedata.RadiusAttribute@24273305 - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:248) 2021-02-22 22:01:31,370 DEBUG - [B@42f30e0a - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:250) 2021-02-22 22:01:31,370 DEBUG - 2 - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:245) 2021-02-22 22:01:31,370 DEBUG - 2=attributedata.RadiusAttribute@5b1d2887 - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:247) 2021-02-22 22:01:31,370 DEBUG - attributedata.RadiusAttribute@5b1d2887 - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:248) 2021-02-22 22:01:31,371 DEBUG - [B@42f30e0a - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:250) 2021-02-22 22:01:31,372 DEBUG - panpanpan - simulatorradius.RadiusPacket.getAttributeBytes(RadiusPacket.java:253)



Read more here: https://stackoverflow.com/questions/66319844/hashmap-reterival-shows-same-value-multiple-time

Content Attribution

This content was originally published by Pankaj Sheoran at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: