X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Futil%2FMixedBuffer.java;fp=src%2Fdolda%2Fjsvc%2Futil%2FMixedBuffer.java;h=f78b035beeff82528993bd725da4e60b705f2d0b;hb=efa9722bc37910a6224346bb20210205c96ecc47;hp=0000000000000000000000000000000000000000;hpb=ffbaba013fa57247890912e54e4e1e085482720d;p=jsvc.git diff --git a/src/dolda/jsvc/util/MixedBuffer.java b/src/dolda/jsvc/util/MixedBuffer.java new file mode 100644 index 0000000..f78b035 --- /dev/null +++ b/src/dolda/jsvc/util/MixedBuffer.java @@ -0,0 +1,46 @@ +package dolda.jsvc.util; + +import java.io.*; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; + +public class MixedBuffer { + private ByteArrayOutputStream buf = new ByteArrayOutputStream(); + private Writer conv; + private Charset cs; + + public MixedBuffer(Charset cs) { + this.cs = cs; + conv = new OutputStreamWriter(buf, cs); + } + + public MixedBuffer() { + this(Misc.utf8); + } + + public void append(byte b) { + buf.write(b); + } + + public void append(char c) { + try { + conv.write(c); + conv.flush(); + } catch(IOException e) { + throw(new Error(e)); + } + } + + public String convert() throws java.nio.charset.CharacterCodingException { + CharsetDecoder dec = cs.newDecoder(); + ByteBuffer in = ByteBuffer.wrap(buf.toByteArray()); + CharBuffer out = dec.decode(in); + return(out.toString()); + } + + public int size() { + return(buf.size()); + } +}