Search in sources :

Example 46 with StringWriter

use of java.io.StringWriter in project groovy by apache.

the class StringEscapeUtils method unescapeJava.

/**
     * Unescapes any Java literals found in the <code>String</code>.
     * For example, it will turn a sequence of <code>'\'</code> and
     * <code>'n'</code> into a newline character, unless the <code>'\'</code>
     * is preceded by another <code>'\'</code>.
     * 
     * @param str  the <code>String</code> to unescape, may be null
     * @return a new unescaped <code>String</code>, <code>null</code> if null string input
     */
public static String unescapeJava(String str) {
    if (str == null) {
        return null;
    }
    try {
        StringWriter writer = new StringWriter(str.length());
        unescapeJava(writer, str);
        return writer.toString();
    } catch (IOException ioe) {
        // this should never ever happen while writing to a StringWriter
        throw new RuntimeException(ioe);
    }
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException)

Example 47 with StringWriter

use of java.io.StringWriter in project hadoop by apache.

the class MetricsSystemImpl method currentConfig.

@Override
public synchronized String currentConfig() {
    PropertiesConfiguration saver = new PropertiesConfiguration();
    StringWriter writer = new StringWriter();
    saver.copy(config);
    try {
        saver.write(writer);
    } catch (Exception e) {
        throw new MetricsConfigException("Error stringify config", e);
    }
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) MetricsException(org.apache.hadoop.metrics2.MetricsException)

Example 48 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class JsonMapper method getOptimizerPropertiesJson.

public static String getOptimizerPropertiesJson(JsonFactory jsonFactory, PlanNode node) {
    try {
        final StringWriter writer = new StringWriter(256);
        final JsonGenerator gen = jsonFactory.createGenerator(writer);
        final OptimizerNode optNode = node.getOptimizerNode();
        gen.writeStartObject();
        // global properties
        if (node.getGlobalProperties() != null) {
            GlobalProperties gp = node.getGlobalProperties();
            gen.writeArrayFieldStart("global_properties");
            addProperty(gen, "Partitioning", gp.getPartitioning().name());
            if (gp.getPartitioningFields() != null) {
                addProperty(gen, "Partitioned on", gp.getPartitioningFields().toString());
            }
            if (gp.getPartitioningOrdering() != null) {
                addProperty(gen, "Partitioning Order", gp.getPartitioningOrdering().toString());
            } else {
                addProperty(gen, "Partitioning Order", "(none)");
            }
            if (optNode.getUniqueFields() == null || optNode.getUniqueFields().size() == 0) {
                addProperty(gen, "Uniqueness", "not unique");
            } else {
                addProperty(gen, "Uniqueness", optNode.getUniqueFields().toString());
            }
            gen.writeEndArray();
        }
        // local properties
        if (node.getLocalProperties() != null) {
            LocalProperties lp = node.getLocalProperties();
            gen.writeArrayFieldStart("local_properties");
            if (lp.getOrdering() != null) {
                addProperty(gen, "Order", lp.getOrdering().toString());
            } else {
                addProperty(gen, "Order", "(none)");
            }
            if (lp.getGroupedFields() != null && lp.getGroupedFields().size() > 0) {
                addProperty(gen, "Grouped on", lp.getGroupedFields().toString());
            } else {
                addProperty(gen, "Grouping", "not grouped");
            }
            if (optNode.getUniqueFields() == null || optNode.getUniqueFields().size() == 0) {
                addProperty(gen, "Uniqueness", "not unique");
            } else {
                addProperty(gen, "Uniqueness", optNode.getUniqueFields().toString());
            }
            gen.writeEndArray();
        }
        // output size estimates
        {
            gen.writeArrayFieldStart("estimates");
            addProperty(gen, "Est. Output Size", optNode.getEstimatedOutputSize() == -1 ? "(unknown)" : formatNumber(optNode.getEstimatedOutputSize(), "B"));
            addProperty(gen, "Est. Cardinality", optNode.getEstimatedNumRecords() == -1 ? "(unknown)" : formatNumber(optNode.getEstimatedNumRecords()));
            gen.writeEndArray();
        }
        // output node cost
        if (node.getNodeCosts() != null) {
            gen.writeArrayFieldStart("costs");
            addProperty(gen, "Network", node.getNodeCosts().getNetworkCost() == -1 ? "(unknown)" : formatNumber(node.getNodeCosts().getNetworkCost(), "B"));
            addProperty(gen, "Disk I/O", node.getNodeCosts().getDiskCost() == -1 ? "(unknown)" : formatNumber(node.getNodeCosts().getDiskCost(), "B"));
            addProperty(gen, "CPU", node.getNodeCosts().getCpuCost() == -1 ? "(unknown)" : formatNumber(node.getNodeCosts().getCpuCost(), ""));
            addProperty(gen, "Cumulative Network", node.getCumulativeCosts().getNetworkCost() == -1 ? "(unknown)" : formatNumber(node.getCumulativeCosts().getNetworkCost(), "B"));
            addProperty(gen, "Cumulative Disk I/O", node.getCumulativeCosts().getDiskCost() == -1 ? "(unknown)" : formatNumber(node.getCumulativeCosts().getDiskCost(), "B"));
            addProperty(gen, "Cumulative CPU", node.getCumulativeCosts().getCpuCost() == -1 ? "(unknown)" : formatNumber(node.getCumulativeCosts().getCpuCost(), ""));
            gen.writeEndArray();
        }
        // compiler hints
        if (optNode.getOperator().getCompilerHints() != null) {
            CompilerHints hints = optNode.getOperator().getCompilerHints();
            CompilerHints defaults = new CompilerHints();
            String size = hints.getOutputSize() == defaults.getOutputSize() ? "(none)" : String.valueOf(hints.getOutputSize());
            String card = hints.getOutputCardinality() == defaults.getOutputCardinality() ? "(none)" : String.valueOf(hints.getOutputCardinality());
            String width = hints.getAvgOutputRecordSize() == defaults.getAvgOutputRecordSize() ? "(none)" : String.valueOf(hints.getAvgOutputRecordSize());
            String filter = hints.getFilterFactor() == defaults.getFilterFactor() ? "(none)" : String.valueOf(hints.getFilterFactor());
            gen.writeArrayFieldStart("compiler_hints");
            addProperty(gen, "Output Size (bytes)", size);
            addProperty(gen, "Output Cardinality", card);
            addProperty(gen, "Avg. Output Record Size (bytes)", width);
            addProperty(gen, "Filter Factor", filter);
            gen.writeEndArray();
        }
        gen.writeEndObject();
        gen.close();
        return writer.toString();
    } catch (Exception e) {
        return "{}";
    }
}
Also used : StringWriter(java.io.StringWriter) OptimizerNode(org.apache.flink.optimizer.dag.OptimizerNode) GlobalProperties(org.apache.flink.optimizer.dataproperties.GlobalProperties) CompilerHints(org.apache.flink.api.common.operators.CompilerHints) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) LocalProperties(org.apache.flink.optimizer.dataproperties.LocalProperties) IOException(java.io.IOException)

Example 49 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class PlanJSONDumpGenerator method getOptimizerPlanAsJSON.

public String getOptimizerPlanAsJSON(OptimizedPlan plan) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    dumpOptimizerPlanAsJSON(plan, pw);
    pw.close();
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 50 with StringWriter

use of java.io.StringWriter in project groovy by apache.

the class InvokerHelper method append.

/**
     * Appends an object to an Appendable using Groovy's default representation for the object.
     */
public static void append(Appendable out, Object object) throws IOException {
    if (object instanceof String) {
        out.append((String) object);
    } else if (object instanceof Object[]) {
        out.append(toArrayString((Object[]) object));
    } else if (object instanceof Map) {
        out.append(toMapString((Map) object));
    } else if (object instanceof Collection) {
        out.append(toListString((Collection) object));
    } else if (object instanceof Writable) {
        Writable writable = (Writable) object;
        StringWriter stringWriter = new StringWriter();
        writable.writeTo(stringWriter);
        out.append(stringWriter.toString());
    } else if (object instanceof InputStream || object instanceof Reader) {
        // Copy stream to stream
        Reader reader;
        if (object instanceof InputStream) {
            reader = new InputStreamReader((InputStream) object);
        } else {
            reader = (Reader) object;
        }
        char[] chars = new char[8192];
        int i;
        while ((i = reader.read(chars)) != -1) {
            for (int j = 0; j < i; j++) {
                out.append(chars[j]);
            }
        }
        reader.close();
    } else {
        out.append(toString(object));
    }
}
Also used : StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Collection(java.util.Collection) Writable(groovy.lang.Writable) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) GroovyObject(groovy.lang.GroovyObject) GString(groovy.lang.GString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SpreadMap(groovy.lang.SpreadMap)

Aggregations

StringWriter (java.io.StringWriter)3175 PrintWriter (java.io.PrintWriter)1057 Test (org.junit.Test)612 IOException (java.io.IOException)516 StringReader (java.io.StringReader)232 Writer (java.io.Writer)211 StreamResult (javax.xml.transform.stream.StreamResult)207 File (java.io.File)194 InputStreamReader (java.io.InputStreamReader)140 HashMap (java.util.HashMap)136 Transformer (javax.xml.transform.Transformer)125 InputStream (java.io.InputStream)119 Map (java.util.Map)116 ArrayList (java.util.ArrayList)106 DOMSource (javax.xml.transform.dom.DOMSource)99 BufferedReader (java.io.BufferedReader)96 ByteArrayInputStream (java.io.ByteArrayInputStream)84 Reader (java.io.Reader)77 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)75 HttpServletResponse (javax.servlet.http.HttpServletResponse)73