Example usage for com.google.common.io ByteSource openBufferedStream

List of usage examples for com.google.common.io ByteSource openBufferedStream

Introduction

In this page you can find the example usage for com.google.common.io ByteSource openBufferedStream.

Prototype

public InputStream openBufferedStream() throws IOException 

Source Link

Document

Opens a new buffered InputStream for reading from this source.

Usage

From source file:com.zenika.doclipser.api.ReadPropertiesWithGuava.java

public final static void main(final String[] args) {
    final URL url = Resources.getResource(Constants.PROPERTY_FILE_NAME);
    final ByteSource byteSource = Resources.asByteSource(url);
    final Properties properties = new Properties();
    InputStream inputStream = null;
    try {/*from  w w w  . j  ava  2  s.  c om*/
        inputStream = byteSource.openBufferedStream();
        properties.load(inputStream);
        properties.list(System.out);
    } catch (final IOException ioException) {
        ioException.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (final IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}

From source file:com.github.steveash.jg2p.util.ReadWrite.java

private static <T> T readFromSource(ByteSource bs) throws IOException, ClassNotFoundException {
    try (ObjectInputStream ois = new ObjectInputStream(bs.openBufferedStream())) {
        return (T) ois.readObject();
    }/*from w  w w  . j  a  v a2  s  . c o m*/
}

From source file:com.zenika.doclipser.api.DockerClientFactory.java

private static String getDockerClientClassName() {
    final URL url = Resources.getResource(Constants.PROPERTY_FILE_NAME);
    final ByteSource byteSource = Resources.asByteSource(url);
    final Properties properties = new Properties();
    InputStream inputStream = null;
    try {/*from   w  w w. j a  v a 2s.  co  m*/
        inputStream = byteSource.openBufferedStream();
        properties.load(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (final IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
    return properties.getProperty(Constants.PROPERTY_DOCKER_CLIENT_LIB);
}

From source file:com.feedeo.shopify.web.client.rest.ShopifyOAuth2RestClient.java

private static Properties getProperties() {
    final Properties properties = new Properties();

    final URL url = Resources.getResource("jopify.properties");
    final ByteSource byteSource = Resources.asByteSource(url);

    InputStream inputStream = null;
    try {//  w ww  .j  a  va  2  s  .  c  o m
        inputStream = byteSource.openBufferedStream();
        properties.load(inputStream);
    } catch (final IOException ioException) {
        ioException.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (final IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    return properties;
}

From source file:com.epam.reportportal.utils.MimeTypeDetector.java

public static String detect(ByteSource source, String resourceName) throws IOException {

    final Metadata metadata = new Metadata();
    if (!isNullOrEmpty(resourceName)) {
        metadata.set(Metadata.RESOURCE_NAME_KEY, resourceName);
    }//from www .  java 2  s .co  m
    return detect(TikaInputStream.get(source.openBufferedStream()), metadata);

}

From source file:com.skcraft.launcher.persistence.Persistence.java

/**
 * Read an object from a byte source, without binding it.
 *
 * @param source byte source/*from  www.jav  a 2s . c  om*/
 * @param cls the class
 * @param returnNull true to return null if the object could not be loaded
 * @param <V> the type of class
 * @return an object
 */
public static <V> V read(ByteSource source, Class<V> cls, boolean returnNull) {
    V object;
    Closer closer = Closer.create();

    try {
        object = mapper.readValue(closer.register(source.openBufferedStream()), cls);
    } catch (IOException e) {
        if (!(e instanceof FileNotFoundException)) {
            log.log(Level.INFO, "Failed to load" + cls.getCanonicalName(), e);
        }

        if (returnNull) {
            return null;
        }

        try {
            object = cls.newInstance();
        } catch (InstantiationException e1) {
            throw new RuntimeException("Failed to construct object with no-arg constructor", e1);
        } catch (IllegalAccessException e1) {
            throw new RuntimeException("Failed to construct object with no-arg constructor", e1);
        }
    } finally {
        try {
            closer.close();
        } catch (IOException e) {
        }
    }

    return object;
}

From source file:com.opengamma.strata.collect.io.XmlFile.java

/**
 * Parses the specified source as an XML file to an in-memory DOM-like structure.
 * <p>//from   w ww  .  j  a  v  a  2  s .c o  m
 * This parses the specified byte source expecting an XML file format.
 * The resulting instance can be queried for the root element.
 * <p>
 * This supports capturing attribute references, such as an id/href pair.
 * Wherever the parser finds an attribute with the specified name, the element is added
 * to the internal map, accessible by calling {@link #getReferences()}.
 * <p>
 * For example, if one part of the XML has {@code <foo id="fooId">}, the references map will
 * contain an entry mapping "fooId" to the parsed element {@code <foo>}.
 * 
 * @param source  the XML source data
 * @param refAttrName  the attribute name that should be parsed as a reference
 * @return the parsed file
 * @throws UncheckedIOException if an IO exception occurs
 * @throws IllegalArgumentException if the file cannot be parsed
 */
public static XmlFile of(ByteSource source, String refAttrName) {
    ArgChecker.notNull(source, "source");
    return Unchecked.wrap(() -> {
        try (InputStream in = source.openBufferedStream()) {
            XMLStreamReader xmlReader = xmlInputFactory().createXMLStreamReader(in);
            try {
                HashMap<String, XmlElement> refs = new HashMap<>();
                XmlElement root = parse(xmlReader, refAttrName, refs);
                return new XmlFile(root, refs);
            } finally {
                xmlReader.close();
            }
        }
    });
}

From source file:de.nx42.maps4cim.header.HeaderParser.java

/**
 * Returns the first index after the end of the header
 * @param source the stream to find the end of the header in
 * @return the end of the header / start of the body
 * @throws IOException if the stream can't be read
 *//*from w ww  .j a v a 2s.c o  m*/
public static int findEndOfHeader(ByteSource source) throws IOException {
    InputStream is = null;
    try {
        is = source.openBufferedStream();
        // find "GameState+SerializableTerrainData"
        int lastBlockStart = KMPMatch.indexOf(is, CustomHeader.formatHeaderString(CustomHeader.staticString06));
        // header length: how many multiples of 4096 (+256)
        int headerMulti = lastBlockStart / 4096;
        return headerMulti * 4096 + 256;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:org.trnltk.tokenizer.data.TokenizerTrainingData.java

public static TokenizerTrainingData createFromYamlByteSource(ByteSource byteSource) {
    TypeDescription dataDescription = new TypeDescription(TokenizerTrainingData.class);
    dataDescription.putListPropertyType("entries", TokenizerTrainingEntry.class);

    Constructor constructor = new Constructor(TokenizerTrainingData.class);
    constructor.addTypeDescription(dataDescription);
    Yaml yaml = new Yaml(constructor);

    try {/* w  w w.java  2  s . c o m*/
        InputStream str = byteSource.openBufferedStream();
        return (TokenizerTrainingData) yaml.load(str);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:de.nx42.maps4cim.header.HeaderParser.java

/**
 * Takes any ByteSource representing a full CiM 2 map (or at least the full
 * header of the map), parses the header and returns a CustomHeader-object
 * representing the relevant contents of this header.
 * @param source the map to parse/*w w  w. j a  v a 2s  .co  m*/
 * @return the CustomHeader-object containing the data of this header
 * @throws ParseException if there is an error parsing the header
 * @throws IOException if there is an error accessing the array-contents
 */
public static CustomHeader parse(ByteSource source) throws ParseException, IOException {
    InputStream is = null;
    try {
        is = source.openBufferedStream();
        byte[] relevant = getRelevantPart(is);
        return execute(relevant);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}