Skip to content

Commit

Permalink
refactor: reduce convert bitmap size
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed Jun 4, 2024
1 parent 8f56c82 commit ec97104
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/org/omegat/util/TmxEscapingWriterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand All @@ -39,27 +38,25 @@
public class TmxEscapingWriterFactory implements EscapingWriterFactory {

@Override
public Writer createEscapingWriterFor(final Writer writer, final String s)
throws UnsupportedEncodingException {
public Writer createEscapingWriterFor(@NotNull final Writer writer, final String s) {
return new EscapeWriter(writer);
}

@Override
public Writer createEscapingWriterFor(final OutputStream outputStream, final String s)
throws UnsupportedEncodingException {
public Writer createEscapingWriterFor(@NotNull final OutputStream outputStream, final String s) {
return new EscapeWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
}

public static class EscapeWriter extends Writer {

// Copy from woodstox:com.ctc.wstx.sw.BufferingXmlWriter
private static final int[] QUOTABLE_TEXT_CHARS;
private static final byte[] QUOTABLE_TEXT_CHARS;
private static final int HIGH_ENC = 0xFFFE;

static {
int[] q = new int[4096];
Arrays.fill(q, 0, 32, 1);
Arrays.fill(q, 127, 160, 1);
byte[] q = new byte[256];
Arrays.fill(q, 0, 32, (byte) 1);
Arrays.fill(q, 127, 160, (byte) 1);
q['\t'] = 0;
q['\n'] = 0;
q['<'] = 1;
Expand All @@ -86,6 +83,7 @@ public EscapeWriter(@NotNull Writer delegate) {
* @param len
* Number of characters to write
* @throws IOException
* when underlying writer object raises.
*/
@Override
public void write(@NotNull final char[] cbuf, final int off, final int len) throws IOException {
Expand All @@ -96,7 +94,7 @@ public void write(@NotNull final char[] cbuf, final int off, final int len) thro
String ent = null;
for (; offset < end; offset++) {
int c = cbuf[offset];
if (c < 4096 && QUOTABLE_TEXT_CHARS[c] != 0) {
if (c < 256 && QUOTABLE_TEXT_CHARS[c] != 0) {
if (c == '<') {
ent = "&lt;";
break;
Expand Down

0 comments on commit ec97104

Please sign in to comment.