use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class CompileTaskSupport method handleException.
protected void handleException(final Exception e) throws BuildException {
assert e != null;
Writer writer = new StringBuilderWriter();
new ErrorReporter(e, false).write(new PrintWriter(writer));
String message = writer.toString();
if (failOnError) {
throw new BuildException(message, e, getLocation());
} else {
log.error(message);
}
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class StringGroovyMethods method denormalize.
/**
* Return a CharSequence with lines (separated by LF, CR/LF, or CR)
* terminated by the platform specific line separator.
*
* @param self a CharSequence object
* @return the denormalized toString() of this CharSequence
*
* @since 1.8.2
*/
public static String denormalize(final CharSequence self) {
// TODO: Put this lineSeparator property somewhere everyone can use it.
if (lineSeparator == null) {
final Writer sw = new StringBuilderWriter(2);
// the security manager rigamarole to deal with the possible exception
try (final BufferedWriter bw = new BufferedWriter(sw)) {
bw.newLine();
bw.flush();
lineSeparator = sw.toString();
} catch (IOException ioe) {
// This shouldn't happen, but this is the same default used by
// BufferedWriter on a security exception.
lineSeparator = "\n";
}
}
final int len = self.length();
if (len < 1) {
return self.toString();
}
final StringBuilder sb = new StringBuilder((110 * len) / 100);
int i = 0;
// GROOVY-7873: GString calls toString() on each invocation of CharSequence methods such
// as charAt which is very expensive for large GStrings.
CharSequence cs = (self instanceof GString) ? self.toString() : self;
while (i < len) {
final char ch = cs.charAt(i++);
switch(ch) {
case '\r':
sb.append(lineSeparator);
// Eat the following LF if any.
if ((i < len) && (cs.charAt(i) == '\n')) {
++i;
}
break;
case '\n':
sb.append(lineSeparator);
break;
default:
sb.append(ch);
break;
}
}
return sb.toString();
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class FormatHelper 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;
Writer stringWriter = new StringBuilderWriter();
writable.writeTo(stringWriter);
out.append(stringWriter.toString());
} else if (object instanceof InputStream || object instanceof Reader) {
// Copy stream to stream
try (Reader reader = object instanceof InputStream ? new InputStreamReader((InputStream) object) : (Reader) object) {
char[] chars = new char[8192];
for (int i; (i = reader.read(chars)) != -1; ) {
for (int j = 0; j < i; j++) {
out.append(chars[j]);
}
}
}
} else {
out.append(toString(object));
}
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class IOGroovyMethods method filterLine.
/**
* Filter the lines from this Reader, and return a Writable which can be
* used to stream the filtered lines to a destination. The closure should
* return <code>true</code> if the line should be passed to the writer.
*
* @param reader this reader
* @param closure a closure used for filtering
* @return a Writable which will use the closure to filter each line
* from the reader when the Writable#writeTo(Writer) is called.
* @since 1.0
*/
public static Writable filterLine(Reader reader, @ClosureParams(value = SimpleType.class, options = "java.lang.String") final Closure closure) {
final BufferedReader br = new BufferedReader(reader);
return new Writable() {
@Override
public Writer writeTo(Writer out) throws IOException {
BufferedWriter bw = new BufferedWriter(out);
String line;
BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
while ((line = br.readLine()) != null) {
if (bcw.call(line)) {
bw.write(line);
bw.newLine();
}
}
bw.flush();
return out;
}
@Override
public String toString() {
Writer buffer = new StringBuilderWriter();
try {
writeTo(buffer);
} catch (IOException e) {
throw new StringWriterIOException(e);
}
return buffer.toString();
}
};
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class StringEscapeUtils method escapeJavaStyleString.
/**
* Worker method for the {@link #escapeJavaScript(String)} method.
*
* @param str String to escape values in, may be null
* @param escapeSingleQuotes escapes single quotes if <code>true</code>
* @param escapeForwardSlash TODO
* @return the escaped string
*/
private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes, boolean escapeForwardSlash) {
if (str == null) {
return null;
}
try {
Writer writer = new StringBuilderWriter(str.length() * 2);
escapeJavaStyleString(writer, str, escapeSingleQuotes, escapeForwardSlash);
return writer.toString();
} catch (IOException ioe) {
// this should never ever happen while writing to a StringBuilderWriter
throw new RuntimeException(ioe);
}
}
Aggregations