KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > io > BufferType


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.io;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.OutputStream JavaDoc;
11
12 /**
13  * @oddjob.description A buffer can be used to store output for different jobs to use.
14  *
15  * @oddjob.example
16  * <pre>
17  * &lt;copy id="foo" from="foo.txt"&gt;
18  * &lt;output&gt;
19  * &lt;buffer/&gt;
20  * &lt;/output&gt;
21  * &lt;/copy&gt;
22  * &lt;echo text="${foo.output}"&gt;
23  */

24 public class BufferType {
25
26     private final ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
27     
28     public Object JavaDoc valueFor(Class JavaDoc required) {
29         if (required == null) {
30             required = OutputStream JavaDoc.class;
31         }
32         if (required.isAssignableFrom(String JavaDoc.class)) {
33             return new String JavaDoc(buffer.toByteArray());
34         }
35         if (required.isAssignableFrom(InputStream JavaDoc.class)) {
36             return new ByteArrayInputStream JavaDoc(buffer.toByteArray());
37         }
38         if (required.isAssignableFrom(OutputStream JavaDoc.class)) {
39             return new WrapperOutputStream();
40         }
41         throw new ClassCastException JavaDoc("Can not convert from Buffer to " + required);
42     }
43
44     public String JavaDoc toString() {
45         return "Buffer (" + buffer.size() + " bytes)";
46     }
47     
48     public class WrapperOutputStream extends OutputStream JavaDoc {
49         public Object JavaDoc valueFor(Class JavaDoc required) {
50             if (required == null) {
51                 throw new ClassCastException JavaDoc("No default conversion for Buffer contents.");
52             }
53             if (required.isAssignableFrom(String JavaDoc.class)) {
54                 return new String JavaDoc(buffer.toByteArray());
55             }
56             if (required.isAssignableFrom(InputStream JavaDoc.class)) {
57                 return new ByteArrayInputStream JavaDoc(buffer.toByteArray());
58             }
59             throw new ClassCastException JavaDoc("Can not convert from Buffer contents to " + required);
60         }
61         
62         /* (non-Javadoc)
63          * @see java.io.OutputStream#write(int)
64          */

65         public void write(int b) throws IOException JavaDoc {
66             buffer.write(b);
67         }
68         /* (non-Javadoc)
69          * @see java.io.OutputStream#close()
70          */

71         public void close() throws IOException JavaDoc {
72             buffer.close();
73         }
74         /* (non-Javadoc)
75          * @see java.io.OutputStream#flush()
76          */

77         public void flush() throws IOException JavaDoc {
78             buffer.flush();
79         }
80         /* (non-Javadoc)
81          * @see java.io.OutputStream#write(byte[])
82          */

83         public void write(byte[] b) throws IOException JavaDoc {
84             buffer.write(b);
85         }
86         /* (non-Javadoc)
87          * @see java.io.OutputStream#write(byte[], int, int)
88          */

89         public void write(byte[] b, int off, int len) throws IOException JavaDoc {
90             buffer.write(b, off, len);
91         }
92         
93     }
94     
95 }
96
Popular Tags