1 package jp.sf.grizzly.storage.impl;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8
9 import jp.sf.grizzly.storage.StreamStorage;
10 import jp.sf.grizzly.storage.StreamStorageException;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 public class ByteArrayStreamStorageImpl implements StreamStorage
16 {
17 /***
18 * Logger for this class
19 */
20 private static final Log log = LogFactory
21 .getLog(ByteArrayStreamStorageImpl.class);
22
23 private int DEFAULT_ARRAY_SIZE = 512;
24
25 private InputStream inputStream;
26
27 private ByteArrayOutputStream outputStream;
28
29 private String encoding;
30
31 private byte[] buffer = null;
32
33 public ByteArrayStreamStorageImpl(InputStream in, String encoding)
34 throws StreamStorageException
35 {
36 init(in, encoding);
37 }
38
39 public void init(InputStream in, String encoding)
40 throws StreamStorageException
41 {
42
43 inputStream = in;
44 this.encoding = encoding;
45 outputStream = new ByteArrayOutputStream(DEFAULT_ARRAY_SIZE);
46 }
47
48 public void destroy()
49 {
50 buffer = null;
51 outputStream = null;
52 }
53
54
55
56
57 public void commit() throws StreamStorageException
58 {
59 try
60 {
61 if (inputStream != null)
62 {
63 inputStream.close();
64 }
65 if (outputStream != null)
66 {
67 outputStream.flush();
68 outputStream.close();
69 }
70
71 buffer = outputStream.toByteArray();
72 inputStream = new ByteArrayInputStream(buffer);
73 outputStream = new ByteArrayOutputStream(DEFAULT_ARRAY_SIZE);
74 }
75
76 catch (IOException e)
77 {
78 log.error("I/O error occurs. ", e);
79 throw new StreamStorageException((Throwable) e);
80 }
81 }
82
83
84
85
86 public InputStream getResultInputStream() throws StreamStorageException
87 {
88 try
89 {
90
91 if (buffer == null)
92 {
93 if (inputStream.markSupported())
94 {
95 inputStream.reset();
96 }
97 return inputStream;
98 }
99
100 return new ByteArrayInputStream(buffer);
101 }
102
103 catch (IOException e)
104 {
105 log.error("I/O error occurs. ", e);
106 throw new StreamStorageException((Throwable) e);
107 }
108 }
109
110
111
112
113 public String getEncoding()
114 {
115 return encoding;
116 }
117
118 /***
119 * @param encoding The encoding to set.
120 */
121 public void setEncoding(String encoding)
122 {
123 this.encoding = encoding;
124 }
125
126
127
128
129 public InputStream getInputStream()
130 {
131 return inputStream;
132 }
133
134 /***
135 * @param inputStream The inputStream to set.
136 */
137 public void setInputStream(InputStream inputStream)
138 {
139 this.inputStream = inputStream;
140 }
141
142
143
144
145 public OutputStream getOutputStream()
146 {
147 return outputStream;
148 }
149
150 /***
151 * @param outputStream The outputStream to set.
152 */
153 public void setOutputStream(OutputStream outputStream)
154 {
155 if (outputStream instanceof ByteArrayOutputStream)
156 {
157 this.outputStream = (ByteArrayOutputStream) outputStream;
158 }
159 else
160 {
161 throw new IllegalArgumentException(
162 "The output stream is not ByteArrayOutputStream.");
163 }
164 }
165 }