1 24 25 package com.mckoi.database; 26 27 import java.io.InputStream ; 28 import java.io.BufferedInputStream ; 29 import java.io.IOException ; 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 41 42 public class TBinaryType extends TType { 43 44 static final long serialVersionUID = 5141996433600529406L; 45 46 50 private int max_size; 51 52 55 public TBinaryType(int sql_type, int max_size) { 56 super(sql_type); 57 this.max_size = max_size; 58 } 59 60 63 public int getMaximumSize() { 64 return max_size; 65 } 66 67 69 74 static int compareBlobs(BlobAccessor blob1, BlobAccessor blob2) { 75 int c = blob1.length() - blob2.length(); 77 if (c != 0) { 78 return c; 79 } 80 else { 81 85 int len = blob1.length(); 86 87 InputStream b1 = blob1.getInputStream(); 88 InputStream b2 = blob2.getInputStream(); 89 try { 90 BufferedInputStream bin1 = new BufferedInputStream (b1); 91 BufferedInputStream bin2 = new BufferedInputStream (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 e) { 103 throw new RuntimeException ("IO Error when comparing blobs: " + 104 e.getMessage()); 105 } 106 } 107 } 108 109 111 public boolean comparableTypes(TType type) { 112 return (type instanceof BlobAccessor); 113 } 114 115 public int compareObs(Object ob1, Object 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 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 javaClass() { 141 return BlobAccessor.class; 142 } 143 144 } 145 | Popular Tags |