<commit_before>package us.dot.its.jpo.ode.plugin.j2735.oss;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import javax.xml.bind.DatatypeConverter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.oss.asn1.AbstractData;
import com.oss.asn1.DecodeFailedException;
import com.oss.asn1.DecodeNotSupportedException;
import com.oss.asn1.PERUnalignedCoder;

import us.dot.its.jpo.ode.j2735.J2735;
import us.dot.its.jpo.ode.j2735.dsrc.BasicSafetyMessage;
import us.dot.its.jpo.ode.j2735.dsrc.MessageFrame;
import us.dot.its.jpo.ode.plugin.asn1.Asn1Object;
import us.dot.its.jpo.ode.plugin.asn1.Asn1Plugin;
import us.dot.its.jpo.ode.plugin.j2735.J2735Bsm;
import us.dot.its.jpo.ode.plugin.j2735.J2735MessageFrame;
import us.dot.its.jpo.ode.plugin.j2735.oss.OssBsmPart2Content.OssBsmPart2Exception;
import us.dot.its.jpo.ode.plugin.j2735.oss.OssMessageFrame.OssMessageFrameException;

public class OssAsn1Coder implements Asn1Plugin {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private PERUnalignedCoder coder;

    public OssAsn1Coder() {
        coder = J2735.getPERUnalignedCoder();
    }

    @Override
    public Asn1Object UPER_DecodeBase64(String base64Msg) {
        return UPER_DecodeMessageFrameBytes(DatatypeConverter.parseBase64Binary(base64Msg));
    }

    @Override
    public Asn1Object UPER_DecodeMessageFrameHex(String hexMsg) {
        return UPER_DecodeMessageFrameBytes(DatatypeConverter.parseHexBinary(hexMsg));
    }

    @Override
    public Asn1Object UPER_DecodeMessageFrameBytes(byte[] byteArrayMsg) {

        InputStream ins = new ByteArrayInputStream(byteArrayMsg);

        MessageFrame mf = new MessageFrame();

        J2735Bsm returnValue = null;

        try {
            coder.decode(ins, mf);
            returnValue = OssMessageFrame.genericMessageFrame(mf).getValue();
        } catch (Exception e) {
            logger.error("Error decoding ", e);
        } finally {
            try {
                ins.close();
            } catch (IOException e) {
                logger.warn("Error closing input stream: ", e);
            }
        }

        return returnValue;
    }

    @Override
    public Asn1Object UPER_DecodeBsmBytes(byte[] byteArrayMsg) {
        InputStream ins = new ByteArrayInputStream(byteArrayMsg);

        BasicSafetyMessage bsm = new BasicSafetyMessage();
        J2735Bsm gbsm = null;

        try {
            coder.decode(ins, bsm);
            gbsm = OssBsm.genericBsm(bsm);
        } catch (Exception e) {
            logger.error("Error decoding ", e);
        } finally {
            try {
                ins.close();
            } catch (IOException e) {
                logger.warn("Error closing input stream: ", e);
            }
        }

        return gbsm;
    }

    @Override
    public Asn1Object UPER_DecodeBsmStream(InputStream ins) {
        BasicSafetyMessage bsm = new BasicSafetyMessage();
        J2735Bsm gbsm = null;

        try {
            if (ins.available() > 0) {
                logger.debug("DECODER - Message Size = {}", ins.available());
                coder.decode(ins, bsm);
                gbsm = OssBsm.genericBsm(bsm);
            }
        } catch (DecodeFailedException e) {
            AbstractData partialDecodedMessage = e.getDecodedData();
            if (partialDecodedMessage != null) {
                logger.debug("DECODER - Warning, message only partially decoded: {}", partialDecodedMessage);
            } else {
                logger.debug("DECODER - Warning, invalid bytes detected in file.");
            }
        } catch (DecodeNotSupportedException e) {
            logger.error("DECODER - Error decoding, data does not represent valid message", e);
        } catch (IOException e) {
            logger.error("DECODER - Error decoding, general error: {}", e);
        } catch (OssBsmPart2Exception e) {
            logger.error("DECODER - Error decoding, unable to parse BSM part 2: {}", e);
        }
        
        return gbsm;
    }

    @Override
    public Asn1Object UPER_DecodeMessageFrameStream(InputStream ins) {
        MessageFrame mf = new MessageFrame();
        J2735MessageFrame gmf = null;

        try {
            if (ins.available() > 0) {
                logger.debug("DECODER - Message Size = {}", ins.available());
                coder.decode(ins, mf);
                gmf = OssMessageFrame.genericMessageFrame(mf);
            }
        } catch (DecodeFailedException e) {
            AbstractData partialDecodedMessage = e.getDecodedData();
            if (partialDecodedMessage != null) {
                logger.debug("DECODER - Warning, message only partially decoded: {}", partialDecodedMessage);
            } else {
                logger.debug("DECODER - Warning, invalid bytes detected in file.");
            }
        } catch (DecodeNotSupportedException e) {
            logger.error("DECODER - Error decoding, data does not represent valid message", e);
        } catch (IOException e) {
            logger.error("DECODER - Error decoding, general error: {}", e);
        } catch (OssBsmPart2Exception e) {
            logger.error("DECODER - Error decoding, unable to parse BSM part 2: {}", e);
        } catch (OssMessageFrameException e) {
            logger.error("DECODER - Error decoding, message frame exception: {}", e);
        }

        return gmf;
    }

    @Override
    public String UPER_DecodeBase64ToJson(String base64Msg) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String UPER_DecodeHexToJson(String hexMsg) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String UPER_DecodeBytesToJson(byte[] byteArrayMsg) {
        // TODO Auto-generated method stub<commit_msg>Changed logging message<commit_after>package us.dot.its.jpo.ode.plugin.j2735.oss;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import javax.xml.bind.DatatypeConverter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.oss.asn1.AbstractData;
import com.oss.asn1.DecodeFailedException;
import com.oss.asn1.DecodeNotSupportedException;
import com.oss.asn1.PERUnalignedCoder;

import us.dot.its.jpo.ode.j2735.J2735;
import us.dot.its.jpo.ode.j2735.dsrc.BasicSafetyMessage;
import us.dot.its.jpo.ode.j2735.dsrc.MessageFrame;
import us.dot.its.jpo.ode.plugin.asn1.Asn1Object;
import us.dot.its.jpo.ode.plugin.asn1.Asn1Plugin;
import us.dot.its.jpo.ode.plugin.j2735.J2735Bsm;
import us.dot.its.jpo.ode.plugin.j2735.J2735MessageFrame;
import us.dot.its.jpo.ode.plugin.j2735.oss.OssBsmPart2Content.OssBsmPart2Exception;
import us.dot.its.jpo.ode.plugin.j2735.oss.OssMessageFrame.OssMessageFrameException;

public class OssAsn1Coder implements Asn1Plugin {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private PERUnalignedCoder coder;

    public OssAsn1Coder() {
        coder = J2735.getPERUnalignedCoder();
    }

    @Override
    public Asn1Object UPER_DecodeBase64(String base64Msg) {
        return UPER_DecodeMessageFrameBytes(DatatypeConverter.parseBase64Binary(base64Msg));
    }

    @Override
    public Asn1Object UPER_DecodeMessageFrameHex(String hexMsg) {
        return UPER_DecodeMessageFrameBytes(DatatypeConverter.parseHexBinary(hexMsg));
    }

    @Override
    public Asn1Object UPER_DecodeMessageFrameBytes(byte[] byteArrayMsg) {

        InputStream ins = new ByteArrayInputStream(byteArrayMsg);

        MessageFrame mf = new MessageFrame();

        J2735Bsm returnValue = null;

        try {
            coder.decode(ins, mf);
            returnValue = OssMessageFrame.genericMessageFrame(mf).getValue();
        } catch (Exception e) {
            logger.error("Error decoding ", e);
        } finally {
            try {
                ins.close();
            } catch (IOException e) {
                logger.warn("Error closing input stream: ", e);
            }
        }

        return returnValue;
    }

    @Override
    public Asn1Object UPER_DecodeBsmBytes(byte[] byteArrayMsg) {
        InputStream ins = new ByteArrayInputStream(byteArrayMsg);

        BasicSafetyMessage bsm = new BasicSafetyMessage();
        J2735Bsm gbsm = null;

        try {
            coder.decode(ins, bsm);
            gbsm = OssBsm.genericBsm(bsm);
        } catch (Exception e) {
            logger.error("Error decoding ", e);
        } finally {
            try {
                ins.close();
            } catch (IOException e) {
                logger.warn("Error closing input stream: ", e);
            }
        }

        return gbsm;
    }

    @Override
    public Asn1Object UPER_DecodeBsmStream(InputStream ins) {
        BasicSafetyMessage bsm = new BasicSafetyMessage();
        J2735Bsm gbsm = null;

        try {
            if (ins.available() > 0) {
                logger.debug("DECODER - Message Size = {}", ins.available());
                coder.decode(ins, bsm);
                gbsm = OssBsm.genericBsm(bsm);
            }
        } catch (DecodeFailedException e) {
            AbstractData partialDecodedMessage = e.getDecodedData();
            if (partialDecodedMessage != null) {
                logger.debug("DECODER - Warning, message only partially decoded: {}", partialDecodedMessage);
            } else {
                logger.debug("DECODER - Ignoring extraneous bytes at the end of the input stream.");
            }
        } catch (DecodeNotSupportedException e) {
            logger.error("DECODER - Error decoding, data does not represent valid message", e);
        } catch (IOException e) {
            logger.error("DECODER - Error decoding, general error: {}", e);
        } catch (OssBsmPart2Exception e) {
            logger.error("DECODER - Error decoding, unable to parse BSM part 2: {}", e);
        }
        
        return gbsm;
    }

    @Override
    public Asn1Object UPER_DecodeMessageFrameStream(InputStream ins) {
        MessageFrame mf = new MessageFrame();
        J2735MessageFrame gmf = null;

        try {
            if (ins.available() > 0) {
                logger.debug("DECODER - Message Size = {}", ins.available());
                coder.decode(ins, mf);
                gmf = OssMessageFrame.genericMessageFrame(mf);
            }
        } catch (DecodeFailedException e) {
            AbstractData partialDecodedMessage = e.getDecodedData();
            if (partialDecodedMessage != null) {
                logger.debug("DECODER - Warning, message only partially decoded: {}", partialDecodedMessage.toString());
            } else {
                logger.debug("DECODER - Ignoring extraneous bytes at the end of the input stream.");
            }
        } catch (DecodeNotSupportedException e) {
            logger.error("DECODER - Error decoding, data does not represent valid message", e);
        } catch (IOException e) {
            logger.error("DECODER - Error decoding, general error: {}", e);
        } catch (OssBsmPart2Exception e) {
            logger.error("DECODER - Error decoding, unable to parse BSM part 2: {}", e);
        } catch (OssMessageFrameException e) {
            logger.error("DECODER - Error decoding, message frame exception: {}", e);
        }

        return gmf;
    }

    @Override
    public String UPER_DecodeBase64ToJson(String base64Msg) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String UPER_DecodeHexToJson(String hexMsg) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String UPER_DecodeBytesToJson(byte[] byteArrayMsg) {
        // TODO Auto-generated method stub
{'fraction_non_alphanumeric': 0.1001100110011001, 'fraction_numerical': 0.017432512482017434, 'mean_word_length': 2.3650341685649203, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2657503', 'n_tokens_mistral': 3605, 'n_tokens_neox': 3430, 'n_words': 735}