001: package vicazh.hyperpool;
002:
003: import java.beans.*;
004: import java.io.*;
005: import java.util.logging.*;
006: import javax.xml.transform.*;
007:
008: /**
009: * The object reader
010: *
011: * @author Victor Zhigunov
012: * @version 0.4.0
013: */
014: public class Reader extends Processor implements ExceptionListener {
015:
016: /**
017: * @param tin
018: * xsl source transformer
019: */
020: public Reader(String tin) throws TransformerException {
021: super (tin);
022: }
023:
024: /**
025: * Return bytes from resource
026: *
027: * @param in
028: * xml source href
029: * @return bytes
030: */
031: static public byte[] get(String in) throws IOException {
032: return get(new BufferedInputStream(Reader.class.getResource(in)
033: .openStream()));
034: }
035:
036: /**
037: * Return bytes from file
038: *
039: * @param in
040: * xml source file
041: * @return bytes
042: */
043: static public byte[] get(File in) throws IOException {
044: return get(new BufferedInputStream(new FileInputStream(in)));
045: }
046:
047: static private byte[] get(InputStream s) throws IOException {
048: ByteArrayOutputStream stream = new ByteArrayOutputStream();
049: int i;
050: while ((i = s.read()) != -1) {
051: Thread.yield();
052: stream.write(i);
053: }
054: s.close();
055: return stream.toByteArray();
056: }
057:
058: /**
059: * Return object from resource
060: *
061: * @param in
062: * xml source href
063: * @return object
064: */
065: public Object getObject(String in) throws IOException,
066: TransformerException {
067: return getObject(get(in));
068: }
069:
070: /**
071: * Read object from file
072: *
073: * @param in
074: * xml source file
075: * @return object
076: */
077: public Object getObject(File in) throws IOException,
078: TransformerException {
079: return getObject(get(in));
080: }
081:
082: /**
083: * Read object from file
084: *
085: * @param in
086: * xml source file
087: * @param object
088: * default object
089: * @return object
090: */
091: public Object getObject(File in, Object object) throws IOException,
092: TransformerException {
093: if (!in.exists() || in.length() == 0)
094: return object;
095: return getObject(in);
096: }
097:
098: /**
099: * Read object from bytes
100: *
101: * @param b
102: * bytes
103: * @return object
104: */
105: public Object getObject(byte[] b) throws TransformerException {
106: byte[] in = transform(b);
107: Start.logger.finest(new String(in));
108: XMLDecoder d = new XMLDecoder(new ByteArrayInputStream(in),
109: this );
110: d.setExceptionListener(this );
111: Object o = d.readObject();
112: d.close();
113: return o;
114: }
115:
116: public void exceptionThrown(Exception e) {
117: Start.logger.log(Level.SEVERE, e.getMessage(), e);
118: }
119: }
|