1
2
3
4
5
6
7
8
9
10
11 package org.mule.config.spring.util;
12
13 import org.mule.util.IOUtils;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.Reader;
19 import java.io.UnsupportedEncodingException;
20
21 import org.springframework.core.io.AbstractResource;
22
23
24
25
26
27
28
29
30
31
32
33 public class CachedResource extends AbstractResource
34 {
35
36 private static final String DEFAULT_DESCRIPTION = "cached in-memory resource";
37
38 private final byte[] buffer;
39 private final String description;
40
41 public CachedResource(byte[] source)
42 {
43 this(source, null);
44 }
45
46 public CachedResource(String source, String encoding) throws UnsupportedEncodingException
47 {
48 this(source.trim().getBytes(encoding), DEFAULT_DESCRIPTION);
49 }
50
51 public CachedResource(byte[] source, String description)
52 {
53 this.buffer = source;
54 this.description = description;
55 }
56
57 public CachedResource(Reader reader, String encoding) throws IOException
58 {
59 this(IOUtils.toByteArray(reader, encoding), DEFAULT_DESCRIPTION);
60 }
61
62 public String getDescription()
63 {
64 return (description == null) ? "" : description;
65 }
66
67 public InputStream getInputStream() throws IOException
68 {
69
70
71
72 return new ByteArrayInputStream(buffer);
73 }
74 }