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

brooklyn.util.stream.Streams Maven / Gradle / Ivy

Go to download

Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.util.stream;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import brooklyn.util.exceptions.Exceptions;

import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;
import com.google.common.io.InputSupplier;

public class Streams {

    private static final Logger log = LoggerFactory.getLogger(Streams.class);
    
    public static void closeQuietly(Closeable x) {
        try {
            x.close();
        } catch (Exception e) {
            if (log.isDebugEnabled())
                log.debug("Error closing (ignored) "+x+": "+e);
        }
    }

    public static InputStream fromString(String input) {
        try {
            byte[] bytes = checkNotNull(input, "input").getBytes(Charsets.UTF_8);
            InputSupplier supplier = ByteStreams.newInputStreamSupplier(bytes);
            InputStream stream = supplier.getInput();
            return stream;
        } catch (IOException ioe) {
            if (log.isDebugEnabled()) log.debug("Error creating InputStream from String: " + ioe.getMessage());
            throw Exceptions.propagate(ioe);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy