// Path: src/main/java/com/pearson/statspoller/utilities/core_utils/StackTrace.java
// public class StackTrace {
//     
//     private static final Logger logger = LoggerFactory.getLogger(StackTrace.class.getName());
//     
//     public static String getStringFromStackTrace(Exception exception) {
//         try {
//             StringWriter stringWriter = new StringWriter();
//             PrintWriter printWriter = new PrintWriter(stringWriter);
//             
//             exception.printStackTrace(printWriter);
//             
//             return stringWriter.toString();
//         }
//         catch (Exception e) {
//             logger.error(e.toString() + " - Failed to convert stack-trace to string");
//             return null;
//         }
//     }
//     
//     public static String getStringFromStackTrace(StackTraceElement[] stackTraceElements) {
//         try {
//             
//             StringBuilder stackTrace = new StringBuilder();
//             
//             for (StackTraceElement stackTraceElement : stackTraceElements) {
//                 stackTrace.append(stackTraceElement).append(System.lineSeparator());
//             }
//             
//             return stackTrace.toString();
//         }
//         catch (Exception e) {
//             logger.error(e.toString() + " - Failed to convert stack-trace to string");
//             return null;
//         }
//     }
//     
// }


import com.pearson.statspoller.utilities.core_utils.StackTrace;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
package com.pearson.statspoller.utilities.string_utils;


/**
 * @author Jeffrey Schmidt
 */
public class StringUtilities {
    
    private static final Logger logger = LoggerFactory.getLogger(StringUtilities.class.getName());
    
    private static final Map<String,Charset> charsetCache = new ConcurrentHashMap<>();

    public static Charset getCharsetFromString(String charset) {
        
        if (charset == null) {
            return null;
        } 
        
        Charset charsetToUse;
        
        try {
            charsetToUse = charsetCache.get(charset);
            
            if (charsetToUse == null) {
                charsetToUse = Charset.availableCharsets().get(charset);
                
                if (charsetToUse == null) {
                    logger.warn("Couldn't find Charset \"" + removeNewlinesFromString(charset) + "\". Using default charset.");
                    charsetToUse = Charset.defaultCharset();
                }
                else charsetCache.put(charset, charsetToUse);
            }
        }
        catch (Exception e) {
            logger.error("Error using Charset \"" + removeNewlinesFromString(charset) + "\". Using default charset." + 
// Path: src/main/java/com/pearson/statspoller/utilities/core_utils/StackTrace.java
// public class StackTrace {
//     
//     private static final Logger logger = LoggerFactory.getLogger(StackTrace.class.getName());
//     
//     public static String getStringFromStackTrace(Exception exception) {
//         try {
//             StringWriter stringWriter = new StringWriter();
//             PrintWriter printWriter = new PrintWriter(stringWriter);
//             
//             exception.printStackTrace(printWriter);
//             
//             return stringWriter.toString();
//         }
//         catch (Exception e) {
//             logger.error(e.toString() + " - Failed to convert stack-trace to string");
//             return null;
//         }
//     }
//     
//     public static String getStringFromStackTrace(StackTraceElement[] stackTraceElements) {
//         try {
//             
//             StringBuilder stackTrace = new StringBuilder();
//             
//             for (StackTraceElement stackTraceElement : stackTraceElements) {
//                 stackTrace.append(stackTraceElement).append(System.lineSeparator());
//             }
//             
//             return stackTrace.toString();
//         }
//         catch (Exception e) {
//             logger.error(e.toString() + " - Failed to convert stack-trace to string");
//             return null;
//         }
//     }
//     
// }


// Path: src/main/java/com/pearson/statspoller/utilities/string_utils/StringUtilities.java
import com.pearson.statspoller.utilities.core_utils.StackTrace;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

package com.pearson.statspoller.utilities.string_utils;


/**
 * @author Jeffrey Schmidt
 */
public class StringUtilities {
    
    private static final Logger logger = LoggerFactory.getLogger(StringUtilities.class.getName());
    
    private static final Map<String,Charset> charsetCache = new ConcurrentHashMap<>();

    public static Charset getCharsetFromString(String charset) {
        
        if (charset == null) {
            return null;
        } 
        
        Charset charsetToUse;
        
        try {
            charsetToUse = charsetCache.get(charset);
            
            if (charsetToUse == null) {
                charsetToUse = Charset.availableCharsets().get(charset);
                
                if (charsetToUse == null) {
                    logger.warn("Couldn't find Charset \"" + removeNewlinesFromString(charset) + "\". Using default charset.");
                    charsetToUse = Charset.defaultCharset();
                }
                else charsetCache.put(charset, charsetToUse);
            }
        }
        catch (Exception e) {
            logger.error("Error using Charset \"" + removeNewlinesFromString(charset) + "\". Using default charset." + 
                    e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));