use of java.io.StringWriter in project flink by apache.
the class TestLogger method exceptionToString.
private static String exceptionToString(Throwable t) {
if (t == null) {
return "(null)";
}
try {
StringWriter stm = new StringWriter();
PrintWriter wrt = new PrintWriter(stm);
t.printStackTrace(wrt);
wrt.close();
return stm.toString();
} catch (Throwable ignored) {
return t.getClass().getName() + " (error while printing stack trace)";
}
}
use of java.io.StringWriter in project groovy by apache.
the class EncodingGroovyMethods method encodeHex.
/**
* Produces a Writable that writes the hex encoding of the byte[]. Calling
* toString() on this Writable returns the hex encoding as a String. The hex
* encoding includes two characters for each byte and all letters are lower case.
*
* @param data byte array to be encoded
* @return object which will write the hex encoding of the byte array
* @see Integer#toHexString(int)
*/
public static Writable encodeHex(final byte[] data) {
return new Writable() {
public Writer writeTo(Writer out) throws IOException {
for (int i = 0; i < data.length; i++) {
// convert byte into unsigned hex string
String hexString = Integer.toHexString(data[i] & 0xFF);
// add leading zero if the length of the string is one
if (hexString.length() < 2) {
out.write("0");
}
// write hex string to writer
out.write(hexString);
}
return out;
}
public String toString() {
StringWriter buffer = new StringWriter();
try {
writeTo(buffer);
} catch (IOException e) {
throw new StringWriterIOException(e);
}
return buffer.toString();
}
};
}
use of java.io.StringWriter in project groovy by apache.
the class PlatformLineWriterTest method testPlatformLineWriter.
public void testPlatformLineWriter() throws IOException, ClassNotFoundException {
String LS = System.getProperty("line.separator");
Binding binding = new Binding();
binding.setVariable("first", "Tom");
binding.setVariable("last", "Adams");
StringWriter stringWriter = new StringWriter();
Writer platformWriter = new PlatformLineWriter(stringWriter);
GroovyShell shell = new GroovyShell(binding);
platformWriter.write(shell.evaluate("\"$first\\n$last\\n\"").toString());
platformWriter.flush();
assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
stringWriter = new StringWriter();
platformWriter = new PlatformLineWriter(stringWriter);
platformWriter.write(shell.evaluate("\"$first\\r\\n$last\\r\\n\"").toString());
platformWriter.flush();
assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
}
use of java.io.StringWriter in project groovy by apache.
the class JavacJavaCompiler method compile.
public void compile(List<String> files, CompilationUnit cu) {
String[] javacParameters = makeParameters(files, cu.getClassLoader());
StringWriter javacOutput = null;
int javacReturnValue = 0;
try {
Class javac = findJavac(cu);
Method method = null;
try {
method = javac.getMethod("compile", new Class[] { String[].class, PrintWriter.class });
javacOutput = new StringWriter();
PrintWriter writer = new PrintWriter(javacOutput);
Object ret = method.invoke(null, javacParameters, writer);
javacReturnValue = (Integer) ret;
} catch (NoSuchMethodException e) {
}
if (method == null) {
method = javac.getMethod("compile", new Class[] { String[].class });
Object ret = method.invoke(null, new Object[] { javacParameters });
javacReturnValue = (Integer) ret;
}
} catch (InvocationTargetException ite) {
cu.getErrorCollector().addFatalError(new ExceptionMessage((Exception) ite.getCause(), true, cu));
} catch (Exception e) {
cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu));
}
if (javacReturnValue != 0) {
switch(javacReturnValue) {
case 1:
addJavacError("Compile error during compilation with javac.", cu, javacOutput);
break;
case 2:
addJavacError("Invalid commandline usage for javac.", cu, javacOutput);
break;
case 3:
addJavacError("System error during compilation with javac.", cu, javacOutput);
break;
case 4:
addJavacError("Abnormal termination of javac.", cu, javacOutput);
break;
default:
addJavacError("unexpected return value by javac.", cu, javacOutput);
break;
}
} else {
// print warnings if any
System.out.print(javacOutput);
}
}
use of java.io.StringWriter in project groovy by apache.
the class CSTNode method toString.
//---------------------------------------------------------------------------
// STRING CONVERSION
/**
* Formats the node as a <code>String</code> and returns it.
*/
public String toString() {
StringWriter string = new StringWriter();
write(new PrintWriter(string));
string.flush();
return string.toString();
}
Aggregations