/*
 *  Copyright (c) 2016, Glib Briia  <a href="mailto:glib.briia@assertthat.com">Glib Briia</a>
 *  Distributed under the terms of the MIT License
 */

package com.assertthat.selenium_shutterbug.utils.file;


import com.assertthat.selenium_shutterbug.utils.web.UnableTakeSnapshotException;
import org.apache.commons.io.IOUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by Glib_Briia on 17/06/2016.
 */
public class FileUtil {

    public static String getJsScript(String filePath) {
        try {
            return IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath));
        } catch (IOException e) {
            throw new UnableTakeSnapshotException("Unable to load JS script", e);
        }
    }

    public static void writeImage(BufferedImage imageFile, String extension, File fileToWriteTo) {
        try {
            ImageIO.write(imageFile, extension, fileToWriteTo);
        } catch (IOException e) {
            throw new UnableSaveSnapshotException(e);
        }
    }
}

@@ -438,16 +438,44 @@
 ception;
+%0Aimport java.io.InputStream;
 %0A%0A/**%0A *
@@ -627,32 +627,25 @@
     
-return IOUtils.toString(
+InputStream is = 
 Thre
@@ -716,16 +716,515 @@
 ilePath)
+;%0A            if (is == null) %7B%0A                // This is needed to load the files in an OSGI enviroment when enclosed in a bundle%0A                is = FileUtil.class.getClassLoader().getResourceAsStream(filePath);%0A            %7D%0A            // if the input stream is still null, this will avoid a non descriptive null pointer exception%0A            if (is == null) new UnableTakeSnapshotException(%22Unable to load JS script, unable to locate resource stream.%22);%0A            return IOUtils.toString(is
 );%0A     

Update FileUtil.java (#24)