mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 18:18:48 +00:00
Added unicode escape writing to avoid pesky encoding issues.
This commit is contained in:
1 parent
9c07a465c1
commit
2552315058
2 files changed
+53
-2
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user