package com.huimv.receiver.utils;

import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Component
public class TextUtil {

    //转义符处理
    public String translateSymbol2(String data) throws UnsupportedEncodingException {
        String data11 = data.replace("data=","");
        System.out.println("data11>>"+data11);
        return decode2(data11);
    }

    //转义符处理
    public String translateSymbol(String data) throws UnsupportedEncodingException {
        return decode(data.replace("data=","").replace("%3D","="));
    }

    //base64编码
    public String encode(String text) throws UnsupportedEncodingException {
        final Base64.Encoder encoder = Base64.getEncoder();
        final byte[] textByte = text.replaceAll(" ", "").getBytes("UTF-8");
        return encoder.encodeToString(textByte);
    }

    //base64解码
    public String decode2(String encodedText) throws UnsupportedEncodingException {
        final Base64.Decoder decoder = Base64.getDecoder();
        return new String(decoder.decode(encodedText.toString().replace("\r\n", "")), "UTF-8");
    }

    //base64解码
    public String decode(String encodedText) throws UnsupportedEncodingException {
        final Base64.Decoder decoder = Base64.getDecoder();
        return new String(decoder.decode(encodedText.toString().replace("\r\n", "")), "UTF-8");
    }
}
