| 1 | package dolda.jsvc.util; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.nio.ByteBuffer; |
| 5 | import java.nio.CharBuffer; |
| 6 | import java.nio.charset.Charset; |
| 7 | import java.nio.charset.CharsetDecoder; |
| 8 | |
| 9 | public class MixedBuffer { |
| 10 | private ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
| 11 | private Writer conv; |
| 12 | private Charset cs; |
| 13 | |
| 14 | public MixedBuffer(Charset cs) { |
| 15 | this.cs = cs; |
| 16 | conv = new OutputStreamWriter(buf, cs); |
| 17 | } |
| 18 | |
| 19 | public MixedBuffer() { |
| 20 | this(Misc.utf8); |
| 21 | } |
| 22 | |
| 23 | public void append(byte b) { |
| 24 | buf.write(b); |
| 25 | } |
| 26 | |
| 27 | public void append(char c) { |
| 28 | try { |
| 29 | conv.write(c); |
| 30 | conv.flush(); |
| 31 | } catch(IOException e) { |
| 32 | throw(new Error(e)); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | public String convert() throws java.nio.charset.CharacterCodingException { |
| 37 | CharsetDecoder dec = cs.newDecoder(); |
| 38 | ByteBuffer in = ByteBuffer.wrap(buf.toByteArray()); |
| 39 | CharBuffer out = dec.decode(in); |
| 40 | return(out.toString()); |
| 41 | } |
| 42 | |
| 43 | public int size() { |
| 44 | return(buf.size()); |
| 45 | } |
| 46 | } |