001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.lib;
032:
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.io.ObjectInputStream;
036: import java.io.ObjectOutputStream;
037: import java.io.OutputStream;
038: import java.io.Serializable;
039:
040: /**
041: * Input / Output utility
042: *
043: * @author fredt@users
044: * @version 1.8.0
045: * @since 1.7.2
046: */
047: public class InOutUtil {
048:
049: /**
050: * Implementation only supports unix line-end format and is suitable for
051: * processing HTTP and other network protocol communications. Reads and writes
052: * a line of data. Returns the number of bytes read/written.
053: */
054: public static int readLine(InputStream in, OutputStream out)
055: throws IOException {
056:
057: int count = 0;
058:
059: for (;;) {
060: int b = in.read();
061:
062: if (b == -1) {
063: break;
064: }
065:
066: count++;
067:
068: out.write(b);
069:
070: if (b == '\n') {
071: break;
072: }
073: }
074:
075: return count;
076: }
077:
078: /**
079: * Retrieves the serialized form of the specified <CODE>Object</CODE>
080: * as an array of bytes.
081: *
082: * @param s the Object to serialize
083: * @return a static byte array representing the passed Object
084: * @throws HsqlException if a serialization failure occurs
085: */
086: public static byte[] serialize(Serializable s) throws IOException {
087:
088: HsqlByteArrayOutputStream bo = new HsqlByteArrayOutputStream();
089: ObjectOutputStream os = new ObjectOutputStream(bo);
090:
091: os.writeObject(s);
092:
093: return bo.toByteArray();
094: }
095:
096: /**
097: * Deserializes the specified byte array to an
098: * <CODE>Object</CODE> instance.
099: *
100: * @return the Object resulting from deserializing the specified array of bytes
101: * @param ba the byte array to deserialize to an Object
102: * @throws HsqlException if a serialization failure occurs
103: */
104: public static Serializable deserialize(byte[] ba)
105: throws IOException, ClassNotFoundException {
106:
107: HsqlByteArrayInputStream bi = new HsqlByteArrayInputStream(ba);
108: ObjectInputStream is = new ObjectInputStream(bi);
109:
110: return (Serializable) is.readObject();
111: }
112: }
|