01: /**
02: * Sequoia: Database clustering technology.
03: * Copyright (C) 2002-2004 French National Institute For Research In Computer
04: * Science And Control (INRIA).
05: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
06: * Contact: sequoia@continuent.org
07: *
08: * Licensed under the Apache License, Version 2.0 (the "License");
09: * 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: */package org.continuent.sequoia.common.stream;
23:
24: import java.io.ByteArrayOutputStream;
25: import java.io.IOException;
26: import java.io.ObjectOutputStream;
27: import java.nio.charset.Charset;
28:
29: /**
30: * This is a useful stream class to compress and decompress object to a stream.
31: *
32: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
33: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
34: */
35: public class DriverStream {
36:
37: /** @see DriverBufferedOutputStream#writeLongUTF(String) */
38: static final int STRING_CHUNK_SIZE = 64000 / 3;
39:
40: static final Charset UTF8Codec = Charset.forName("UTF-8");
41:
42: /**
43: * Statistic method to count the number of bytes of a class.
44: *
45: * @param obj to count bytes
46: * @return the number of bytes
47: * @throws IOException if fails to read
48: */
49: public static final int countBytes(Object obj) throws IOException {
50: ByteArrayOutputStream bos = new ByteArrayOutputStream();
51: ObjectOutputStream oos = new ObjectOutputStream(bos);
52: oos.writeObject(obj);
53: oos.flush();
54: oos.close();
55: bos.close();
56: return bos.size();
57: }
58:
59: }
|