1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.surf.util;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
26 import java.io.Serializable;
27
28 import org.springframework.util.FileCopyUtils;
29
30
31
32
33
34 public class InputStreamContent implements Content, Serializable
35 {
36 private static final long serialVersionUID = -7729633986840536282L;
37
38 private InputStream stream;
39 private String mimetype;
40 private String encoding;
41
42
43 private String content;
44
45
46
47
48
49
50
51
52 public InputStreamContent(InputStream stream, String mimetype, String encoding)
53 {
54 this.stream = stream;
55 this.mimetype = mimetype;
56 this.encoding = encoding;
57 }
58
59
60
61
62 public String getContent()
63 throws IOException
64 {
65
66
67 if (this.content == null)
68 {
69 ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
70 FileCopyUtils.copy(stream, os);
71 byte[] bytes = os.toByteArray();
72
73 String encoding = getEncoding();
74
75 this.content = (encoding == null) ? new String(bytes) : new String(bytes, encoding);
76 }
77 return this.content;
78 }
79
80
81
82
83 public InputStream getInputStream()
84 {
85 return stream;
86 }
87
88
89
90
91
92 public Reader getReader()
93 throws IOException
94 {
95 return (encoding == null) ? new InputStreamReader(stream) : new InputStreamReader(stream, encoding);
96 }
97
98
99
100
101 public long getSize()
102 {
103 return -1;
104 }
105
106
107
108
109 public String getMimetype()
110 {
111 return mimetype;
112 }
113
114
115
116
117 public String getEncoding()
118 {
119 return encoding;
120 }
121
122 }