KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > jdbc > BinaryToRawStream


1 /*
2
3    Derby - Class org.apache.derby.impl.jdbc.BinaryToRawStream
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
22 package org.apache.derby.impl.jdbc;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25
26 import java.io.InputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.EOFException JavaDoc;
29
30 /**
31     Converts a stream containing the Cloudscape stored binary form
32     to one that just contains the application's data.
33     Simply read and save the length information.
34 */

35 final class BinaryToRawStream
36 extends java.io.FilterInputStream JavaDoc
37 {
38     /**
39      * Length of the value represented by this stream.
40      * Set to -1 if the length is unknown.
41      */

42     private int length;
43
44     // used by caller to insure that parent can not be GC'd until this
45
// stream is no longer being used.
46
private Object JavaDoc parent;
47
48     BinaryToRawStream(InputStream JavaDoc in, Object JavaDoc parent)
49         throws IOException JavaDoc
50     {
51         super(in);
52
53         this.parent = parent;
54
55         int bl = in.read();
56         if (bl == -1)
57             throw new java.io.EOFException JavaDoc();
58
59         if ((bl & 0x80) != 0)
60         {
61             if (bl == 0xC0)
62             {
63                 int v1 = in.read();
64                 int v2 = in.read();
65                 int v3 = in.read();
66                 int v4 = in.read();
67
68                 if (v1 == -1 || v2 == -1 || v3 == -1 || v4 == -1)
69                     throw new java.io.EOFException JavaDoc();
70                 length = (((v1 & 0xff) << 24) |
71                           ((v2 & 0xff) << 16) |
72                           ((v3 & 0xff) << 8) |
73                            (v4 & 0xff));
74
75             }
76             else if (bl == 0xA0)
77             {
78                 // read an unsigned short
79
int v1 = in.read();
80                 int v2 = in.read();
81                 if (v1 == -1 || v2 == -1)
82                     throw new java.io.EOFException JavaDoc();
83                 length = (((v1 & 0xff) << 8) + (v2 & 0xff));
84
85             }
86             else
87             {
88                 length = bl & 0x1F;
89             }
90         }
91         else
92         {
93             // old length in bits
94
int v2 = in.read();
95             int v3 = in.read();
96             int v4 = in.read();
97             if (v2 == -1 || v3 == -1 || v4 == -1)
98                 throw new java.io.EOFException JavaDoc();
99             int lenInBits = (((bl & 0xff) << 24) | ((v2 & 0xff) << 16) | ((v3 & 0xff) << 8) | (v4 & 0xff));
100
101             length = lenInBits / 8;
102             if ((lenInBits % 8) != 0)
103                 length++;
104             
105             // Signifies unknown length
106
if (length == 0)
107                 length = -1;
108         }
109     }
110     
111     /**
112      * Return the length of the value in thie stream in bytes.
113      * If the value is unknown then -1 is returned.
114      */

115     int getLength()
116     {
117         return length;
118     }
119 }
120
Popular Tags