Java Code Examples for java.io.StringWriter#append()

The following examples show how to use java.io.StringWriter#append() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RpcCallExceptionDecoder.java    From ja-micro with Apache License 2.0 6 votes vote down vote up
static String exceptionToString(Throwable ex) {
    if (ex == null) {
        return "null";
    }
    StringWriter str = new StringWriter();
    str.append("Exception: ").append(ex.getClass().getSimpleName());
    str.append(" Message: ").append(ex.getMessage());
    str.append(" Stacktrace: ");
    Throwable cause = ex.getCause();
    if (cause != null) {
        str.append("\nCause: ").append(exceptionToString(cause));
    }
    PrintWriter writer = new PrintWriter(str);
    try {
        ex.printStackTrace(writer);
        return str.getBuffer().toString();
    } finally {
        try {
            str.close();
            writer.close();
        } catch (IOException e) {}
    }
}
 
Example 2
Source File: DgeHeaderCodecTest.java    From Drop-seq with MIT License 6 votes vote down vote up
/**
 * Confirm that position of reader is correct after looking for a header and finding none.
 */
@Test
public void testNoHeaderWithTrailingContents() throws IOException {
    final DgeHeader header = new DgeHeader();
    // Clear default values so the header looks like a codec not finding a header
    header.setVersion(null);
    header.setExpressionFormat(DgeHeader.ExpressionFormat.unknown);

    final StringWriter writer = new StringWriter();
    final String contentAfterHeader = "Goodbye, cruel world!";
    writer.append(contentAfterHeader);
    final BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
    final DgeHeader decodedHeader = new DgeHeaderCodec().decode(reader, "test input");
    Assert.assertEquals(decodedHeader, header);
    final String actualContentAfterHeader = reader.readLine();
    Assert.assertEquals(actualContentAfterHeader, contentAfterHeader);
}
 
Example 3
Source File: FunctionGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static String createLocalNew(final Class<? extends Invoke> type, final Function fxn) {
    final String identifier = makeInstanceName(fxn);
    StringWriter out = new StringWriter();

    out.append(String.format("%3$s[] argCoders = new %3$s[%1$d + %2$s.length];\n", fxn.args.size(), VARARGS_NAME, Coder.class.getCanonicalName()));

    for(int i = 0; i < fxn.args.size(); i++)
        out.append(String.format("argCoders[%1$d] = %2$s;\n", i, fxn.args.get(0).type.getJType().getCoderDescriptor().getCoderInstanceName()));

    if(fxn.variadic){
        out.append(String.format("for(int i = %1$d; i < (%1$d + %2$s.length); i++)\n", fxn.args.size(), VARARGS_NAME));
        out.append(String.format("\targCoders[i] = %1$s.getCoderAtRuntime(%2$s[i - %3$s]);\n", Coder.class.getCanonicalName(), VARARGS_NAME, fxn.args.size()));
    }

    out.append("final " + type.getCanonicalName() + " " + identifier + " = new " + type.getCanonicalName() + "(" + firstArg(fxn) + ", \"" + fxn.name + "\", "
            + fxn.returnValue.type.getJType().getCoderDescriptor().getCoderInstanceName() + ", argCoders);");

    return out.toString();
}
 
Example 4
Source File: FunctionGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static String createLocalNew(final Class<? extends Invoke> type, final Function fxn) {
    final String identifier = makeInstanceName(fxn);
    StringWriter out = new StringWriter();

    out.append(String.format("%3$s[] argCoders = new %3$s[%1$d + %2$s.length];\n", fxn.args.size(), VARARGS_NAME, Coder.class.getCanonicalName()));

    for(int i = 0; i < fxn.args.size(); i++)
        out.append(String.format("argCoders[%1$d] = %2$s;\n", i, fxn.args.get(0).type.getJType().getCoderDescriptor().getCoderInstanceName()));

    if(fxn.variadic){
        out.append(String.format("for(int i = %1$d; i < (%1$d + %2$s.length); i++)\n", fxn.args.size(), VARARGS_NAME));
        out.append(String.format("\targCoders[i] = %1$s.getCoderAtRuntime(%2$s[i - %3$s]);\n", Coder.class.getCanonicalName(), VARARGS_NAME, fxn.args.size()));
    }

    out.append("final " + type.getCanonicalName() + " " + identifier + " = new " + type.getCanonicalName() + "(" + firstArg(fxn) + ", \"" + fxn.name + "\", "
            + fxn.returnValue.type.getJType().getCoderDescriptor().getCoderInstanceName() + ", argCoders);");

    return out.toString();
}
 
Example 5
Source File: DgeHeaderCodecTest.java    From Drop-seq with MIT License 6 votes vote down vote up
/**
 * Confirm that position of reader is immediately after header, after header is decoded.
 */
@Test(dataProvider = "numberOfLibrariesDataProvider")
public void testHeaderWithTrailingContentsInputStream(final Integer numberOfLibraries) throws IOException {
    final DgeHeader header = makeDgeHeader(numberOfLibraries);

    final StringWriter writer = new StringWriter();
    final DgeHeaderCodec codec = new DgeHeaderCodec();
    codec.encode(writer, header);
    final String contentAfterHeader = "Goodbye, cruel world!";
    writer.append(contentAfterHeader);
    System.out.print(writer.toString());
    final BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(writer.toString().getBytes()));
    final DgeHeader decodedHeader = codec.decode(inputStream, "test input");
    Assert.assertEquals(decodedHeader, header);
    final String actualContentAfterHeader = new BufferedReader(new InputStreamReader(inputStream)).readLine();
    Assert.assertEquals(actualContentAfterHeader, contentAfterHeader);
}
 
Example 6
Source File: StructTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
String print(Struct st){
    StringWriter sw = new StringWriter();
    st.raw.position(0);
    sw.append(st.getClass().getSimpleName() + ":" + st.raw.limit() + " @ " + Long.toHexString(st.raw.bufferPtr) + " : ");
    for(int i = 0; i < st.raw.limit(); i++){
        sw.append(byteToHexString(st.raw.get()) + " ");
        if((i+1) % 4 == 0)
            sw.append(" ");
    }
    System.out.println(sw.toString().trim());
    return sw.toString().trim();
}
 
Example 7
Source File: Record.java    From bitsy with Apache License 2.0 5 votes vote down vote up
public static void generateVertexLine(StringWriter sw, ObjectMapper mapper, VertexBean vBean) throws JsonGenerationException, JsonMappingException, IOException {
    sw.getBuffer().setLength(0);

    sw.append('V'); // Record type
    sw.append('=');

    mapper.writeValue(sw, vBean);

    sw.append('#');

    int hashCode = hashCode(sw.toString());
    sw.append(toHex(hashCode));
    sw.append('\n');
}
 
Example 8
Source File: Coder.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static String objCEncoding(final Coder[] elementCoders) {
    StringWriter str = new StringWriter();
    str.append("{?=");
    for(Coder c : elementCoders)
        str.append(c.getObjCEncoding());
    str.append("}");
    return str.toString();
}
 
Example 9
Source File: JavaLang.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override public String toString(){
    StringWriter out = new StringWriter();
    if(jdoc.size() > 0){
        out.append("\t/**\n");
        out.append("\t * " + Fp.join("\n\t * ", jdoc));
        out.append("\t */\n");
    }
    out.append("\t" + Fp.join(" ", attrs) + " " + Fp.join(" ", mods) + " " + type + " " + name + "(" + Fp.join(", ", args) + "){\n");
    out.append("\t\t" + Fp.join("\n\t\t", body) + "\n");
    out.append("\t}\n");
    return out.toString();
}
 
Example 10
Source File: Coder.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static String objCEncoding(final Coder[] elementCoders) {
    StringWriter str = new StringWriter();
    str.append("{?=");
    for(Coder c : elementCoders)
        str.append(c.getObjCEncoding());
    str.append("}");
    return str.toString();
}
 
Example 11
Source File: ListDisplayTagBase.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
protected void renderPanelHeading(JspWriter out) throws IOException {

        StringWriter headFilterContent = new StringWriter();
        StringWriter titleContent = new StringWriter();
        StringWriter headAddons = new StringWriter();

        renderTitle(titleContent);
        if (getPageList().hasFilter()) {
            headFilterContent.append("<div class=\"spacewalk-list-filter\">");
            renderFilterBox(headFilterContent);
            headFilterContent.append("</div>");
        }
        renderHeadExtraAddons(headAddons);

        int headContentLength = headFilterContent.getBuffer().length() +
                                titleContent.getBuffer().length() +
                                headAddons.getBuffer().length();

        if (headContentLength > 0) {
            out.println("<div class=\"panel-heading\">");
            out.println(titleContent.toString());
            out.println("<div class=\"spacewalk-list-head-addons\">");
            out.println(headFilterContent.toString());
            out.println("<div class=\"spacewalk-list-head-addons-extra\">");
            out.println(headAddons.toString());
            out.println("</div>");
            out.println("</div>");
            out.println("</div>");
        }
    }
 
Example 12
Source File: TestScript.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
public void publishHtmlReport(StringWriter writer)
{
    final int failedNumber = getTestCaseNumber(NumberType.FAILED);
    writer.append("\n\n<h1>" + scriptContent + "</h1>\n");
    writer.append("<p><b>Name</b>: " + scriptName + "</p>\n");
    writer.append("<p><b>Number of test cases</b>: " + getTestCaseNumber(NumberType.TOTAL) + "</p>\n");
    writer.append("<p><b>Reading duration</b>: " + readingDuration + "ms</p>\n");
    writer.append("<table border = \"1\" cellspacing=\"0\" cellpadding=\"5\">\n");
    String title = "    <tr>";
    for (String t : TestCase.PARAMETERS)
    {
        title += "<td><b>" + t + "</b></td>";
    }
    title += "</tr>\n";
    writer.append(title);
    for (TestCase tc : testCases)
    {
        tc.publishHtmlReport(writer);
    }
    writer.append("</table>\n");
    String status = "<p><b>Status</b>: ";
    if (failedNumber == 0)
    {
        status += "<font color=\"green\">PASSED</font>";
    }
    else
    {
        status += "<font color=\"red\">FAILED</font>";
    }
    status += " (passed: " + getTestCaseNumber(NumberType.PASSED) + ", failed: " + failedNumber + ")</p>\n";
    writer.append(status);
}
 
Example 13
Source File: StructTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
String print(Struct st){
    StringWriter sw = new StringWriter();
    st.raw.position(0);
    sw.append(st.getClass().getSimpleName() + ":" + st.raw.limit() + " @ " + Long.toHexString(st.raw.bufferPtr) + " : ");
    for(int i = 0; i < st.raw.limit(); i++){
        sw.append(byteToHexString(st.raw.get()) + " ");
        if((i+1) % 4 == 0)
            sw.append(" ");
    }
    System.out.println(sw.toString().trim());
    return sw.toString().trim();
}
 
Example 14
Source File: JavaLang.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override public String toString(){
    StringWriter out = new StringWriter();
    if(jdoc.size() > 0){
        out.append("\t/**\n");
        out.append("\t * " + Fp.join("\n\t * ", jdoc));
        out.append("\t */\n");
    }
    out.append("\t" + Fp.join(" ", attrs) + " " + Fp.join(" ", mods) + " " + type + " " + name + "(" + Fp.join(", ", args) + "){\n");
    out.append("\t\t" + Fp.join("\n\t\t", body) + "\n");
    out.append("\t}\n");
    return out.toString();
}
 
Example 15
Source File: StructTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private String byteToHexString(Byte b){
    StringWriter sw = new StringWriter();
    sw.append(halfByteToHex(b & 0xF));
    sw.append(halfByteToHex((b & 0xF0) >> 4));
    return sw.toString();
}
 
Example 16
Source File: Subclassing.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
boolean registerUserClass(final Class<? extends ID> clazz, final Class<? extends NSClass> clazzClazz) {
    final String nativeClassName = clazz.getSimpleName();
    // Is it already registered?
    if(0 != NSClass.getNativeClassByName(nativeClassName))
        return false;

    if(clazz.isAnonymousClass())
        throw new RuntimeException("JObjC cannot register anonymous classes.");

    // Verify superclass
    long superClass = NSClass.getNativeClassByName(clazz.getSuperclass().getSimpleName());
    if(0 == superClass)
        throw new RuntimeException(clazz.getSuperclass() + ", the superclass of " + clazz + ", must be a registered class.");

    runtime.registerPackage(clazz.getPackage().getName());

    // Create class
    long classPtr = Subclassing.allocateClassPair(superClass, nativeClassName);
    if(classPtr == 0) throw new RuntimeException("objc_allocateClassPair returned 0.");

    // Add ivar to hold jobject
    boolean addedI = Subclassing.addIVarForJObj(classPtr);
    if(!addedI) throw new RuntimeException("class_addIvar returned false.");

    // Verify constructor
    try {
        clazz.getConstructor(ID.CTOR_ARGS);
    } catch (Exception e) {
        throw new RuntimeException("Could not access required constructor: " + ID.CTOR_ARGS, e);
    }

    // Patch alloc to create corresponding jobject on invoke
    patchAlloc(classPtr);

    // Add methods
    Set<String> takenSelNames = new HashSet<String>();
    for(Method method : clazz.getDeclaredMethods()){
        // No overloading
        String selName = SEL.selectorName(method.getName(), method.getParameterTypes().length > 0);
        if(takenSelNames.contains(selName))
            throw new RuntimeException("Obj-C does not allow method overloading. The Objective-C selector '"
                    + selName + "' appears more than once in class " + clazz.getCanonicalName() + " / " + nativeClassName + ".");

        method.setAccessible(true);

        // Divine CIF
        Coder returnCoder = Coder.getCoderAtRuntimeForType(method.getReturnType());
        Class[] paramTypes = method.getParameterTypes();
        Coder[] argCoders = new Coder[paramTypes.length];
        for(int i = 0; i < paramTypes.length; i++)
            argCoders[i] = Coder.getCoderAtRuntimeForType(paramTypes[i]);

        CIF cif = new MsgSend(runtime, selName, returnCoder, argCoders).funCall.cif;

        // .. and objc encoding
        StringWriter encType = new StringWriter();
        encType.append(returnCoder.getObjCEncoding());
        encType.append("@:");
        for(int i = 0; i < argCoders.length; i++)
            encType.append(argCoders[i].getObjCEncoding());

        // Add it!
        boolean addedM = Subclassing.addMethod(classPtr, selName, method, cif, cif.cif.bufferPtr, encType.toString());
        if(!addedM) throw new RuntimeException("Failed to add method.");
        takenSelNames.add(selName);
    }

    // Seal it
    Subclassing.registerClassPair(classPtr);
    registeredUserSubclasses.add(classPtr);

    return true;
}
 
Example 17
Source File: StringStream.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public String readWhile(String s) {
    StringWriter sw = new StringWriter();
    while(s.indexOf(peek()) != -1)
        sw.append(read());
    return sw.toString();
}
 
Example 18
Source File: StructTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private String byteToHexString(Byte b){
    StringWriter sw = new StringWriter();
    sw.append(halfByteToHex(b & 0xF));
    sw.append(halfByteToHex((b & 0xF0) >> 4));
    return sw.toString();
}
 
Example 19
Source File: StructTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private String byteToHexString(Byte b){
    StringWriter sw = new StringWriter();
    sw.append(halfByteToHex(b & 0xF));
    sw.append(halfByteToHex((b & 0xF0) >> 4));
    return sw.toString();
}
 
Example 20
Source File: StringStream.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public String readWhile(String s) {
    StringWriter sw = new StringWriter();
    while(s.indexOf(peek()) != -1)
        sw.append(read());
    return sw.toString();
}