diff options
-rw-r--r-- | src/delombok/lombok/delombok/Delombok.java | 8 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/UnicodeEscapeWriter.java | 47 |
2 files changed, 53 insertions, 2 deletions
diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index 88e8decb..943136cf 100644 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -327,10 +327,14 @@ public class Delombok { File outFile = new File(base, fileName); outFile.getParentFile().mkdirs(); FileOutputStream out = new FileOutputStream(outFile); - return new OutputStreamWriter(out, charset); + return createUnicodeEscapeWriter(out); } private Writer createStandardOutWriter() { - return new OutputStreamWriter(System.out, charset); + return createUnicodeEscapeWriter(System.out); + } + + private Writer createUnicodeEscapeWriter(OutputStream out) { + return new UnicodeEscapeWriter(new OutputStreamWriter(out, charset), charset); } } diff --git a/src/delombok/lombok/delombok/UnicodeEscapeWriter.java b/src/delombok/lombok/delombok/UnicodeEscapeWriter.java new file mode 100644 index 00000000..afb779dd --- /dev/null +++ b/src/delombok/lombok/delombok/UnicodeEscapeWriter.java @@ -0,0 +1,47 @@ +package lombok.delombok; +import java.io.IOException; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; + +public class UnicodeEscapeWriter extends Writer { + private final Writer writer; + private CharsetEncoder encoder; + + public UnicodeEscapeWriter(Writer writer, Charset charset) { + this.writer = writer; + encoder = charset.newEncoder(); + } + + @Override + public void close() throws IOException { + writer.close(); + } + + @Override + public void flush() throws IOException { + writer.flush(); + } + + @Override + public final void write(char[] cbuf, int off, int len) throws IOException { + int start = off; + int index = start; + int end = off + len; + while (index < end) { + if (!encoder.canEncode(cbuf[index])) { + writer.write(cbuf, start, index - start); + writeUnicodeEscape(cbuf[index]); + start = index + 1; + } + index++; + } + if (start < end) { + writer.write(cbuf, start, end - start); + } + } + + protected void writeUnicodeEscape(char c) throws IOException { + writer.write("\\u" + Integer.toHexString(c)); + } +}
\ No newline at end of file |