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