From fbb1b5fb1e97f1f9c5d9c9df8de58abc64e79a93 Mon Sep 17 00:00:00 2001 From: doni Date: Mon, 2 Nov 2020 16:51:20 +0700 Subject: [PATCH 1/7] repalce output stream with writeroutputstream --- transfuse-core/pom.xml | 4 ++-- .../org/androidtransfuse/gen/FilerResourceWriter.java | 11 +++-------- .../androidtransfuse/gen/FilerSourceCodeWriter.java | 10 +++------- transfuse/pom.xml | 4 ++-- .../androidtransfuse/gen/FilerResourceWriterTest.java | 6 +----- .../gen/FilerSourceCodeWriterTest.java | 6 +----- 6 files changed, 12 insertions(+), 29 deletions(-) diff --git a/transfuse-core/pom.xml b/transfuse-core/pom.xml index 47280993..0e2ab068 100644 --- a/transfuse-core/pom.xml +++ b/transfuse-core/pom.xml @@ -35,8 +35,8 @@ commons-io commons-io - 2.4 - test + 2.5 + org.mockito diff --git a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java index dd5a857c..b208ae4d 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java @@ -17,6 +17,7 @@ import com.sun.codemodel.CodeWriter; import com.sun.codemodel.JPackage; +import org.apache.commons.io.output.WriterOutputStream; import javax.annotation.processing.Filer; import javax.inject.Inject; @@ -30,7 +31,6 @@ public class FilerResourceWriter extends CodeWriter { private final Filer filer; - private final Collection openStreams = new HashSet(); @Inject public FilerResourceWriter(Filer filer) { @@ -41,18 +41,13 @@ public FilerResourceWriter(Filer filer) { public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { FileObject resource = filer.createResource(StandardLocation.SOURCE_OUTPUT, pkg.name(), fileName); - OutputStream os = resource.openOutputStream(); - openStreams.add(os); - + OutputStream os = new WriterOutputStream(resource.openWriter()); return os; } @Override public void close() throws IOException { - for (OutputStream openStream : openStreams) { - openStream.flush(); - openStream.close(); - } + } } \ No newline at end of file diff --git a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java index 77a82f5c..441a2511 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java @@ -26,6 +26,7 @@ import java.io.OutputStream; import java.util.Collection; import java.util.HashSet; +import org.apache.commons.io.output.WriterOutputStream; /** * Adapter class to allow codemodel to write its output source and source files to the Java Annotation Processor Filer @@ -36,7 +37,6 @@ public class FilerSourceCodeWriter extends CodeWriter { private final Filer filer; private final Originating originating; - private final Collection openStreams = new HashSet(); @Inject public FilerSourceCodeWriter(Filer filer, Originating originating) { @@ -50,9 +50,8 @@ public OutputStream openBinary(JPackage jPackage, String fileName) throws IOExce String qualified = toQualifiedClassName(jPackage, fileName); JavaFileObject sourceFile = filer.createSourceFile(qualified, originating.getOriginatingElements(qualified)); - OutputStream os = sourceFile.openOutputStream(); - openStreams.add(os); + OutputStream os = new WriterOutputStream(sourceFile.openWriter()); return os; } @@ -62,9 +61,6 @@ private String toQualifiedClassName(JPackage pkg, String fileName) { @Override public void close() throws IOException { - for (OutputStream openStream : openStreams) { - openStream.flush(); - openStream.close(); - } + } } diff --git a/transfuse/pom.xml b/transfuse/pom.xml index fdc67d8c..36f4046e 100644 --- a/transfuse/pom.xml +++ b/transfuse/pom.xml @@ -53,8 +53,8 @@ commons-io commons-io - 2.4 - test + 2.5 + org.mockito diff --git a/transfuse/src/test/java/org/androidtransfuse/gen/FilerResourceWriterTest.java b/transfuse/src/test/java/org/androidtransfuse/gen/FilerResourceWriterTest.java index cf4d9fff..f955f79c 100644 --- a/transfuse/src/test/java/org/androidtransfuse/gen/FilerResourceWriterTest.java +++ b/transfuse/src/test/java/org/androidtransfuse/gen/FilerResourceWriterTest.java @@ -16,6 +16,7 @@ package org.androidtransfuse.gen; import com.sun.codemodel.JCodeModel; +import org.apache.commons.io.output.WriterOutputStream; import org.junit.Before; import org.junit.Test; @@ -58,10 +59,5 @@ public void testCreateResource() throws IOException { when(mockFiler.createResource(StandardLocation.SOURCE_OUTPUT, TEST_PACKAGE, TEST_FILENAME)).thenReturn(mockFile); when(mockFile.openOutputStream()).thenReturn(mockOutputStream); - assertEquals(mockOutputStream, resourceWriter.openBinary(codeModel._package(TEST_PACKAGE), TEST_FILENAME)); - - resourceWriter.close(); - verify(mockOutputStream).flush(); - verify(mockOutputStream).close(); } } diff --git a/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java b/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java index f15c9c3e..ffd0656f 100644 --- a/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java +++ b/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java @@ -56,11 +56,7 @@ public void testCreateSourceFile() throws IOException { when(mockFiler.createSourceFile(TEST_PACKAGE + "." + TEST_CLASS)).thenReturn(mockFile); when(mockFile.openOutputStream()).thenReturn(mockOutputStream); + OutputStream os = codeWriter.openBinary(codeModel._package(TEST_PACKAGE), TEST_CLASS); - assertEquals(mockOutputStream, codeWriter.openBinary(codeModel._package(TEST_PACKAGE), TEST_CLASS)); - - codeWriter.close(); - verify(mockOutputStream).flush(); - verify(mockOutputStream).close(); } } From 5fdd78b3534112d590c3547d0b75253b309e5835 Mon Sep 17 00:00:00 2001 From: doni Date: Mon, 2 Nov 2020 16:54:52 +0700 Subject: [PATCH 2/7] remove unused variable --- .../org/androidtransfuse/gen/FilerSourceCodeWriterTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java b/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java index ffd0656f..1755b838 100644 --- a/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java +++ b/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java @@ -53,10 +53,7 @@ public void setUp() throws Exception { @Test public void testCreateSourceFile() throws IOException { - when(mockFiler.createSourceFile(TEST_PACKAGE + "." + TEST_CLASS)).thenReturn(mockFile); when(mockFile.openOutputStream()).thenReturn(mockOutputStream); - OutputStream os = codeWriter.openBinary(codeModel._package(TEST_PACKAGE), TEST_CLASS); - } } From 59b671c6bed5e8d4b564013641c3a6134b2ff65c Mon Sep 17 00:00:00 2001 From: doni Date: Thu, 5 Nov 2020 16:27:35 +0700 Subject: [PATCH 3/7] Copy WriterOutputStream locally and add charset --- transfuse-core/pom.xml | 1 + .../gen/FilerResourceWriter.java | 13 +- .../gen/FilerSourceCodeWriter.java | 13 +- .../apache/commons/WriterOutputStream.java | 146 ++++++++++++++++++ 4 files changed, 164 insertions(+), 9 deletions(-) create mode 100644 transfuse-core/src/main/java/org/androidtransfuse/util/apache/commons/WriterOutputStream.java diff --git a/transfuse-core/pom.xml b/transfuse-core/pom.xml index 0e2ab068..10d90b11 100644 --- a/transfuse-core/pom.xml +++ b/transfuse-core/pom.xml @@ -36,6 +36,7 @@ commons-io commons-io 2.5 + test diff --git a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java index b208ae4d..9fd9dff7 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java @@ -17,7 +17,8 @@ import com.sun.codemodel.CodeWriter; import com.sun.codemodel.JPackage; -import org.apache.commons.io.output.WriterOutputStream; +import org.androidtransfuse.util.apache.commons.WriterOutputStream; + import javax.annotation.processing.Filer; import javax.inject.Inject; @@ -31,6 +32,7 @@ public class FilerResourceWriter extends CodeWriter { private final Filer filer; + private final Collection openStreams = new HashSet(); @Inject public FilerResourceWriter(Filer filer) { @@ -40,14 +42,17 @@ public FilerResourceWriter(Filer filer) { @Override public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { FileObject resource = filer.createResource(StandardLocation.SOURCE_OUTPUT, pkg.name(), fileName); - - OutputStream os = new WriterOutputStream(resource.openWriter()); + WriterOutputStream os = new WriterOutputStream(resource.openWriter(),"UTF-8"); + openStreams.add(os); return os; } @Override public void close() throws IOException { - + for (OutputStream openStream : openStreams) { +// openStream.flush(); + openStream.close(); + } } } \ No newline at end of file diff --git a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java index 441a2511..87daec56 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java @@ -18,6 +18,7 @@ import com.sun.codemodel.CodeWriter; import com.sun.codemodel.JPackage; import org.androidtransfuse.adapter.PackageClass; +import org.androidtransfuse.util.apache.commons.WriterOutputStream; import javax.annotation.processing.Filer; import javax.inject.Inject; @@ -26,7 +27,6 @@ import java.io.OutputStream; import java.util.Collection; import java.util.HashSet; -import org.apache.commons.io.output.WriterOutputStream; /** * Adapter class to allow codemodel to write its output source and source files to the Java Annotation Processor Filer @@ -37,6 +37,7 @@ public class FilerSourceCodeWriter extends CodeWriter { private final Filer filer; private final Originating originating; + private final Collection openStreams = new HashSet(); @Inject public FilerSourceCodeWriter(Filer filer, Originating originating) { @@ -49,9 +50,8 @@ public OutputStream openBinary(JPackage jPackage, String fileName) throws IOExce //generate a source file based on package and fileName String qualified = toQualifiedClassName(jPackage, fileName); JavaFileObject sourceFile = filer.createSourceFile(qualified, originating.getOriginatingElements(qualified)); - - - OutputStream os = new WriterOutputStream(sourceFile.openWriter()); + OutputStream os = new WriterOutputStream(sourceFile.openWriter(), "UTF-8"); + openStreams.add(os); return os; } @@ -61,6 +61,9 @@ private String toQualifiedClassName(JPackage pkg, String fileName) { @Override public void close() throws IOException { - + for (OutputStream openStream : openStreams) { +// openStream.flush(); + openStream.close(); + } } } diff --git a/transfuse-core/src/main/java/org/androidtransfuse/util/apache/commons/WriterOutputStream.java b/transfuse-core/src/main/java/org/androidtransfuse/util/apache/commons/WriterOutputStream.java new file mode 100644 index 00000000..8dae8181 --- /dev/null +++ b/transfuse-core/src/main/java/org/androidtransfuse/util/apache/commons/WriterOutputStream.java @@ -0,0 +1,146 @@ +package org.androidtransfuse.util.apache.commons; + + +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CoderResult; +import java.nio.charset.CodingErrorAction; + + +public class WriterOutputStream extends OutputStream { + private static final int DEFAULT_BUFFER_SIZE = 1024; + private final Writer writer; + private final CharsetDecoder decoder; + private final boolean writeImmediately; + private final ByteBuffer decoderIn; + private final CharBuffer decoderOut; + + public WriterOutputStream(Writer writer, CharsetDecoder decoder) { + this(writer, (CharsetDecoder)decoder, 1024, false); + } + + public WriterOutputStream(Writer writer, CharsetDecoder decoder, int bufferSize, boolean writeImmediately) { + this.decoderIn = ByteBuffer.allocate(128); + checkIbmJdkWithBrokenUTF16(decoder.charset()); + this.writer = writer; + this.decoder = decoder; + this.writeImmediately = writeImmediately; + this.decoderOut = CharBuffer.allocate(bufferSize); + } + + public WriterOutputStream(Writer writer, Charset charset, int bufferSize, boolean writeImmediately) { + this(writer, charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).replaceWith("?"), bufferSize, writeImmediately); + } + + public WriterOutputStream(Writer writer, Charset charset) { + this(writer, (Charset)charset, 1024, false); + } + + public WriterOutputStream(Writer writer, String charsetName, int bufferSize, boolean writeImmediately) { + this(writer, Charset.forName(charsetName), bufferSize, writeImmediately); + } + + public WriterOutputStream(Writer writer, String charsetName) { + this(writer, (String)charsetName, 1024, false); + } + + /** @deprecated */ + @Deprecated + public WriterOutputStream(Writer writer) { + this(writer, (Charset)Charset.defaultCharset(), 1024, false); + } + + public void write(byte[] b, int off, int len) throws IOException { + while(len > 0) { + int c = Math.min(len, this.decoderIn.remaining()); + this.decoderIn.put(b, off, c); + this.processInput(false); + len -= c; + off += c; + } + + if (this.writeImmediately) { + this.flushOutput(); + } + + } + + public void write(byte[] b) throws IOException { + this.write(b, 0, b.length); + } + + public void write(int b) throws IOException { + this.write(new byte[]{(byte)b}, 0, 1); + } + + public void flush() throws IOException { + this.flushOutput(); + this.writer.flush(); + } + + public void close() throws IOException { + this.processInput(true); + this.flushOutput(); + this.writer.close(); + } + + private void processInput(boolean endOfInput) throws IOException { + this.decoderIn.flip(); + + while(true) { + CoderResult coderResult = this.decoder.decode(this.decoderIn, this.decoderOut, endOfInput); + if (!coderResult.isOverflow()) { + if (coderResult.isUnderflow()) { + this.decoderIn.compact(); + return; + } else { + throw new IOException("Unexpected coder result"); + } + } + + this.flushOutput(); + } + } + + private void flushOutput() throws IOException { + if (this.decoderOut.position() > 0) { + this.writer.write(this.decoderOut.array(), 0, this.decoderOut.position()); + this.decoderOut.rewind(); + } + + } + + private static void checkIbmJdkWithBrokenUTF16(Charset charset) { + if ("UTF-16".equals(charset.name())) { + String TEST_STRING_2 = "vés"; + byte[] bytes = "vés".getBytes(charset); + CharsetDecoder charsetDecoder2 = charset.newDecoder(); + ByteBuffer bb2 = ByteBuffer.allocate(16); + CharBuffer cb2 = CharBuffer.allocate("vés".length()); + int len = bytes.length; + + for(int i = 0; i < len; ++i) { + bb2.put(bytes[i]); + bb2.flip(); + + try { + charsetDecoder2.decode(bb2, cb2, i == len - 1); + } catch (IllegalArgumentException var9) { + throw new UnsupportedOperationException("UTF-16 requested when runninng on an IBM JDK with broken UTF-16 support. Please find a JDK that supports UTF-16 if you intend to use UF-16 with WriterOutputStream"); + } + + bb2.compact(); + } + + cb2.rewind(); + if (!"vés".equals(cb2.toString())) { + throw new UnsupportedOperationException("UTF-16 requested when runninng on an IBM JDK with broken UTF-16 support. Please find a JDK that supports UTF-16 if you intend to use UF-16 with WriterOutputStream"); + } + } + } +} From 8355efdac97875fa45be238929459d017832a523 Mon Sep 17 00:00:00 2001 From: doni Date: Thu, 5 Nov 2020 16:36:57 +0700 Subject: [PATCH 4/7] add test scope on tranfuse/pom.xml --- transfuse/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/transfuse/pom.xml b/transfuse/pom.xml index 36f4046e..9748a49d 100644 --- a/transfuse/pom.xml +++ b/transfuse/pom.xml @@ -54,6 +54,7 @@ commons-io commons-io 2.5 + test From 2e2f68dccf0fb156d2b96bb4eba302d84b330a2a Mon Sep 17 00:00:00 2001 From: doni Date: Thu, 5 Nov 2020 23:18:50 +0700 Subject: [PATCH 5/7] Change to StandartCharsets, remove comments, update common-io on tranfuse-support, remove unused code on writeroutputstream --- .../gen/FilerResourceWriter.java | 4 +- .../gen/FilerSourceCodeWriter.java | 4 +- .../apache/commons/WriterOutputStream.java | 74 ++----------------- transfuse-support/pom.xml | 2 +- 4 files changed, 13 insertions(+), 71 deletions(-) diff --git a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java index 9fd9dff7..7e9e077d 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java @@ -26,6 +26,7 @@ import javax.tools.StandardLocation; import java.io.IOException; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.HashSet; @@ -42,7 +43,7 @@ public FilerResourceWriter(Filer filer) { @Override public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { FileObject resource = filer.createResource(StandardLocation.SOURCE_OUTPUT, pkg.name(), fileName); - WriterOutputStream os = new WriterOutputStream(resource.openWriter(),"UTF-8"); + WriterOutputStream os = new WriterOutputStream(resource.openWriter(), StandardCharsets.UTF_8); openStreams.add(os); return os; } @@ -51,7 +52,6 @@ public OutputStream openBinary(JPackage pkg, String fileName) throws IOException @Override public void close() throws IOException { for (OutputStream openStream : openStreams) { -// openStream.flush(); openStream.close(); } } diff --git a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java index 87daec56..aa23e144 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java @@ -25,6 +25,7 @@ import javax.tools.JavaFileObject; import java.io.IOException; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.HashSet; @@ -50,7 +51,7 @@ public OutputStream openBinary(JPackage jPackage, String fileName) throws IOExce //generate a source file based on package and fileName String qualified = toQualifiedClassName(jPackage, fileName); JavaFileObject sourceFile = filer.createSourceFile(qualified, originating.getOriginatingElements(qualified)); - OutputStream os = new WriterOutputStream(sourceFile.openWriter(), "UTF-8"); + OutputStream os = new WriterOutputStream(sourceFile.openWriter(), StandardCharsets.UTF_8); openStreams.add(os); return os; } @@ -62,7 +63,6 @@ private String toQualifiedClassName(JPackage pkg, String fileName) { @Override public void close() throws IOException { for (OutputStream openStream : openStreams) { -// openStream.flush(); openStream.close(); } } diff --git a/transfuse-core/src/main/java/org/androidtransfuse/util/apache/commons/WriterOutputStream.java b/transfuse-core/src/main/java/org/androidtransfuse/util/apache/commons/WriterOutputStream.java index 8dae8181..cf58b9ec 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/util/apache/commons/WriterOutputStream.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/util/apache/commons/WriterOutputStream.java @@ -13,50 +13,23 @@ public class WriterOutputStream extends OutputStream { - private static final int DEFAULT_BUFFER_SIZE = 1024; + private final Writer writer; private final CharsetDecoder decoder; private final boolean writeImmediately; private final ByteBuffer decoderIn; private final CharBuffer decoderOut; - public WriterOutputStream(Writer writer, CharsetDecoder decoder) { - this(writer, (CharsetDecoder)decoder, 1024, false); - } - - public WriterOutputStream(Writer writer, CharsetDecoder decoder, int bufferSize, boolean writeImmediately) { + public WriterOutputStream(Writer writer, Charset charset) { this.decoderIn = ByteBuffer.allocate(128); - checkIbmJdkWithBrokenUTF16(decoder.charset()); this.writer = writer; - this.decoder = decoder; - this.writeImmediately = writeImmediately; - this.decoderOut = CharBuffer.allocate(bufferSize); - } - - public WriterOutputStream(Writer writer, Charset charset, int bufferSize, boolean writeImmediately) { - this(writer, charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).replaceWith("?"), bufferSize, writeImmediately); - } - - public WriterOutputStream(Writer writer, Charset charset) { - this(writer, (Charset)charset, 1024, false); - } - - public WriterOutputStream(Writer writer, String charsetName, int bufferSize, boolean writeImmediately) { - this(writer, Charset.forName(charsetName), bufferSize, writeImmediately); - } - - public WriterOutputStream(Writer writer, String charsetName) { - this(writer, (String)charsetName, 1024, false); - } - - /** @deprecated */ - @Deprecated - public WriterOutputStream(Writer writer) { - this(writer, (Charset)Charset.defaultCharset(), 1024, false); + this.decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).replaceWith("?"); + this.writeImmediately = false; + this.decoderOut = CharBuffer.allocate(1024); } public void write(byte[] b, int off, int len) throws IOException { - while(len > 0) { + while (len > 0) { int c = Math.min(len, this.decoderIn.remaining()); this.decoderIn.put(b, off, c); this.processInput(false); @@ -67,7 +40,6 @@ public void write(byte[] b, int off, int len) throws IOException { if (this.writeImmediately) { this.flushOutput(); } - } public void write(byte[] b) throws IOException { @@ -75,7 +47,7 @@ public void write(byte[] b) throws IOException { } public void write(int b) throws IOException { - this.write(new byte[]{(byte)b}, 0, 1); + this.write(new byte[]{(byte) b}, 0, 1); } public void flush() throws IOException { @@ -92,7 +64,7 @@ public void close() throws IOException { private void processInput(boolean endOfInput) throws IOException { this.decoderIn.flip(); - while(true) { + while (true) { CoderResult coderResult = this.decoder.decode(this.decoderIn, this.decoderOut, endOfInput); if (!coderResult.isOverflow()) { if (coderResult.isUnderflow()) { @@ -112,35 +84,5 @@ private void flushOutput() throws IOException { this.writer.write(this.decoderOut.array(), 0, this.decoderOut.position()); this.decoderOut.rewind(); } - - } - - private static void checkIbmJdkWithBrokenUTF16(Charset charset) { - if ("UTF-16".equals(charset.name())) { - String TEST_STRING_2 = "vés"; - byte[] bytes = "vés".getBytes(charset); - CharsetDecoder charsetDecoder2 = charset.newDecoder(); - ByteBuffer bb2 = ByteBuffer.allocate(16); - CharBuffer cb2 = CharBuffer.allocate("vés".length()); - int len = bytes.length; - - for(int i = 0; i < len; ++i) { - bb2.put(bytes[i]); - bb2.flip(); - - try { - charsetDecoder2.decode(bb2, cb2, i == len - 1); - } catch (IllegalArgumentException var9) { - throw new UnsupportedOperationException("UTF-16 requested when runninng on an IBM JDK with broken UTF-16 support. Please find a JDK that supports UTF-16 if you intend to use UF-16 with WriterOutputStream"); - } - - bb2.compact(); - } - - cb2.rewind(); - if (!"vés".equals(cb2.toString())) { - throw new UnsupportedOperationException("UTF-16 requested when runninng on an IBM JDK with broken UTF-16 support. Please find a JDK that supports UTF-16 if you intend to use UF-16 with WriterOutputStream"); - } - } } } diff --git a/transfuse-support/pom.xml b/transfuse-support/pom.xml index a95f4e3f..d77bfe77 100644 --- a/transfuse-support/pom.xml +++ b/transfuse-support/pom.xml @@ -79,7 +79,7 @@ commons-io commons-io - 2.4 + 2.5 test From b7b97483141d36f63bea0b4c25c9fbc18b848065 Mon Sep 17 00:00:00 2001 From: doni Date: Fri, 6 Nov 2020 18:27:13 +0700 Subject: [PATCH 6/7] fix test and move writer to gen folder --- .../gen/FilerResourceWriter.java | 8 +++++--- .../gen/FilerSourceCodeWriter.java | 17 +++++++++++------ .../commons => gen}/WriterOutputStream.java | 2 +- .../gen/FilerResourceWriterTest.java | 15 ++++++++++----- .../gen/FilerSourceCodeWriterTest.java | 19 +++++++++++++------ 5 files changed, 40 insertions(+), 21 deletions(-) rename transfuse-core/src/main/java/org/androidtransfuse/{util/apache/commons => gen}/WriterOutputStream.java (98%) diff --git a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java index 7e9e077d..cc4c07c3 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java @@ -17,7 +17,6 @@ import com.sun.codemodel.CodeWriter; import com.sun.codemodel.JPackage; -import org.androidtransfuse.util.apache.commons.WriterOutputStream; import javax.annotation.processing.Filer; @@ -26,6 +25,7 @@ import javax.tools.StandardLocation; import java.io.IOException; import java.io.OutputStream; +import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.HashSet; @@ -43,11 +43,13 @@ public FilerResourceWriter(Filer filer) { @Override public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { FileObject resource = filer.createResource(StandardLocation.SOURCE_OUTPUT, pkg.name(), fileName); - WriterOutputStream os = new WriterOutputStream(resource.openWriter(), StandardCharsets.UTF_8); + OutputStream os = getWriterOutputStream(resource.openWriter()); openStreams.add(os); return os; } - + public OutputStream getWriterOutputStream(Writer writer) { + return new WriterOutputStream(writer, StandardCharsets.UTF_8); + } @Override public void close() throws IOException { diff --git a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java index aa23e144..87051e70 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java @@ -1,12 +1,12 @@ /** * Copyright 2011-2015 John Ericksen - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -18,13 +18,13 @@ import com.sun.codemodel.CodeWriter; import com.sun.codemodel.JPackage; import org.androidtransfuse.adapter.PackageClass; -import org.androidtransfuse.util.apache.commons.WriterOutputStream; import javax.annotation.processing.Filer; import javax.inject.Inject; import javax.tools.JavaFileObject; import java.io.IOException; import java.io.OutputStream; +import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.HashSet; @@ -51,11 +51,16 @@ public OutputStream openBinary(JPackage jPackage, String fileName) throws IOExce //generate a source file based on package and fileName String qualified = toQualifiedClassName(jPackage, fileName); JavaFileObject sourceFile = filer.createSourceFile(qualified, originating.getOriginatingElements(qualified)); - OutputStream os = new WriterOutputStream(sourceFile.openWriter(), StandardCharsets.UTF_8); + OutputStream os = getWriterOutputStream(sourceFile.openWriter()); openStreams.add(os); + return os; } + public OutputStream getWriterOutputStream(Writer writer) { + return new WriterOutputStream(writer, StandardCharsets.UTF_8); + } + private String toQualifiedClassName(JPackage pkg, String fileName) { return new PackageClass(pkg.name(), fileName).getFullyQualifiedName(); } diff --git a/transfuse-core/src/main/java/org/androidtransfuse/util/apache/commons/WriterOutputStream.java b/transfuse-core/src/main/java/org/androidtransfuse/gen/WriterOutputStream.java similarity index 98% rename from transfuse-core/src/main/java/org/androidtransfuse/util/apache/commons/WriterOutputStream.java rename to transfuse-core/src/main/java/org/androidtransfuse/gen/WriterOutputStream.java index cf58b9ec..c4f6b179 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/util/apache/commons/WriterOutputStream.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/gen/WriterOutputStream.java @@ -1,4 +1,4 @@ -package org.androidtransfuse.util.apache.commons; +package org.androidtransfuse.gen; import java.io.IOException; diff --git a/transfuse/src/test/java/org/androidtransfuse/gen/FilerResourceWriterTest.java b/transfuse/src/test/java/org/androidtransfuse/gen/FilerResourceWriterTest.java index f955f79c..f3aaa579 100644 --- a/transfuse/src/test/java/org/androidtransfuse/gen/FilerResourceWriterTest.java +++ b/transfuse/src/test/java/org/androidtransfuse/gen/FilerResourceWriterTest.java @@ -16,7 +16,6 @@ package org.androidtransfuse.gen; import com.sun.codemodel.JCodeModel; -import org.apache.commons.io.output.WriterOutputStream; import org.junit.Before; import org.junit.Test; @@ -25,6 +24,7 @@ import javax.tools.StandardLocation; import java.io.IOException; import java.io.OutputStream; +import java.io.Writer; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.*; @@ -32,6 +32,7 @@ /** * @author John Ericksen */ + public class FilerResourceWriterTest { private static final String TEST_PACKAGE = "org.test"; @@ -41,6 +42,7 @@ public class FilerResourceWriterTest { private Filer mockFiler; private FileObject mockFile; private OutputStream mockOutputStream; + private Writer mockWriter; private JCodeModel codeModel; @Before @@ -48,16 +50,19 @@ public void setUp() throws Exception { mockFiler = mock(Filer.class); mockFile = mock(FileObject.class); mockOutputStream = mock(OutputStream.class); + mockWriter = mock(Writer.class); - resourceWriter = new FilerResourceWriter(mockFiler); + resourceWriter = spy(new FilerResourceWriter(mockFiler)); codeModel = new JCodeModel(); } @Test public void testCreateResource() throws IOException { - when(mockFiler.createResource(StandardLocation.SOURCE_OUTPUT, TEST_PACKAGE, TEST_FILENAME)).thenReturn(mockFile); - when(mockFile.openOutputStream()).thenReturn(mockOutputStream); - + doReturn(mockWriter).when(mockFile).openWriter(); + doReturn(mockOutputStream).when(resourceWriter).getWriterOutputStream(mockWriter); + assertEquals(mockOutputStream, resourceWriter.openBinary(codeModel._package(TEST_PACKAGE), TEST_FILENAME)); + resourceWriter.close(); + verify(mockOutputStream).close(); } } diff --git a/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java b/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java index 1755b838..8df54040 100644 --- a/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java +++ b/transfuse/src/test/java/org/androidtransfuse/gen/FilerSourceCodeWriterTest.java @@ -19,17 +19,19 @@ import org.junit.Before; import org.junit.Test; + import javax.annotation.processing.Filer; import javax.tools.JavaFileObject; -import java.io.IOException; import java.io.OutputStream; - +import java.io.Writer; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.*; + /** * @author John Ericksen */ + public class FilerSourceCodeWriterTest { private static final String TEST_PACKAGE = "org.test"; @@ -39,21 +41,26 @@ public class FilerSourceCodeWriterTest { private Filer mockFiler; private JavaFileObject mockFile; private OutputStream mockOutputStream; + private Writer mockWriter; private JCodeModel codeModel; @Before public void setUp() throws Exception { + mockWriter = mock(Writer.class); mockFiler = mock(Filer.class); mockFile = mock(JavaFileObject.class); mockOutputStream = mock(OutputStream.class); - - codeWriter = new FilerSourceCodeWriter(mockFiler, new Originating()); + codeWriter = spy(new FilerSourceCodeWriter(mockFiler, new Originating())); codeModel = new JCodeModel(); } @Test - public void testCreateSourceFile() throws IOException { + public void testCreateSourceFile() throws Exception { when(mockFiler.createSourceFile(TEST_PACKAGE + "." + TEST_CLASS)).thenReturn(mockFile); - when(mockFile.openOutputStream()).thenReturn(mockOutputStream); + doReturn(mockWriter).when(mockFile).openWriter(); + doReturn(mockOutputStream).when(codeWriter).getWriterOutputStream(mockWriter); + assertEquals(mockOutputStream, codeWriter.openBinary(codeModel._package(TEST_PACKAGE), TEST_CLASS)); + codeWriter.close(); + verify(mockOutputStream).close(); } } From 4e6e31039bfd8aaeb867e5fe7c42ad154fe0a1ba Mon Sep 17 00:00:00 2001 From: doni Date: Sat, 7 Nov 2020 10:10:36 +0700 Subject: [PATCH 7/7] change standardCharsets to charset.forname --- .../androidtransfuse/gen/FilerResourceWriter.java | 5 ++--- .../androidtransfuse/gen/FilerSourceCodeWriter.java | 12 ++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java index cc4c07c3..3c623cc2 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerResourceWriter.java @@ -18,7 +18,6 @@ import com.sun.codemodel.CodeWriter; import com.sun.codemodel.JPackage; - import javax.annotation.processing.Filer; import javax.inject.Inject; import javax.tools.FileObject; @@ -26,7 +25,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.Writer; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; import java.util.Collection; import java.util.HashSet; @@ -48,7 +47,7 @@ public OutputStream openBinary(JPackage pkg, String fileName) throws IOException return os; } public OutputStream getWriterOutputStream(Writer writer) { - return new WriterOutputStream(writer, StandardCharsets.UTF_8); + return new WriterOutputStream(writer, Charset.forName("UTF-8")); } @Override diff --git a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java index 87051e70..169ad591 100644 --- a/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java +++ b/transfuse-core/src/main/java/org/androidtransfuse/gen/FilerSourceCodeWriter.java @@ -1,12 +1,12 @@ /** * Copyright 2011-2015 John Ericksen - *

+ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

+ * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -25,7 +25,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.Writer; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; import java.util.Collection; import java.util.HashSet; @@ -58,7 +58,7 @@ public OutputStream openBinary(JPackage jPackage, String fileName) throws IOExce } public OutputStream getWriterOutputStream(Writer writer) { - return new WriterOutputStream(writer, StandardCharsets.UTF_8); + return new WriterOutputStream(writer, Charset.forName("UTF-8")); } private String toQualifiedClassName(JPackage pkg, String fileName) {