01: /*
02: * ChainBuilder ESB
03: * Visual Enterprise Integration
04: *
05: * Copyright (C) 2006 Bostech Corporation
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the
09: * Free Software Foundation; either version 2 of the License, or (at your option)
10: * any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15: * for more details.
16: *
17: * You should have received a copy of the GNU General Public License along with
18: * this program; if not, write to the Free Software Foundation, Inc.,
19: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: *
21: *
22: * $Id: ByteArraySource.java 6624 2007-04-11 01:09:05Z zjin $
23: */
24: package com.bostechcorp.cbesb.runtime.ccsl.nmhandler;
25:
26: import java.io.ByteArrayInputStream;
27: import java.io.InputStream;
28: import java.io.InputStreamReader;
29: import java.io.Reader;
30: import java.io.UnsupportedEncodingException;
31: import java.nio.charset.Charset;
32:
33: import javax.xml.transform.Source;
34: import javax.xml.transform.stream.StreamSource;
35:
36: import org.apache.commons.logging.Log;
37: import org.apache.commons.logging.LogFactory;
38:
39: /**
40: * A helper class which provides a JAXP {@link Source} from a byte[]
41: * which can be read as many times as required.
42: *
43: */
44: public class ByteArraySource extends StreamSource {
45:
46: protected static transient Log logger = LogFactory
47: .getLog(ByteArraySource.class);
48: protected byte[] bytes;
49: protected Charset charset;
50:
51: public ByteArraySource(byte[] bytes) {
52: this .bytes = bytes;
53: charset = Charset.defaultCharset();
54: }
55:
56: public ByteArraySource(byte[] bytes, String systemId) {
57: this .bytes = bytes;
58: charset = Charset.defaultCharset();
59: setSystemId(systemId);
60: }
61:
62: public InputStream getInputStream() {
63: return new ByteArrayInputStream(bytes);
64: }
65:
66: public Reader getReader() {
67: return new InputStreamReader(getInputStream(), charset);
68: }
69:
70: public String toString() {
71: try {
72: return "ByteArraySource["
73: + new String(bytes, charset.displayName()) + "]";
74: } catch (UnsupportedEncodingException e) {
75:
76: logger.error("Exception in toString(): " + e.getMessage());
77: if (logger.isDebugEnabled()) {
78: logger.debug("Exception in toString():", e);
79: }
80: return "StringSource[UNABLE TO DISPLAY]";
81: }
82: }
83:
84: public Charset getCharset() {
85: return charset;
86: }
87:
88: public void setCharset(Charset charset) {
89: this .charset = charset;
90: }
91:
92: public byte[] getBytes() {
93: return bytes;
94: }
95:
96: }
|