Skip to content

Commit

Permalink
Merge pull request #234 from doniwinata0309/fix/writefile
Browse files Browse the repository at this point in the history
Replace outputstream with writeroutputstream
  • Loading branch information
johncarl81 authored Jul 17, 2021
2 parents a8ee2e9 + 4e6e310 commit 217e00f
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 27 deletions.
3 changes: 2 additions & 1 deletion transfuse-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<version>2.5</version>
<scope>test</scope>

</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import javax.tools.StandardLocation;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashSet;

Expand All @@ -40,18 +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 = resource.openOutputStream();
OutputStream os = getWriterOutputStream(resource.openWriter());
openStreams.add(os);

return os;
}

public OutputStream getWriterOutputStream(Writer writer) {
return new WriterOutputStream(writer, Charset.forName("UTF-8"));
}

@Override
public void close() throws IOException {
for (OutputStream openStream : openStreams) {
openStream.flush();
openStream.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashSet;

Expand All @@ -49,21 +51,23 @@ 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 = sourceFile.openOutputStream();
OutputStream os = getWriterOutputStream(sourceFile.openWriter());
openStreams.add(os);

return os;
}

public OutputStream getWriterOutputStream(Writer writer) {
return new WriterOutputStream(writer, Charset.forName("UTF-8"));
}

private String toQualifiedClassName(JPackage pkg, String fileName) {
return new PackageClass(pkg.name(), fileName).getFullyQualifiedName();
}

@Override
public void close() throws IOException {
for (OutputStream openStream : openStreams) {
openStream.flush();
openStream.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.androidtransfuse.gen;


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 final Writer writer;
private final CharsetDecoder decoder;
private final boolean writeImmediately;
private final ByteBuffer decoderIn;
private final CharBuffer decoderOut;

public WriterOutputStream(Writer writer, Charset charset) {
this.decoderIn = ByteBuffer.allocate(128);
this.writer = writer;
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) {
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();
}
}
}
2 changes: 1 addition & 1 deletion transfuse-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<version>2.5</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
3 changes: 2 additions & 1 deletion transfuse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<version>2.5</version>
<scope>test</scope>

</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
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.*;

/**
* @author John Ericksen
*/

public class FilerResourceWriterTest {

private static final String TEST_PACKAGE = "org.test";
Expand All @@ -40,28 +42,27 @@ public class FilerResourceWriterTest {
private Filer mockFiler;
private FileObject mockFile;
private OutputStream mockOutputStream;
private Writer mockWriter;
private JCodeModel codeModel;

@Before
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).flush();
verify(mockOutputStream).close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -39,28 +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).flush();
verify(mockOutputStream).close();
}
}

0 comments on commit 217e00f

Please sign in to comment.