<commit_before>/*
 * Copyright (c) 2010-2012 Grid Dynamics Consulting Services, Inc, All Rights Reserved
 * http://www.griddynamics.com
 *
 * This library is free software; you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.griddynamics.jagger.util;

import java.io.*;
import java.util.concurrent.atomic.AtomicLong;

import biz.source_code.base64Coder.Base64Coder;
import com.griddynamics.jagger.exception.TechnicalException;
import org.jboss.serial.io.JBossObjectInputStream;
import org.jboss.serial.io.JBossObjectOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// TODO Avoid static code. Extract interface and make this one default implementation.
public class SerializationUtils {
    private static final Logger log = LoggerFactory.getLogger(SerializationUtils.class);
    private static final AtomicLong fromStringCount = new AtomicLong(0);
    private static final AtomicLong toStringCount = new AtomicLong(0);
    private static boolean useJBoss=true;//if false - using default java serialization

    private SerializationUtils() {
    }

    public static <T extends Serializable> T fromString(String s) {
        if (s.isEmpty()) {
            log.info("fromString({}, '{}')", fromStringCount.getAndIncrement(), s);
        }
        ObjectInputStream ois = null;
        InputStream in=null;
        try {
            byte[] data = Base64Coder.decode(s);

            in=new ByteArrayInputStream(data);
            try{
                //TODO fixes for support old reports
                ois=new JBossObjectInputStream(in);
            } catch (IOException e) {
                // /data stored not with JBoss
                ois=new ObjectInputStream(in);
            }
            T obj = (T) ois.readObject();
            return obj;
        } catch (IOException e) {
            log.error("Deserialization exception ", e);
            log.error("fromString('{}')", s);
            throw new TechnicalException(e);
        } catch (ClassNotFoundException e) {
            log.error("Deserialization exception ", e);
            throw new TechnicalException(e);
        } finally {
            if (ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    log.warn("Exception during closing InputStream: ",e);
                }
            } else {
                if (in!=null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        log.warn("Exception during closing InputStream: ",e);
                    }
                }
            }
        }
    }

    public static String toString(Serializable o) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            if(useJBoss){
                oos=new JBossObjectOutputStream(baos);
            } else{
                oos=new ObjectOutputStream(baos);
            }
            oos.writeObject(o);
        } catch (IOException e) {
            log.error("Serialization exception ", e);
            throw new TechnicalException(e);
        }  finally {
            if (oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    log.warn("Exception during closing OutputStream: ",e);
                }
            }
        }

        String s = new String(Base64Coder.encode(baos.toByteArray()));
        if (s.isEmpty()) {
            log.info("toString({}, '{}', '{}')", new Object[] {toStringCount.getAndIncrement(), s, o});
        }
        try {
            baos.close();
        } catch (IOException e) {
            log.warn("Exception during closing OutputStream: ",e);
        }
        return s;
    }

    public static byte[] serialize(Object obj) {
        ObjectOutputStream ous = null;
        ByteArrayOutputStream baos=null;
        try {
            baos = new ByteArrayOutputStream();
            if(useJBoss){
                ous=new JBossObjectOutputStream(baos);
            } else {
                ous=new ObjectOutputStream(baos);
            }
            ous.writeObject(obj);

            return baos.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException("Error during " + obj + " serialization", e);
        }   finally {
            if (ous!=null){
                try {
                    ous.close();
                } catch (IOException e) {
                    log.warn("Exception during closing OutputStream: ",e);
                }
            } else {
                if(baos!=null){
                    try {
                        baos.close();
                    } catch (IOException e) {
                        log.warn("Exception during closing OutputStream: ",e);
                    }
                }
            }
        }
    }

    public static Object deserialize(byte[] data) {
        ObjectInputStream ois = null;
        ByteArrayInputStream bais = null;
        try {
            bais = new ByteArrayInputStream(data);
            try{
                //TODO fixes for support old reports
                ois= new JBossObjectInputStream(bais);
            } catch (IOException e){
                //data stored not with JBoss
                ois=new ObjectInputStream(bais);
            }
            Object payload = ois.readObject();

            return payload;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    log.warn("Exception during closing InputStream: ",e);
                }
            } else {
                if(bais!=null){
                    try {
                        bais.close();
                    } catch (IOException e) {
                        log.warn("Exception during closing InputStream: ",e);
                    }
                }
            }
        }
    }

    public static Configurator getConfigurator() {
        return Configurator.configuratorInstance;<commit_msg>Refactored code (now uses Closeables.closeQuietly())<commit_after>/*
 * Copyright (c) 2010-2012 Grid Dynamics Consulting Services, Inc, All Rights Reserved
 * http://www.griddynamics.com
 *
 * This library is free software; you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.griddynamics.jagger.util;

import java.io.*;
import java.util.concurrent.atomic.AtomicLong;

import biz.source_code.base64Coder.Base64Coder;
import com.griddynamics.jagger.exception.TechnicalException;
import org.jboss.serial.io.JBossObjectInputStream;
import org.jboss.serial.io.JBossObjectOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.io.Closeables;

// TODO Avoid static code. Extract interface and make this one default implementation.
public class SerializationUtils {
    private static final Logger log = LoggerFactory.getLogger(SerializationUtils.class);
    private static final AtomicLong fromStringCount = new AtomicLong(0);
    private static final AtomicLong toStringCount = new AtomicLong(0);
    private static boolean useJBoss=true;//if false - using default java serialization

    private SerializationUtils() {
    }

    public static <T extends Serializable> T fromString(String s) {
        if (s.isEmpty()) {
            log.info("fromString({}, '{}')", fromStringCount.getAndIncrement(), s);
        }
        ObjectInputStream ois = null;
        InputStream in=null;
        try {
            byte[] data = Base64Coder.decode(s);

            in=new ByteArrayInputStream(data);
            try{
                //TODO fixes for support old reports
                ois=new JBossObjectInputStream(in);
            } catch (IOException e) {
                // /data stored not with JBoss
                ois=new ObjectInputStream(in);
            }
            T obj = (T) ois.readObject();
            return obj;
        } catch (IOException e) {
            log.error("Deserialization exception ", e);
            log.error("fromString('{}')", s);
            throw new TechnicalException(e);
        } catch (ClassNotFoundException e) {
            log.error("Deserialization exception ", e);
            throw new TechnicalException(e);
        } finally {
            Closeables.closeQuietly(ois);
            Closeables.closeQuietly(in);
        }
    }

    public static String toString(Serializable o) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            if(useJBoss){
                oos=new JBossObjectOutputStream(baos);
            } else{
                oos=new ObjectOutputStream(baos);
            }
            oos.writeObject(o);
        } catch (IOException e) {
            log.error("Serialization exception ", e);
            throw new TechnicalException(e);
        } finally {
            String s = new String(Base64Coder.encode(baos.toByteArray()));
            if (s.isEmpty()) {
                log.info("toString({}, '{}', '{}')", new Object[] {toStringCount.getAndIncrement(), s, o});
            }
            Closeables.closeQuietly(oos);
            return s;
        }
    }

    public static byte[] serialize(Object obj) {
        ObjectOutputStream ous = null;
        ByteArrayOutputStream baos=null;
        try {
            baos = new ByteArrayOutputStream();
            if(useJBoss){
                ous=new JBossObjectOutputStream(baos);
            } else {
                ous=new ObjectOutputStream(baos);
            }
            ous.writeObject(obj);

            return baos.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException("Error during " + obj + " serialization", e);
        }  finally {
            Closeables.closeQuietly(ous);
            Closeables.closeQuietly(baos);
        }
    }

    public static Object deserialize(byte[] data) {
        ObjectInputStream ois = null;
        ByteArrayInputStream bais = null;
        try {
            bais = new ByteArrayInputStream(data);
            try{
                //TODO fixes for support old reports
                ois= new JBossObjectInputStream(bais);
            } catch (IOException e){
                //data stored not with JBoss
                ois=new ObjectInputStream(bais);
            }
            Object payload = ois.readObject();

            return payload;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Closeables.closeQuietly(ois);
            Closeables.closeQuietly(bais);
        }
    }

    public static Configurator getConfigurator() {
        return Configurator.configuratorInstance;
{'fraction_non_alphanumeric': 0.0892479021253235, 'fraction_numerical': 0.003450709748255039, 'mean_word_length': 1.930820501034245, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6990492', 'n_tokens_mistral': 3360, 'n_tokens_neox': 3126, 'n_words': 973}