KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > stream > DriverStream


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Nicolas Modrzyk
21  * Contributor(s): Emmanuel Cecchet.
22  */

23
24 package org.continuent.sequoia.common.stream;
25
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.nio.charset.Charset JavaDoc;
30
31 /**
32  * This is a useful stream class to compress and decompress object to a stream.
33  *
34  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
35  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
36  */

37 public class DriverStream
38 {
39
40   /** @see DriverBufferedOutputStream#writeLongUTF(String) */
41   static final int STRING_CHUNK_SIZE = 64000/3;
42
43   static final Charset JavaDoc UTF8Codec = Charset.forName("UTF-8");
44
45   /**
46    * Statistic method to count the number of bytes of a class.
47    *
48    * @param obj to count bytes
49    * @return the number of bytes
50    * @throws IOException if fails to read
51    */

52   public static final int countBytes(Object JavaDoc obj) throws IOException JavaDoc
53   {
54     ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
55     ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(bos);
56     oos.writeObject(obj);
57     oos.flush();
58     oos.close();
59     bos.close();
60     return bos.size();
61   }
62
63 }
Popular Tags