<commit_before>package de.uniluebeck.itm.spitfire.nCoap.message.options;

import com.google.common.primitives.Bytes;
import com.google.common.primitives.Longs;
import de.uniluebeck.itm.spitfire.nCoap.message.options.OptionRegistry.OptionName;
import org.apache.log4j.Logger;

import java.util.Arrays;

/**
 *
 * @author <NAME>
 */
public class UintOption extends Option{

    private static Logger log = Logger.getLogger(UintOption.class.getName());

    //constructor with encoded value should only be used for incoming messages
    UintOption(OptionName optionName, byte[] value) throws InvalidOptionException{
        super(optionName);
        setValue(optionName, value);

        if(log.isDebugEnabled()){
            log.debug("[UintOption] New Option (" + optionName + ")" +
                      " created (Value: " + getHexString(value)+ ")");
        }
    }

    //constructor with decoded value should only be used for outgoing messages
    UintOption(OptionName optionName, long value) throws InvalidOptionException{
        super(optionName);
        setValue(optionName, value);

        if(log.isDebugEnabled()){
            log.debug("[UintOption] New Option (" + optionName + ")" +
                      " created (Value: " + getHexString(this.value)+ ")");
        }
    }

    private void setValue(OptionName optionName, byte[] bytes) throws InvalidOptionException {
        //Check value length constraints
        if(bytes.length > OptionRegistry.getMaxLength(optionName)){
            String msg = "[UintOption] Value length for " + optionName +
                     " option must not be longer than " + OptionRegistry.getMaxLength(optionName) +
                     " but is " + bytes.length + ".";
            throw new InvalidOptionException(optionNumber, msg);
        }

        this.value = removeLeadingZeroBytes(bytes);
    }

    //Sets the options value after checking its correctness
    private void setValue(OptionName optionName, long value) throws InvalidOptionException{
        //Check value constraints
        int max_length = OptionRegistry.getMaxLength(optionName);
        long max_value = (long)Math.pow(2, max_length * 8) - 1;
        
        if(value < 0 || value > max_value){
            String msg = "[UintOption] Value for " + optionName +
                     " option must not be negative or greater than " + max_value + " but is " + value + ".";
            throw new InvalidOptionException(optionNumber, msg);
        }

        //Set value if there was no Exception thrown so far
        this.value = removeLeadingZeroBytes(Longs.toByteArray(value));
    }

    private byte[] removeLeadingZeroBytes(byte[] bytes) throws InvalidOptionException {
        //Empty array and thus nothing to remove
        if(bytes.length == 0){
            return bytes;
        }

        //Remove eventual leading zeros
        int iMax = Math.max(8, bytes.length);
        for(int i = 0; i < iMax; i++){
            if(bytes[i] != 0){
                return Arrays.copyOfRange(bytes, i, bytes.length);
            }
        }

        //All elements were zeros so return empty array
        return new byte[0];
    }

    public long getDecodedValue(){
        return Longs.fromByteArray(Bytes.concat(new byte[8-value.length], value));
    }
    /**
     * This method checks the equality of any given object with the current UintOption from which the method
     * is called. The method returns <code>true</code> if and only if the given object is an instance of
     * UintOption and has both the same {@link OptionName} and value as the current UintOption.
     * @param o
     * @return   <code>true</code> if and only if the given object is an instance of
     * UintOption and has both the same OptionName and value as the current UintOption. Otherwise it returns
     * <code>false</code>.
     */
    @Override
    public boolean equals(Object o) {
        if(!(o instanceof UintOption)){
            return false;
        }
        UintOption opt = (UintOption) o;
        if((optionNumber == opt.optionNumber) && (Arrays.equals(value, opt.value))){
            return true;
        }
        return false;
    }
}
<commit_msg>Bugfix removeLeadingZeroBytes() in UintOption<commit_after>package de.uniluebeck.itm.spitfire.nCoap.message.options;

import com.google.common.primitives.Bytes;
import com.google.common.primitives.Longs;
import de.uniluebeck.itm.spitfire.nCoap.message.options.OptionRegistry.OptionName;
import org.apache.log4j.Logger;

import java.util.Arrays;

/**
 *
 * @author <NAME>
 */
public class UintOption extends Option{

    private static Logger log = Logger.getLogger(UintOption.class.getName());

    //constructor with encoded value should only be used for incoming messages
    UintOption(OptionName optionName, byte[] value) throws InvalidOptionException{
        super(optionName);
        setValue(optionName, value);

        if(log.isDebugEnabled()){
            log.debug("[UintOption] New Option (" + optionName + ")" +
                      " created (Value: " + getHexString(value)+ ")");
        }
    }

    //constructor with decoded value should only be used for outgoing messages
    UintOption(OptionName optionName, long value) throws InvalidOptionException{
        super(optionName);
        setValue(optionName, value);

        if(log.isDebugEnabled()){
            log.debug("[UintOption] New Option (" + optionName + ")" +
                      " created (Value: " + getHexString(this.value)+ ")");
        }
    }

    private void setValue(OptionName optionName, byte[] bytes) throws InvalidOptionException {
        //Check value length constraints
        if(bytes.length > OptionRegistry.getMaxLength(optionName)){
            String msg = "[UintOption] Value length for " + optionName +
                     " option must not be longer than " + OptionRegistry.getMaxLength(optionName) +
                     " but is " + bytes.length + ".";
            throw new InvalidOptionException(optionNumber, msg);
        }

        this.value = removeLeadingZeroBytes(bytes);
    }

    //Sets the options value after checking its correctness
    private void setValue(OptionName optionName, long value) throws InvalidOptionException{
        //Check value constraints
        int max_length = OptionRegistry.getMaxLength(optionName);
        long max_value = (long)Math.pow(2, max_length * 8) - 1;
        
        if(value < 0 || value > max_value){
            String msg = "[UintOption] Value for " + optionName +
                     " option must not be negative or greater than " + max_value + " but is " + value + ".";
            throw new InvalidOptionException(optionNumber, msg);
        }

        //Set value if there was no Exception thrown so far
        this.value = removeLeadingZeroBytes(Longs.toByteArray(value));
    }

    private byte[] removeLeadingZeroBytes(byte[] bytes) throws InvalidOptionException {
        //Empty array and thus nothing to remove
        if(bytes.length == 0){
            return bytes;
        }

        //Remove eventual leading zeros
        int iMin = Math.min(8, bytes.length);
        for(int i = 0; i < iMin; i++){
            if(bytes[i] != 0){
                return Arrays.copyOfRange(bytes, i, bytes.length);
            }
        }

        //All elements were zeros so return empty array
        return new byte[0];
    }

    public long getDecodedValue(){
        return Longs.fromByteArray(Bytes.concat(new byte[8-value.length], value));
    }
    /**
     * This method checks the equality of any given object with the current UintOption from which the method
     * is called. The method returns <code>true</code> if and only if the given object is an instance of
     * UintOption and has both the same {@link OptionName} and value as the current UintOption.
     * @param o
     * @return   <code>true</code> if and only if the given object is an instance of
     * UintOption and has both the same OptionName and value as the current UintOption. Otherwise it returns
     * <code>false</code>.
     */
    @Override
    public boolean equals(Object o) {
        if(!(o instanceof UintOption)){
            return false;
        }
        UintOption opt = (UintOption) o;
        if((optionNumber == opt.optionNumber) && (Arrays.equals(value, opt.value))){
            return true;
        }
        return false;
    }
}

{'fraction_non_alphanumeric': 0.09914204003813155, 'fraction_numerical': 0.002621544327931363, 'mean_word_length': 2.4653179190751446, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4743456', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2209, 'n_words': 683}