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

com.databasesandlife.util.InputOutputStreamUtil Maven / Gradle / Ivy

The newest version!
package com.databasesandlife.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Element;

import com.esotericsoftware.yamlbeans.YamlReader;

/**
 * @author This source is copyright Adrian Smith and licensed under the LGPL 3.
 * @see Project on GitHub
 */
public class InputOutputStreamUtil {

    public static class Response {
        public String contentType;
        public byte[] byteArray;
    }

    /** @deprecated use Apache Commons, {@link IOUtils#copy(InputStream, OutputStream)} instead */
    @Deprecated public static void copyBytesFromInputToOutputStream(OutputStream oStream, InputStream iStream)
    throws IOException {
        byte[] buffer = new byte[10000];
        int bytesRead;
        while ((bytesRead = iStream.read(buffer)) >= 0)
           oStream.write(buffer, 0, bytesRead);
    }

    /** @deprecated use Apache Commons, {@link IOUtils#toByteArray(InputStream)} instead */
    @Deprecated public static byte[] readBytesFromInputStream(InputStream iStream)
    throws IOException {
        var oStream = new ByteArrayOutputStream();
        IOUtils.copy(iStream, oStream);
        return oStream.toByteArray();
    }

    /** @deprecated use Apache Commons, {@link IOUtils#toString(Reader)} instead */
    @Deprecated public static String readStringFromReader(Reader iStream)
    throws IOException {
        var oStream = new CharArrayWriter();
        char[] buffer = new char[10000];
        int charsRead;
        while ((charsRead = iStream.read(buffer)) >= 0)
           oStream.write(buffer, 0, charsRead);
        return oStream.toString();
    }

    public static Response readBytesFromUrl(URL url) {
        try {
            var r = new Response();
            var conn = url.openConnection();
            r.contentType = conn.getContentType();
            if (r.contentType == null) throw new RuntimeException(
                "cannot open input stream on URL '"+url.toExternalForm()+"'");
            r.byteArray = IOUtils.toByteArray(conn.getInputStream());
            return r;
        }
        catch (IOException e) { throw new RuntimeException(e); }
    }

    /** @deprecated use Apache Commons, {@link FileUtils#writeStringToFile(File, String, String)} instead */
    @Deprecated public static void writeStringToFileUtf8(File f, String str)
    throws IOException {
        try (var o = new FileOutputStream(f)) {
            var w = new OutputStreamWriter(o, StandardCharsets.UTF_8);
            w.write(str, 0, str.length());
            w.close();
        }
    }

    public static void writeBytesToOutputStream(OutputStream out, byte[] src) throws IOException {
        IOUtils.copy(new ByteArrayInputStream(src), out);
    }

    public static void prettyPrintXml(Writer writer, Element xml) {
        try {
            var systemProperties = System.getProperties();
            systemProperties.remove("javax.xml.transform.TransformerFactory");
            System.setProperties(systemProperties);

            var transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); // indents by 3 spaces
            var source = new DOMSource(xml);
            var result = new StreamResult(writer);
            transformer.transform(source, result);
        }
        catch (TransformerException e) { throw new RuntimeException(e); }
    }

    public static String prettyPrintXml(Element xml) {
        var writer = new StringWriter();
        prettyPrintXml(writer, xml);
        return writer.toString();
    }

    /** If class is "X.java" then parse the "X.yaml" */
    public static Object parseYamlConfig(Class c) {
        var name = c.getName().replaceAll("\\.", "/"); // e.g. "com/mypkg/MyClass"
        try (var stream = c.getClassLoader().getResourceAsStream(name + ".yaml")) {
            if (stream == null) throw new IllegalArgumentException("No '.yaml' file for class '" + c.getName() + "'");
            var yamlCharacterReader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
            var yamlParser = new YamlReader(yamlCharacterReader);
            return yamlParser.read();
        }
        catch (IOException e) { throw new RuntimeException(e); }
    }

    public static void writeXmlToFile(File out, Element xml) {
        try (var writer = new OutputStreamWriter(new FileOutputStream(out), StandardCharsets.UTF_8)) {
            prettyPrintXml(writer, xml);
        }
        catch (IOException e) { throw new RuntimeException(e); }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy