All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.carlspring.commons.io.reloading.FSReloadableInputStreamHandler Maven / Gradle / Ivy

There is a newer version: 1.1
Show newest version
package org.carlspring.commons.io.reloading;

import org.carlspring.commons.io.resource.ResourceCloser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @author mtodorov
 */
public class FSReloadableInputStreamHandler
    implements ReloadableInputStreamHandler
{

    private static final Logger logger = LoggerFactory.getLogger(FSReloadableInputStreamHandler.class);

    private File file;

    private InputStream inputStream;


    public FSReloadableInputStreamHandler(File file)
    {
        this.file = file;
    }

    @Override
    public InputStream getInputStream()
            throws IOException
    {
        if (inputStream == null)
        {
            loadInputStream();
            return inputStream;
        }
        else
        {
            return inputStream;
        }
    }

    @Override
    public void reload()
            throws IOException
    {
        ResourceCloser.close(inputStream, logger);
        loadInputStream();
    }

    private void loadInputStream()
            throws FileNotFoundException
    {
        inputStream = new FileInputStream(file);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy