KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > DebugByteTeeOutputStream


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.DebugByteTeeOutputStream
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. 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  */

21 package org.apache.derby.iapi.services.io;
22
23 import java.io.*;
24 import org.apache.derby.iapi.services.io.AccessibleByteArrayOutputStream;
25
26
27 class DebugByteTeeOutputStream extends FilterOutputStream {
28     private AccessibleByteArrayOutputStream tee = new AccessibleByteArrayOutputStream(256);
29
30     DebugByteTeeOutputStream(OutputStream out) {
31         super(out);
32     }
33     
34     public void write(int b) throws IOException {
35         out.write(b);
36         tee.write(b);
37     }
38
39     public void write(byte[] b, int off, int len) throws IOException {
40
41         out.write(b,off,len);
42         tee.write(b,off,len);
43     }
44
45
46     void checkObject(Formatable f) {
47
48         ByteArrayInputStream in = new ByteArrayInputStream(tee.getInternalByteArray(), 0, tee.size());
49
50         FormatIdInputStream fin = new FormatIdInputStream(in);
51
52         // now get an empty object given the format identification
53
// read it in
54
// then compare it???
55

56         Formatable f1 = null;
57         try {
58
59             f1 = (Formatable) fin.readObject();
60
61             if (f1.equals(f)) {
62                 return;
63             }
64
65             // If the two objects are not equal and it looks
66
// like they don't implement their own equals()
67
// (which requires a matching hashCode() then
68
// just return. The object was read sucessfully.
69

70             if ((f1.hashCode() == System.identityHashCode(f1)) &&
71                 (f.hashCode() == System.identityHashCode(f)))
72                 return;
73         } catch (Throwable JavaDoc t) {
74             System.out.println("FormatableError:read error : " + t.toString());
75             System.out.println("FormatableError:class written : " + f.getClass());
76             if( null == f1)
77                 System.out.println("FormatableError:read back as null");
78             else
79                 System.out.println("FormatableError:class read : " + f1.getClass());
80             System.out.println("FormatableError:write id : " + FormatIdUtil.formatIdToString(f.getTypeFormatId()));
81             if( null != f1)
82                 System.out.println("FormatableError:read id : " + FormatIdUtil.formatIdToString(f1.getTypeFormatId()));
83             t.printStackTrace(System.out);
84         }
85
86         //System.out.println("FormatableError:Class written " + f.getClass() + " format id " + f.getTypeFormatId());
87
//if (f1 != null)
88
//System.out.println("FormatableError:Class read " + f1.getClass() + " format id " + f1.getTypeFormatId());
89
}
90
91 }
92
Popular Tags