diff options
author | Roel Spilker <r.spilker@gmail.com> | 2010-02-14 03:50:19 +0100 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2010-02-14 03:50:19 +0100 |
commit | 2552315058146bc94a38afba5a1a9cd630d4febe (patch) | |
tree | 02ffe60622646ffbcf335550f5442d86b800cd17 /src/delombok | |
parent | 9c07a465c1976140a11c1bee78af485a1c907f63 (diff) | |
download | lombok-2552315058146bc94a38afba5a1a9cd630d4febe.tar.gz lombok-2552315058146bc94a38afba5a1a9cd630d4febe.tar.bz2 lombok-2552315058146bc94a38afba5a1a9cd630d4febe.zip |
Added unicode escape writing to avoid pesky encoding issues.
Diffstat (limited to 'src/delombok')
-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 |