1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.io.DebugByteTeeOutputStream |
4 | |
5 | Copyright 2001, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | package org.apache.derby.iapi.services.io; |
21 | |
22 | import java.io.*; |
23 | import org.apache.derby.iapi.services.io.AccessibleByteArrayOutputStream; |
24 | |
25 | |
26 | class DebugByteTeeOutputStream extends FilterOutputStream { |
27 | private AccessibleByteArrayOutputStream tee = new AccessibleByteArrayOutputStream(256); |
28 | |
29 | DebugByteTeeOutputStream(OutputStream out) { |
30 | super(out); |
31 | } |
32 | |
33 | public void write(int b) throws IOException { |
34 | out.write(b); |
35 | tee.write(b); |
36 | } |
37 | |
38 | public void write(byte[] b, int off, int len) throws IOException { |
39 | |
40 | out.write(b,off,len); |
41 | tee.write(b,off,len); |
42 | } |
43 | |
44 | |
45 | void checkObject(Formatable f) { |
46 | |
47 | ByteArrayInputStream in = new ByteArrayInputStream(tee.getInternalByteArray(), 0, tee.size()); |
48 | |
49 | FormatIdInputStream fin = new FormatIdInputStream(in); |
50 | |
51 | // now get an empty object given the format identification |
52 | // read it in |
53 | // then compare it??? |
54 | |
55 | Formatable f1 = null; |
56 | try { |
57 | |
58 | f1 = (Formatable) fin.readObject(); |
59 | |
60 | if (f1.equals(f)) { |
61 | return; |
62 | } |
63 | |
64 | // If the two objects are not equal and it looks |
65 | // like they don't implement their own equals() |
66 | // (which requires a matching hashCode() then |
67 | // just return. The object was read sucessfully. |
68 | |
69 | if ((f1.hashCode() == System.identityHashCode(f1)) && |
70 | (f.hashCode() == System.identityHashCode(f))) |
71 | return; |
72 | } catch (Throwable t) { |
73 | System.out.println("FormatableError:read error : " + t.toString()); |
74 | System.out.println("FormatableError:class written : " + f.getClass()); |
75 | if( null == f1) |
76 | System.out.println("FormatableError:read back as null"); |
77 | else |
78 | System.out.println("FormatableError:class read : " + f1.getClass()); |
79 | System.out.println("FormatableError:write id : " + FormatIdUtil.formatIdToString(f.getTypeFormatId())); |
80 | if( null != f1) |
81 | System.out.println("FormatableError:read id : " + FormatIdUtil.formatIdToString(f1.getTypeFormatId())); |
82 | t.printStackTrace(System.out); |
83 | } |
84 | |
85 | //System.out.println("FormatableError:Class written " + f.getClass() + " format id " + f.getTypeFormatId()); |
86 | //if (f1 != null) |
87 | //System.out.println("FormatableError:Class read " + f1.getClass() + " format id " + f1.getTypeFormatId()); |
88 | } |
89 | |
90 | } |