-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from doniwinata0309/fix/writefile
Replace outputstream with writeroutputstream
- Loading branch information
Showing
8 changed files
with
123 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
transfuse-core/src/main/java/org/androidtransfuse/gen/WriterOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters