KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > InOutUtil


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.ObjectInputStream JavaDoc;
37 import java.io.ObjectOutputStream JavaDoc;
38 import java.io.OutputStream JavaDoc;
39 import java.io.Serializable JavaDoc;
40
41 /**
42  * Input / Output utility
43  *
44  * @author fredt@users
45  * @version 1.8.0
46  * @since 1.7.2
47  */

48 public class InOutUtil {
49
50     /**
51      * Implementation only supports unix line-end format and is suitable for
52      * processing HTTP and other network protocol communications. Reads and writes
53      * a line of data. Returns the number of bytes read/written.
54      */

55     public static int readLine(InputStream JavaDoc in,
56                                OutputStream JavaDoc out) throws IOException JavaDoc {
57
58         int count = 0;
59
60         for (;;) {
61             int b = in.read();
62
63             if (b == -1) {
64                 break;
65             }
66
67             count++;
68
69             out.write(b);
70
71             if (b == '\n') {
72                 break;
73             }
74         }
75
76         return count;
77     }
78
79     /**
80      * Retrieves the serialized form of the specified <CODE>Object</CODE>
81      * as an array of bytes.
82      *
83      * @param s the Object to serialize
84      * @return a static byte array representing the passed Object
85      * @throws HsqlException if a serialization failure occurs
86      */

87     public static byte[] serialize(Serializable JavaDoc s) throws IOException JavaDoc {
88
89         HsqlByteArrayOutputStream bo = new HsqlByteArrayOutputStream();
90         ObjectOutputStream JavaDoc os = new ObjectOutputStream JavaDoc(bo);
91
92         os.writeObject(s);
93
94         return bo.toByteArray();
95     }
96
97     /**
98      * Deserializes the specified byte array to an
99      * <CODE>Object</CODE> instance.
100      *
101      * @return the Object resulting from deserializing the specified array of bytes
102      * @param ba the byte array to deserialize to an Object
103      * @throws HsqlException if a serialization failure occurs
104      */

105     public static Serializable JavaDoc deserialize(byte[] ba)
106     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
107
108         HsqlByteArrayInputStream bi = new HsqlByteArrayInputStream(ba);
109         ObjectInputStream JavaDoc is = new ObjectInputStream JavaDoc(bi);
110
111         return (Serializable JavaDoc) is.readObject();
112     }
113 }
114
Popular Tags