blob: f97e6959c4202f4e2ea284c8e2fe28d63f14c64e [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.lib.codec;
2
3import java.io.*;
4import java.lang.reflect.*;
5
6public class HCodec implements Codec {
7 final Codec codec;
8
9 public HCodec(Codec codec) {
10 this.codec = codec;
11 }
12
13 public Object decode(Reader in, Type type) throws Exception {
14 return codec.decode(in, type);
15 }
16
17 public <T> T decode(InputStream in, Class<T> t) throws Exception {
18 return t.cast(decode(in, (Type)t));
19 }
20
21 public <T> T decode(Reader in, Class<T> t) throws Exception {
22 return t.cast(decode(in, (Type) t));
23 }
24
25 public Object decode(InputStream in, Type t) throws Exception {
26 InputStreamReader r = new InputStreamReader(in, "UTF-8");
27 return codec.decode(r, t);
28 }
29
30 public void encode(Type t, Object o, Appendable out) throws Exception {
31 codec.encode(t, o, out);
32 }
33
34 public void encode(Type t, Object o, OutputStream out) throws Exception {
35 OutputStreamWriter wr = new OutputStreamWriter(out, "UTF-8");
36 try {
37 codec.encode(t, o, wr);
38 } finally {
39 wr.flush();
40 }
41 }
42
43 public <T> T decode(File in, Class<T> t) throws Exception {
44 FileInputStream fin = new FileInputStream(in);
45 try {
46 InputStreamReader rdr = new InputStreamReader(fin, "UTF-8");
47 try {
48 return t.cast(decode(rdr, t));
49 } finally {
50 rdr.close();
51 }
52 } finally {
53 fin.close();
54 }
55
56 }
57
58 public void encode(Type t, Object o, File out) throws Exception {
59 OutputStream oout = new FileOutputStream(out);
60 try {
61 Writer wr = new OutputStreamWriter(oout, "UTF-8");
62 try {
63 codec.encode(t, o, wr);
64 } finally {
65 wr.close();
66 }
67 } finally {
68 oout.close();
69 }
70 }
71
72}