KickJava   Java API By Example, From Geeks To Geeks.

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


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.TCDataInput;
7 import com.tc.object.dna.impl.DNAEncoding;
8
9 import java.io.EOFException JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.ObjectInput JavaDoc;
13
14 /**
15  * <p>
16  * ObjectOutputStream/ObjectInputStream is totally inefficient when writing just a small amount of data. It creates a
17  * lot of garbage (2 or 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 readObject() method in this class can only handle reading Literal Objects and will throw an AssertionError for
25  * any other kind of object.
26  * </p>
27  * <p>
28  * TCObjectOutputStream compliments this class. Since methods DataInputStream are final methods and I want a different
29  * write/readUTF() implementation, I have to reimplement DataInputStream.
30  * </p>
31  */

32 public class TCObjectInputStream implements ObjectInput JavaDoc, TCDataInput {
33
34   private static final DNAEncoding SERIALIZER_ENCODING = new DNAEncoding(DNAEncoding.SERIALIZER);
35
36   private final InputStream JavaDoc in;
37
38   public TCObjectInputStream(InputStream JavaDoc in) {
39     this.in = in;
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 readObject() is called only for literal objects. Example : Sleepycat Serialization.
45    *
46    * @see LiteralValues, DNAEncoding
47    */

48   public Object JavaDoc readObject() throws ClassNotFoundException JavaDoc, IOException JavaDoc {
49     return SERIALIZER_ENCODING.decode(this);
50   }
51
52   public int read() throws IOException JavaDoc {
53     return in.read();
54   }
55
56   public int read(byte[] b) throws IOException JavaDoc {
57     return in.read(b);
58   }
59
60   public int read(byte[] b, int off, int len) throws IOException JavaDoc {
61     return in.read(b, off, len);
62   }
63
64   public long skip(long n) throws IOException JavaDoc {
65     return in.skip(n);
66   }
67
68   public int available() throws IOException JavaDoc {
69     return in.available();
70   }
71
72   public void close() throws IOException JavaDoc {
73     in.close();
74   }
75
76   public void readFully(byte[] b) throws IOException JavaDoc {
77     readFully(b, 0, b.length);
78   }
79
80   public void readFully(byte[] b, int off, int len) throws IOException JavaDoc {
81     if (len < 0) throw new IndexOutOfBoundsException JavaDoc();
82     int n = 0;
83     while (n < len) {
84       int count = in.read(b, off + n, len - n);
85       if (count < 0) throw new EOFException JavaDoc();
86       n += count;
87     }
88   }
89
90   public int skipBytes(int n) throws IOException JavaDoc {
91     int total = 0;
92     int cur = 0;
93
94     while ((total < n) && ((cur = (int) in.skip(n - total)) > 0)) {
95       total += cur;
96     }
97
98     return total;
99   }
100
101   public boolean readBoolean() throws IOException JavaDoc {
102     int ch = in.read();
103     if (ch < 0) throw new EOFException JavaDoc();
104     return (ch != 0);
105   }
106
107   public byte readByte() throws IOException JavaDoc {
108     int ch = in.read();
109     if (ch < 0) throw new EOFException JavaDoc();
110     return (byte) (ch);
111   }
112
113   public int readUnsignedByte() throws IOException JavaDoc {
114     int ch = in.read();
115     if (ch < 0) throw new EOFException JavaDoc();
116     return ch;
117   }
118
119   public short readShort() throws IOException JavaDoc {
120     int ch1 = in.read();
121     int ch2 = in.read();
122     if ((ch1 | ch2) < 0) throw new EOFException JavaDoc();
123     return (short) ((ch1 << 8) + (ch2 << 0));
124   }
125
126   public int readUnsignedShort() throws IOException JavaDoc {
127     int ch1 = in.read();
128     int ch2 = in.read();
129     if ((ch1 | ch2) < 0) throw new EOFException JavaDoc();
130     return (ch1 << 8) + (ch2 << 0);
131   }
132
133   public char readChar() throws IOException JavaDoc {
134     int ch1 = in.read();
135     int ch2 = in.read();
136     if ((ch1 | ch2) < 0) throw new EOFException JavaDoc();
137     return (char) ((ch1 << 8) + (ch2 << 0));
138   }
139
140   public int readInt() throws IOException JavaDoc {
141     int ch1 = in.read();
142     int ch2 = in.read();
143     int ch3 = in.read();
144     int ch4 = in.read();
145     if ((ch1 | ch2 | ch3 | ch4) < 0) throw new EOFException JavaDoc();
146     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
147   }
148
149   public long readLong() throws IOException JavaDoc {
150     final byte readBuffer[] = new byte[8];
151     readFully(readBuffer, 0, 8);
152     return (((long) readBuffer[0] << 56) + ((long) (readBuffer[1] & 255) << 48) + ((long) (readBuffer[2] & 255) << 40)
153             + ((long) (readBuffer[3] & 255) << 32) + ((long) (readBuffer[4] & 255) << 24)
154             + ((readBuffer[5] & 255) << 16) + ((readBuffer[6] & 255) << 8) + ((readBuffer[7] & 255) << 0));
155   }
156
157   public float readFloat() throws IOException JavaDoc {
158     return Float.intBitsToFloat(readInt());
159   }
160
161   public double readDouble() throws IOException JavaDoc {
162     return Double.longBitsToDouble(readLong());
163   }
164
165   public String JavaDoc readLine() {
166     throw new UnsupportedOperationException JavaDoc("Use BufferedReader instead.");
167   }
168
169   /**
170    * This implemetation of writeUTF differes from the DataOutputStream's implementation in the following ways. 1) It
171    * handles null strings. 2) It handles long strings (no 65K limit that UTF Encoding poses) 3) Data should have been
172    * written by TCObjectOutputStream.
173    */

174   public String JavaDoc readUTF() throws IOException JavaDoc {
175     return readString();
176   }
177
178   /**
179    * This reads a 4 byte length and then the UTF String itself. If the length is negative, then it is a null string.
180    *
181    * @throws IOException
182    * @see writeUTF();
183    */

184   public String JavaDoc readString() throws IOException JavaDoc {
185     int len = readInt();
186     if (len < 0) return null;
187     else if (len == 0) return "";
188     final byte strbytes[] = new byte[len];
189     readFully(strbytes);
190     return new String JavaDoc(strbytes, "UTF-8");
191   }
192
193 }
194
Popular Tags