use of groovy.lang.Writable in project groovy by apache.
the class XmlUtil method asString.
private static String asString(GPathResult node) {
// little bit of hackery to avoid Groovy dependency in this file
try {
Object builder = ((Class) Class.forName("groovy.xml.StreamingMarkupBuilder")).newInstance();
InvokerHelper.setProperty(builder, "encoding", "UTF-8");
Writable w = (Writable) InvokerHelper.invokeMethod(builder, "bindNode", node);
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + w.toString();
} catch (Exception e) {
return "Couldn't convert node to string because: " + e.getMessage();
}
}
use of groovy.lang.Writable 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 groovy.lang.Writable in project groovy by apache.
the class InvokerHelper method write.
/**
* Writes an object to a Writer using Groovy's default representation for the object.
*/
public static void write(Writer out, Object object) throws IOException {
if (object instanceof String) {
out.write((String) object);
} else if (object instanceof Object[]) {
out.write(toArrayString((Object[]) object));
} else if (object instanceof Map) {
out.write(toMapString((Map) object));
} else if (object instanceof Collection) {
out.write(toListString((Collection) object));
} else if (object instanceof Writable) {
Writable writable = (Writable) object;
writable.writeTo(out);
} 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) {
out.write(chars, 0, i);
}
reader.close();
} else {
out.write(toString(object));
}
}
use of groovy.lang.Writable 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));
}
}
use of groovy.lang.Writable in project ratpack by ratpack.
the class MarkupTemplateRenderer method render.
@Override
public void render(Context ctx, MarkupTemplate template) throws Exception {
String contentType = template.getContentType();
contentType = contentType == null ? ctx.get(MimeTypes.class).getContentType(template.getName()) : contentType;
try {
Template compiledTemplate = engine.createTemplateByPath(template.getName());
Writable boundTemplate = compiledTemplate.make(template.getModel());
ByteBuf byteBuf = byteBufAllocator.directBuffer();
try {
OutputStream outputStream = new ByteBufOutputStream(byteBuf);
Writer writer = new OutputStreamWriter(outputStream, CharsetUtil.encoder(StandardCharsets.UTF_8));
boundTemplate.writeTo(writer);
} catch (Exception e) {
byteBuf.release();
throw e;
}
ctx.getResponse().send(contentType, byteBuf);
} catch (IOException e) {
ctx.error(e);
}
}
Aggregations