<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guerinet.utils;

import android.content.Context;
import android.support.annotation.Nullable;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;

import timber.log.Timber;

/**
 * Utility methods for loading and saving files to internal storage
 * @author <NAME>
 * @since 1.0.18
 */
public class StorageUtils {

    /**
     * @param context  App context
     * @param fileName Internal storage file name
     * @param tag      Tag to use for any caught exception
     * @return The stream to the file, null if an error comes up
     */
    public static @Nullable FileInputStream load(Context context, String fileName, String tag) {
        try {
            //Return the FileInputStream from the given file name
            return context.openFileInput(fileName);
        } catch (FileNotFoundException e) {
            Timber.e("File not found: %s", tag);
            return null;
        }
    }

    /**
     * Loads the object at the given file name
     *
     * @param context  App context
     * @param type     Object class
     * @param fileName File name
     * @param tag      Tag to use for any caught exception
     * @return The object loaded, null if none
     */
    @SuppressWarnings("unchecked")
    public static @Nullable <T> T loadObject(Context context, Class<T> type, String fileName,
            String tag) {
        //If the FIS is null, don't continue
        FileInputStream fis = load(context, fileName, tag);
        if (fis == null) {
            return null;
        }

        //Set up the ObjectInputStream from the FileInputStream
        try {
            ObjectInputStream in = new ObjectInputStream(fis);
            return type.cast(in.readObject());
        } catch (Exception e) {
            Timber.e(e, "Error loading %s from internal storage", tag);
            return null;
        }
    }
}<commit_msg>Added method to save an object to internal storage.<commit_after>/*
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guerinet.utils;

import android.content.Context;
import android.support.annotation.Nullable;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import timber.log.Timber;

/**
 * Utility methods for loading and saving files to internal storage
 * @author <NAME>
 * @since 1.0.18
 */
public class StorageUtils {

    /**
     * @param context  App context
     * @param fileName Internal storage file name
     * @param tag      Tag to use for any caught exception
     * @return The stream to the file, null if an error comes up
     */
    public static @Nullable FileInputStream load(Context context, String fileName, String tag) {
        try {
            //Return the FileInputStream from the given file name
            return context.openFileInput(fileName);
        } catch (FileNotFoundException e) {
            Timber.e("Error: File not found: %s", tag);
            return null;
        }
    }

    /**
     * Loads the object at the given file name
     *
     * @param context  App context
     * @param type     Object class
     * @param fileName File name
     * @param tag      Tag to use for any caught exception
     * @return The object loaded, null if none
     */
    @SuppressWarnings("unchecked")
    public static @Nullable <T> T loadObject(Context context, Class<T> type, String fileName,
            String tag) {
        //If the FIS is null, don't continue
        FileInputStream fis = load(context, fileName, tag);
        if (fis == null) {
            return null;
        }

        //Set up the ObjectInputStream from the FileInputStream
        try {
            ObjectInputStream in = new ObjectInputStream(fis);
            return type.cast(in.readObject());
        } catch (Exception e) {
            Timber.e(e, "Error loading %s from internal storage", tag);
            return null;
        }
    }

    /**
     * Saves an object to internal storage
     *
     * @param context  App context
     * @param object   Object to save
     * @param fileName File name to save the object under
     * @param tag      Tag to use in case of an error
     */
    public static void saveObject(Context context, Object object, String fileName, String tag) {
        try {
            //Open a FileOutputStream to a file in internal storage
            FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            //Open an ObjectOutputStream using the FileOutputStream
            ObjectOutputStream out = new ObjectOutputStream(fos);
            //Write the object to the stream
            out.writeObject(object);
        } catch(Exception e) {
            Timber.e(e, "Error saving %s to internal storage", tag);
        }
    }
}
{'fraction_non_alphanumeric': 0.08018632507070371, 'fraction_numerical': 0.003992680086508068, 'mean_word_length': 2.4771544245228454, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3137060', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1527, 'n_words': 637}