Merge branch 'master' of git.dolda2000.com:/srv/git/r/jsvc
[jsvc.git] / src / dolda / jsvc / util / MixedBuffer.java
CommitLineData
efa9722b
FT
1package dolda.jsvc.util;
2
3import java.io.*;
4import java.nio.ByteBuffer;
5import java.nio.CharBuffer;
6import java.nio.charset.Charset;
7import java.nio.charset.CharsetDecoder;
8
9public 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}