1 21 22 package org.apache.derby.iapi.types; 23 24 import org.apache.derby.iapi.types.DataTypeDescriptor; 25 import org.apache.derby.iapi.types.DataValueDescriptor; 26 import org.apache.derby.iapi.types.TypeId; 27 import org.apache.derby.iapi.types.BitDataValue; 28 import org.apache.derby.iapi.types.DataValueDescriptor; 29 import org.apache.derby.iapi.reference.SQLState; 30 import org.apache.derby.iapi.reference.Limits; 31 import org.apache.derby.iapi.error.StandardException; 32 33 import org.apache.derby.iapi.types.Orderable; 34 35 import org.apache.derby.iapi.services.io.FormatIdUtil; 36 import org.apache.derby.iapi.services.io.StoredFormatIds; 37 38 import org.apache.derby.iapi.services.sanity.SanityManager; 39 40 import org.apache.derby.iapi.types.BooleanDataValue; 41 import org.apache.derby.iapi.types.StringDataValue; 42 import org.apache.derby.iapi.types.NumberDataValue; 43 44 import org.apache.derby.iapi.services.io.FormatableBitSet; 45 46 import java.io.ObjectOutput ; 47 import java.io.ObjectInput ; 48 import java.io.IOException ; 49 import java.sql.Blob ; 50 import java.sql.ResultSet ; 51 import java.sql.SQLException ; 52 import java.sql.PreparedStatement ; 53 54 66 public class SQLBlob extends SQLBinary 67 { 68 69 72 public SQLBlob() 73 { 74 } 75 76 public SQLBlob(byte[] val) 77 { 78 super(val); 79 } 80 81 public String getTypeName() 82 { 83 return TypeId.BLOB_NAME; 84 } 85 86 89 int getMaxMemoryUsage() 90 { 91 return Limits.DB2_LOB_MAXWIDTH; 92 } 93 94 97 public DataValueDescriptor getNewNull() 98 { 99 return new SQLBlob(); 100 } 101 102 114 115 public void normalize( 116 DataTypeDescriptor desiredType, 117 DataValueDescriptor source) 118 throws StandardException 119 { 120 setValue(source); 121 setWidth(desiredType.getMaximumWidth(), 0, true); 122 } 123 124 public void setWidth(int desiredWidth, int desiredScale, boolean errorOnTrunc) 136 throws StandardException 137 { 138 139 if (isNull()) 141 return; 142 143 if (isLengthLess()) { 146 return; 147 } 148 149 int sourceWidth = getLength(); 150 151 if (sourceWidth > desiredWidth) { 153 if (errorOnTrunc) 154 throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(), 155 "XXXX", 156 String.valueOf(desiredWidth)); 157 else { 158 161 162 163 byte[] shrunkData = new byte[desiredWidth]; 164 System.arraycopy(getBytes(), 0, shrunkData, 0, desiredWidth); 165 dataValue = shrunkData; 166 } 167 } 168 } 169 170 175 public final String getTraceString() throws StandardException { 176 if (isNull()) { 178 return "NULL"; 179 } 180 181 if (getStream() != null) { 183 return ("BLOB(" + getStream().toString() + ")"); 184 } 185 186 return ("BLOB(" + getLength() + ")"); 187 } 188 189 194 public int getTypeFormatId() 195 { 196 return StoredFormatIds.SQL_BLOB_ID; 197 } 198 199 205 public void setValueFromResultSet(ResultSet resultSet, int colNumber, 206 boolean isNullable) 207 throws SQLException , StandardException 208 { 209 Blob blob = resultSet.getBlob(colNumber); 210 if (blob == null) 211 setToNull(); 212 else 213 setObject(blob); 214 } 215 216 217 218 221 222 223 public int typePrecedence() 224 { 225 return TypeId.BLOB_PRECEDENCE; } 227 228 public void setInto(PreparedStatement ps, int position) 229 throws SQLException , StandardException 230 { 231 if (isNull()) { 232 ps.setBlob(position, null); 233 return; 234 } 235 236 ps.setBytes(position, getBytes()); 238 } 239 240 243 final void setObject(Object theValue) 244 throws StandardException 245 { 246 Blob vb = (Blob ) theValue; 247 248 try { 249 long vbl = vb.length(); 250 if (vbl < 0L || vbl > Integer.MAX_VALUE) 251 throw this.outOfRange(); 252 253 setValue(new RawToBinaryFormatStream( 254 vb.getBinaryStream(), (int) vbl), 255 (int) vbl); 256 257 } catch (SQLException e) { 258 throw dataTypeConversion("DAN-438-tmp"); 259 } 260 } 261 262 268 private final boolean isLengthLess() { 269 return (stream != null && streamValueLength < 0); 270 } 271 } 272 273 274 | Popular Tags |