KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > io > serializer > TCObjectOutputStream


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.io.serializer;
5
6 import com.tc.io.TCDataOutput;
7 import com.tc.object.dna.impl.DNAEncoding;
8
9 import java.io.IOException JavaDoc;
10 import java.io.ObjectOutput JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.io.UnsupportedEncodingException JavaDoc;
13
14 /**
15  * <p>
16  * ObjectOutputStream is totally inefficient when writing just a small amount of data. It creates a lot of garbage (2 or
17  * 3 KB to just a create it) plus it also writes all the header/block header into the stream.
18  * </p>
19  * <p>
20  * In the server when we serialize the ManagedObjects to sleepycat, we really dont need all the fancy things and we cant
21  * afford to create such overhead. This class is an attempt to solve these problems without getting too complicated.
22  * </p>
23  * <p>
24  * The writeObject() method in this class can only handle writing Literal Objects and will throw an AssertionError for
25  * any other kind of object.
26  * </p>
27  * <p>
28  * TCObjectInputStream compliments this class. Since TCDataOutput doesnt throw IOException and DataOutputStream has all
29  * final methods, I have to reimplement DataOutputStream to not throw IOException.
30  * </p>
31  */

32 public class TCObjectOutputStream implements ObjectOutput JavaDoc, TCDataOutput {
33
34   private static final DNAEncoding SERIALIZER_ENCODING = new DNAEncoding(DNAEncoding.SERIALIZER);
35
36   protected final OutputStream JavaDoc out;
37
38   public TCObjectOutputStream(OutputStream JavaDoc out) {
39     this.out = out;
40   }
41
42   /**
43    * This method is the reason I am writing this class. This implementation only can handle Literal Objects and is
44    * designed to be used where writeObject() is called only for literal objects. Example : Sleepycat Serialization.
45    *
46    * @see LiteralValues, DNAEncoding
47    */

48   public void writeObject(Object JavaDoc obj) {
49     if (obj != null && obj.getClass().getName().charAt(0) == '[') {
50       SERIALIZER_ENCODING.encodeArray(obj, this);
51     } else {
52       SERIALIZER_ENCODING.encode(obj, this);
53     }
54   }
55
56   /**
57    * This writes a 4 byte length and then the UTF String itself. If the length is negative, then it is a null string.
58    *
59    * @throws IOException
60    * @see writeUTF();
61    */

62   public void writeString(String JavaDoc string) {
63     if (string == null) {
64       writeInt(-1);
65       return;
66     }
67     try {
68       byte strbytes[] = string.getBytes("UTF-8");
69       writeInt(strbytes.length);
70       write(strbytes);
71     } catch (UnsupportedEncodingException JavaDoc e) {
72       throw new AssertionError JavaDoc(e);
73     }
74   }
75
76   public void flush() {
77     try {
78       out.flush();
79     } catch (IOException JavaDoc e) {
80       throw new AssertionError JavaDoc(e);
81     }
82   }
83
84   public void writeBytes(String JavaDoc s) {
85     int len = s.length();
86     try {
87       for (int i = 0; i < len; i++) {
88         out.write((byte) s.charAt(i));
89       }
90     } catch (IOException JavaDoc e) {
91       throw new AssertionError JavaDoc(e);
92     }
93   }
94
95   public void writeChars(String JavaDoc s) {
96     int len = s.length();
97     try {
98       for (int i = 0; i < len; i++) {
99         int v = s.charAt(i);
100         out.write((v >>> 8) & 0xFF);
101         out.write((v >>> 0) & 0xFF);
102       }
103     } catch (IOException JavaDoc e) {
104       throw new AssertionError JavaDoc(e);
105     }
106   }
107
108   /**
109    * This implemetation of writeUTF differes from the DataOutputStream's implementation in the following ways. 1) It
110    * handles null strings. 2) It handles long strings (no 65K limit that UTF Encoding poses) 3) Cant be read by
111    * DataInputStream. Use TCObjectInputStream.
112    */

113   public void writeUTF(String JavaDoc str) {
114     writeString(str);
115   }
116
117   public void close() {
118     flush();
119     try {
120       out.close();
121     } catch (IOException JavaDoc e) {
122       throw new AssertionError JavaDoc(e);
123     }
124   }
125
126   public void write(int b) {
127     try {
128       out.write(b);
129     } catch (IOException JavaDoc e) {
130       throw new AssertionError JavaDoc(e);
131     }
132   }
133
134   public void write(byte[] value) {
135     write(value, 0, value.length);
136   }
137
138   public void write(byte[] value, int offset, int length) {
139     try {
140       out.write(value, offset, length);
141     } catch (IOException JavaDoc e) {
142       throw new AssertionError JavaDoc(e);
143     }
144   }
145
146   public void writeBoolean(boolean value) {
147     try {
148       out.write(value ? 1 : 0);
149     } catch (IOException JavaDoc e) {
150       throw new AssertionError JavaDoc(e);
151     }
152   }
153
154   public void writeByte(int value) {
155     try {
156       out.write(value);
157     } catch (IOException JavaDoc e) {
158       throw new AssertionError JavaDoc(e);
159     }
160   }
161
162   public void writeChar(int v) {
163     try {
164       out.write((v >>> 8) & 0xFF);
165       out.write((v >>> 0) & 0xFF);
166     } catch (IOException JavaDoc e) {
167       throw new AssertionError JavaDoc(e);
168     }
169   }
170
171   public void writeDouble(double value) {
172     writeLong(Double.doubleToLongBits(value));
173   }
174
175   public void writeFloat(float value) {
176     writeInt(Float.floatToIntBits(value));
177   }
178
179   public void writeInt(int v) {
180     try {
181       out.write((v >>> 24) & 0xFF);
182       out.write((v >>> 16) & 0xFF);
183       out.write((v >>> 8) & 0xFF);
184       out.write((v >>> 0) & 0xFF);
185     } catch (IOException JavaDoc e) {
186       throw new AssertionError JavaDoc(e);
187     }
188
189   }
190
191   public void writeLong(long v) {
192     final byte writeBuffer[] = new byte[8];
193     writeBuffer[0] = (byte) (v >>> 56);
194     writeBuffer[1] = (byte) (v >>> 48);
195     writeBuffer[2] = (byte) (v >>> 40);
196     writeBuffer[3] = (byte) (v >>> 32);
197     writeBuffer[4] = (byte) (v >>> 24);
198     writeBuffer[5] = (byte) (v >>> 16);
199     writeBuffer[6] = (byte) (v >>> 8);
200     writeBuffer[7] = (byte) (v >>> 0);
201     try {
202       out.write(writeBuffer, 0, 8);
203     } catch (IOException JavaDoc e) {
204       throw new AssertionError JavaDoc(e);
205     }
206   }
207
208   public void writeShort(int v) {
209     try {
210       out.write((v >>> 8) & 0xFF);
211       out.write((v >>> 0) & 0xFF);
212     } catch (IOException JavaDoc e) {
213       throw new AssertionError JavaDoc(e);
214     }
215   }
216
217 }
218
Popular Tags