1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.jdbc.BinaryToRawStream |
4 | |
5 | Copyright 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 | |
21 | package org.apache.derby.impl.jdbc; |
22 | |
23 | import org.apache.derby.iapi.services.sanity.SanityManager; |
24 | |
25 | import java.io.InputStream; |
26 | import java.io.IOException; |
27 | import java.io.EOFException; |
28 | |
29 | /** |
30 | Converts a stream containing the Cloudscape stored binary form |
31 | to one that just contains the application's data. |
32 | Simply read and save the length information. |
33 | */ |
34 | final class BinaryToRawStream |
35 | extends java.io.FilterInputStream |
36 | { |
37 | /** |
38 | * Length of the value represented by this stream. |
39 | * Set to -1 if the length is unknown. |
40 | */ |
41 | private int length; |
42 | |
43 | // used by caller to insure that parent can not be GC'd until this |
44 | // stream is no longer being used. |
45 | private Object parent; |
46 | |
47 | BinaryToRawStream(InputStream in, Object parent) |
48 | throws IOException |
49 | { |
50 | super(in); |
51 | |
52 | this.parent = parent; |
53 | |
54 | int bl = in.read(); |
55 | if (bl == -1) |
56 | throw new java.io.EOFException(); |
57 | |
58 | if ((bl & 0x80) != 0) |
59 | { |
60 | if (bl == 0xC0) |
61 | { |
62 | int v1 = in.read(); |
63 | int v2 = in.read(); |
64 | int v3 = in.read(); |
65 | int v4 = in.read(); |
66 | |
67 | if (v1 == -1 || v2 == -1 || v3 == -1 || v4 == -1) |
68 | throw new java.io.EOFException(); |
69 | length = (((v1 & 0xff) << 24) | |
70 | ((v2 & 0xff) << 16) | |
71 | ((v3 & 0xff) << 8) | |
72 | (v4 & 0xff)); |
73 | |
74 | } |
75 | else if (bl == 0xA0) |
76 | { |
77 | // read an unsigned short |
78 | int v1 = in.read(); |
79 | int v2 = in.read(); |
80 | if (v1 == -1 || v2 == -1) |
81 | throw new java.io.EOFException(); |
82 | length = (((v1 & 0xff) << 8) + (v2 & 0xff)); |
83 | |
84 | } |
85 | else |
86 | { |
87 | length = bl & 0x1F; |
88 | } |
89 | } |
90 | else |
91 | { |
92 | // old length in bits |
93 | int v2 = in.read(); |
94 | int v3 = in.read(); |
95 | int v4 = in.read(); |
96 | if (v2 == -1 || v3 == -1 || v4 == -1) |
97 | throw new java.io.EOFException(); |
98 | int lenInBits = (((bl & 0xff) << 24) | ((v2 & 0xff) << 16) | ((v3 & 0xff) << 8) | (v4 & 0xff)); |
99 | |
100 | length = lenInBits / 8; |
101 | if ((lenInBits % 8) != 0) |
102 | length++; |
103 | |
104 | // Signifies unknown length |
105 | if (length == 0) |
106 | length = -1; |
107 | } |
108 | } |
109 | |
110 | /** |
111 | * Return the length of the value in thie stream in bytes. |
112 | * If the value is unknown then -1 is returned. |
113 | */ |
114 | int getLength() |
115 | { |
116 | return length; |
117 | } |
118 | } |