KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > TBinaryType


1 /**
2  * com.mckoi.database.TBinaryType 31 Jul 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.io.InputStream JavaDoc;
28 import java.io.BufferedInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import com.mckoi.database.global.SQLTypes;
32 import com.mckoi.database.global.ByteLongObject;
33 import com.mckoi.database.global.BlobRef;
34 import com.mckoi.database.global.BlobAccessor;
35
36 /**
37  * An implementation of TType for a binary block of data.
38  *
39  * @author Tobias Downer
40  */

41
42 public class TBinaryType extends TType {
43
44   static final long serialVersionUID = 5141996433600529406L;
45
46   /**
47    * This constrained size of the binary block of data or -1 if there is no
48    * size limit.
49    */

50   private int max_size;
51
52   /**
53    * Constructs the type.
54    */

55   public TBinaryType(int sql_type, int max_size) {
56     super(sql_type);
57     this.max_size = max_size;
58   }
59
60   /**
61    * Returns the maximum size of this binary type.
62    */

63   public int getMaximumSize() {
64     return max_size;
65   }
66
67   // ---------- Static utility method for comparing blobs ----------
68

69   /**
70    * Utility method for comparing one blob with another. Uses the
71    * BlobAccessor interface to compare the blobs. This will collate larger
72    * blobs higher than smaller blobs.
73    */

74   static int compareBlobs(BlobAccessor blob1, BlobAccessor blob2) {
75     // We compare smaller sized blobs before larger sized blobs
76
int c = blob1.length() - blob2.length();
77     if (c != 0) {
78       return c;
79     }
80     else {
81       // Size of the blobs are the same, so find the first non equal byte in
82
// the byte array and return the difference between the two. eg.
83
// compareTo({ 0, 0, 0, 1 }, { 0, 0, 0, 3 }) == -3
84

85       int len = blob1.length();
86
87       InputStream JavaDoc b1 = blob1.getInputStream();
88       InputStream JavaDoc b2 = blob2.getInputStream();
89       try {
90         BufferedInputStream JavaDoc bin1 = new BufferedInputStream JavaDoc(b1);
91         BufferedInputStream JavaDoc bin2 = new BufferedInputStream JavaDoc(b2);
92         while (len > 0) {
93           c = bin1.read() - bin2.read();
94           if (c != 0) {
95             return c;
96           }
97           --len;
98         }
99       
100         return 0;
101       }
102       catch (IOException JavaDoc e) {
103         throw new RuntimeException JavaDoc("IO Error when comparing blobs: " +
104                                    e.getMessage());
105       }
106     }
107   }
108   
109   // ---------- Implemented from TType ----------
110

111   public boolean comparableTypes(TType type) {
112     return (type instanceof BlobAccessor);
113   }
114   
115   public int compareObs(Object JavaDoc ob1, Object JavaDoc ob2) {
116     if (ob1 == ob2) {
117       return 0;
118     }
119
120     BlobAccessor blob1 = (BlobAccessor) ob1;
121     BlobAccessor blob2 = (BlobAccessor) ob2;
122     
123     return compareBlobs(blob1, blob2);
124   }
125   
126   public int calculateApproximateMemoryUse(Object JavaDoc ob) {
127     if (ob != null) {
128       if (ob instanceof BlobRef) {
129         return 256;
130       }
131       else {
132         return ((ByteLongObject) ob).length() + 24;
133       }
134     }
135     else {
136       return 32;
137     }
138   }
139   
140   public Class JavaDoc javaClass() {
141     return BlobAccessor.class;
142   }
143
144 }
145
Popular Tags