SQLBinary.java

Index Score
org.apache.derby.iapi.types
Apache Derby

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
JAVA0034JAVA0034 Missing braces in if statement
RETURNSNumber of return points from functions
INTERFACE_COMPLEXITYInterface complexity
COMPARISONSNumber of comparison operators
DECL_COMMENTSComments in declarations
CYCLOMATICCyclomatic complexity
LINESNumber of lines in the source file
JAVA0106JAVA0106 Unnecessary import from current package
COMMENTSComment lines
LOCLines of code
DOC_COMMENTNumber of javadoc comment lines
SIZESize of the file in bytes
BLOCKSNumber of blocks
EXEC_COMMENTSComments in executable code
JAVA0110JAVA0110 Incorrect javadoc: no @return tag
ELOCEffective lines of code
OPERATORSNumber of operators
PROGRAM_LENGTHHalstead program length
LOGICAL_LINESNumber of statements
JAVA0177JAVA0177 Variable declaration missing initializer
OPERANDSNumber of operands
JAVA0076JAVA0076 Use of magic number
FUNCTIONSNumber of function declarations
JAVA0115JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0145JAVA0145 Tab character used in source file
PARAMSNumber of formal parameter declarations
PROGRAM_VOCABHalstead program vocabulary
UNIQUE_OPERANDSNumber of unique operands
WHITESPACENumber of whitespace lines
UNIQUE_OPERATORSNumber of unique operators
EXITSProcedure exits
JAVA0080JAVA0080 Import declaration not used
JAVA0144JAVA0144 Line exceeds maximum M characters
JAVA0123JAVA0123 Use all three components of for loop
LINE_COMMENTNumber of line comments
JAVA0117JAVA0117 Missing javadoc: method 'method'
BLOCK_COMMENTNumber of block comment lines
PROGRAM_VOLUMEHalstead program volume
JAVA0136JAVA0136 N methods defined in class (maximum: M)
JAVA0126JAVA0126 Method declares unchecked exception in throws
JAVA0256JAVA0256 Assignment of external collection/array to field
JAVA0270JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
NEST_DEPTHMaximum nesting depth
JAVA0108JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
/* Derby - Class org.apache.derby.iapi.types.SQLBinary Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package org.apache.derby.iapi.types; import org.apache.derby.iapi.reference.SQLState; import org.apache.derby.iapi.services.io.ArrayInputStream; import org.apache.derby.iapi.services.io.FormatableBitSet; import org.apache.derby.iapi.services.io.NewByteArrayInputStream; import org.apache.derby.iapi.types.DataTypeDescriptor; import org.apache.derby.iapi.types.DataValueDescriptor; import org.apache.derby.iapi.types.TypeId; import org.apache.derby.iapi.types.BitDataValue; import org.apache.derby.iapi.types.DataValueDescriptor; import org.apache.derby.iapi.types.ConcatableDataValue; import org.apache.derby.iapi.types.VariableSizeDataValue; import org.apache.derby.iapi.error.StandardException; import org.apache.derby.iapi.services.io.FormatIdUtil; import org.apache.derby.iapi.services.io.StoredFormatIds; import org.apache.derby.iapi.services.io.StreamStorable; import org.apache.derby.iapi.services.io.FormatIdInputStream; import org.apache.derby.iapi.services.sanity.SanityManager; import org.apache.derby.iapi.types.BooleanDataValue; import org.apache.derby.iapi.types.StringDataValue; import org.apache.derby.iapi.types.NumberDataValue; import org.apache.derby.iapi.services.cache.ClassSize; import org.apache.derby.iapi.util.StringUtil; import org.apache.derby.iapi.types.SQLInteger; import java.io.ObjectOutput; import java.io.ObjectInput; import java.io.IOException; import java.io.InputStream; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.PreparedStatement; /** * SQLBinary is the abstract class for the binary datatypes. * <UL> * <LI> CHAR FOR BIT DATA * <LI> VARCHAR FOR BIT DATA * <LI> LONG VARCHAR * <LI> BLOB * </UL> <P> Format : <encoded length><raw data> <BR> Length is encoded to support Cloudscape 5.x databases where the length was stored as the number of bits. The first bit of the first byte indicates if the format is an old (Cloudscape 5.x) style or a new Derby style. Derby then uses the next two bits to indicate how the length is encoded. <BR> <encoded length> is one of N styles. <UL> <LI> (5.x format zero) 4 byte Java format integer value 0 - either <raw data> is 0 bytes/bits or an unknown number of bytes. <LI> (5.x format bits) 4 byte Java format integer value >0 (positive) - number of bits in <raw data>, number of bytes in <raw data> is the minimum number of bytes required to store the number of bits. <LI> (Derby format) 1 byte encoded length (0 <= L <= 31) - number of bytes of <raw data> - encoded = 0x80 & L <LI> (Derby format) 3 byte encoded length (32 <= L < 64k) - number of bytes of <raw data> - encoded = 0xA0 <L as Java format unsigned short> <LI> (Derby format) 5 byte encoded length (64k <= L < 2G) - number of bytes of <raw data> - encoded = 0xC0 <L as Java format integer> <LI> (future) to be determined L >= 2G - encoded 0xE0 <encoding of L to be determined> (0xE0 is an esacape to allow any number of arbitary encodings in the future). </UL> <BR> When the value was written from a byte array the Derby encoded byte length format was always used from Derby 10.0 onwards (ie. all open source versions). <BR> When the value was written from a stream (e.g. PreparedStatement.setBinaryStream) then the Cloudscape '5.x format zero' was used by 10.0 and 10.1. The was due to the class RawToBinaryFormatStream always writing four zero bytes for the length before the data. <BR> The Cloudscape '5.x format bits' format I think was never used by Derby. */ abstract class SQLBinary extends DataType implements BitDataValue { static final byte PAD = (byte) 0x20; private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLBinary.class); public int estimateMemoryUsage() { if (dataValue == null) { if (streamValueLength>=0) { return BASE_MEMORY_USAGE + streamValueLength; } else { return getMaxMemoryUsage(); } } else { return BASE_MEMORY_USAGE + dataValue.length; } } // end of estimateMemoryUsage /** * Return max memory usage for a SQL Binary */ abstract int getMaxMemoryUsage(); /* * object state */ byte[] dataValue; /** * Value as a stream, this stream represents the on-disk * format of the value. That is it has length information * encoded in the first fe bytes. */ InputStream stream; /** Length of the value in bytes when this value is set as a stream. Represents the length of the value itself and not the length of the stream which contains this length encoded as the first few bytes. If the value of the stream is unknown then this will be set to -1. If this value is not set as a stream then this value should be ignored. */ int streamValueLength; /** Create a binary value set to NULL */ SQLBinary() { } SQLBinary(byte[] val) { dataValue = val; } public final void setValue(byte[] theValue) { dataValue = theValue; stream = null; streamValueLength = -1; } /** * Used by JDBC -- string should not contain * SQL92 formatting. * * @exception StandardException Thrown on error */ public final String getString() throws StandardException { if (getValue() == null) return null; else if (dataValue.length * 2 < 0) //if converted to hex, length exceeds max int { throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(), "", String.valueOf(Integer.MAX_VALUE)); } else { return org.apache.derby.iapi.util.StringUtil.toHexString(dataValue, 0, dataValue.length); } } /** * @exception StandardException Thrown on error */ public final InputStream getStream() { return (stream); } /** * * @exception StandardException Thrown on error */ public final byte[] getBytes() throws StandardException { return getValue(); } byte[] getValue() throws StandardException { try { if ((dataValue == null) && (stream != null)) { if (stream instanceof FormatIdInputStream) { readExternal((FormatIdInputStream) stream); } else { readExternal(new FormatIdInputStream(stream)); } stream = null; streamValueLength = -1; } } catch (IOException ioe) { throwStreamingIOException(ioe); } return dataValue; } /** * length in bytes * * @exception StandardException Thrown on error */ public final int getLength() throws StandardException { if (stream != null) { if (streamValueLength != -1) return streamValueLength; else if (stream instanceof Resetable){ try { // If we have the stream length encoded. // just read that. streamValueLength = readBinaryLength((ObjectInput) stream); if (streamValueLength != 0) return streamValueLength; // Otherwise we will have to read the whole stream. for (;;) { long skipsize = stream.skip(Integer.MAX_VALUE); streamValueLength += skipsize; if (stream.read() == -1) break; else streamValueLength++; } return streamValueLength; } catch (IOException ioe) { throwStreamingIOException(ioe); } finally { try { ((Resetable) stream).resetStream(); } catch (IOException ioe) { throwStreamingIOException(ioe); } } } } byte[] bytes = getBytes(); return (bytes == null) ? 0 : bytes.length; } private void throwStreamingIOException(IOException ioe) throws StandardException { throw StandardException. newException(SQLState.LANG_STREAMING_COLUMN_I_O_EXCEPTION, ioe, getTypeName()); } /* * Storable interface, implies Externalizable, TypedFormat */ /** * see if the Bit value is null. * @see org.apache.derby.iapi.services.io.Storable#isNull */ public final boolean isNull() { return (dataValue == null) && (stream == null); } /** Write the value out from the byte array (not called if null) using the 8.1 encoding. * @exception IOException io exception */ public final void writeExternal(ObjectOutput out) throws IOException { int len = dataValue.length; if (len <= 31) { out.write((byte) (0x80 | (len & 0xff))); } else if (len <= 0xFFFF) { out.write((byte) 0xA0); out.writeShort((short) len); } else { out.write((byte) 0xC0); out.writeInt(len); } out.write(dataValue, 0, dataValue.length); } /** * delegated to bit * * @exception IOException io exception * @exception ClassNotFoundException class not found */ public final void readExternal(ObjectInput in) throws IOException { // need to clear stream first, in case this object is reused, and // stream is set by previous use. Track 3794. stream = null; streamValueLength = -1; int len = SQLBinary.readBinaryLength(in); if (len != 0) { dataValue = new byte[len]; in.readFully(dataValue); } else { readFromStream((InputStream) in); } } public final void readExternalFromArray(ArrayInputStream in) throws IOException { // need to clear stream first, in case this object is reused, and // stream is set by previous use. Track 3794. stream = null; streamValueLength = -1; int len = SQLBinary.readBinaryLength(in); if (len != 0) { dataValue = new byte[len]; in.readFully(dataValue); } else { readFromStream(in); } } /** * Read the encoded length of the value from the on-disk format. * * @see SQLBinary */ private static int readBinaryLength(ObjectInput in) throws IOException { int bl = in.read(); if (bl == -1) throw new java.io.EOFException(); byte li = (byte) bl; int len; if ((li & ((byte) 0x80)) != 0) { if (li == ((byte) 0xC0)) { len = in.readInt(); } else if (li == ((byte) 0xA0)) { len = in.readUnsignedShort(); } else { len = li & 0x1F; } } else { // old length in bits int v2 = in.read(); int v3 = in.read(); int v4 = in.read(); if (v2 == -1 || v3 == -1 || v4 == -1) throw new java.io.EOFException(); int lenInBits = (((bl & 0xff) << 24) | ((v2 & 0xff) << 16) | ((v3 & 0xff) << 8) | (v4 & 0xff)); len = lenInBits / 8; if ((lenInBits % 8) != 0) len++; } return len; } /** * Read the value from an input stream. The length * encoded in the input stream has already been read * and determined to be unknown. */ private void readFromStream(InputStream in) throws IOException { dataValue = null; // allow gc of the old value before the new. byte[] tmpData = new byte[32 * 1024]; int off = 0; for (;;) { int len = in.read(tmpData, off, tmpData.length - off); if (len == -1) break; off += len; int available = Math.max(1, in.available()); int extraSpace = available - (tmpData.length - off); if (extraSpace > 0) { // need to grow the array int size = tmpData.length * 2; if (extraSpace > tmpData.length) size += extraSpace; byte[] grow = new byte[size]; System.arraycopy(tmpData, 0, grow, 0, off); tmpData = grow; } } dataValue = new byte[off]; System.arraycopy(tmpData, 0, dataValue, 0, off); } /** * @see org.apache.derby.iapi.services.io.Storable#restoreToNull */ public final void restoreToNull() { dataValue = null; stream = null; streamValueLength = -1; } /** @exception StandardException thrown on error */ public final boolean compare(int op, DataValueDescriptor other, boolean orderedNulls, boolean unknownRV) throws StandardException { if (!orderedNulls) // nulls are unordered { if (SanityManager.DEBUG) { int otherTypeFormatId = other.getTypeFormatId(); if (!((StoredFormatIds.SQL_BIT_ID == otherTypeFormatId) || (StoredFormatIds.SQL_VARBIT_ID == otherTypeFormatId) || (StoredFormatIds.SQL_LONGVARBIT_ID == otherTypeFormatId) || (StoredFormatIds.SQL_CHAR_ID == otherTypeFormatId) || (StoredFormatIds.SQL_VARCHAR_ID == otherTypeFormatId) || (StoredFormatIds.SQL_LONGVARCHAR_ID == otherTypeFormatId) || ((StoredFormatIds.SQL_BLOB_ID == otherTypeFormatId) && (StoredFormatIds.SQL_BLOB_ID == getTypeFormatId())) )) SanityManager.THROWASSERT( "Some fool passed in a "+ other.getClass().getName() + ", " + otherTypeFormatId + " to SQLBinary.compare()"); } String otherString = other.getString(); if (this.getString() == null || otherString == null) return unknownRV; } /* Do the comparison */ return super.compare(op, other, orderedNulls, unknownRV); } /** @exception StandardException thrown on error */ public final int compare(DataValueDescriptor other) throws StandardException { /* Use compare method from dominant type, negating result * to reflect flipping of sides. */ if (typePrecedence() < other.typePrecedence()) { return - (other.compare(this)); } /* ** By convention, nulls sort High, and null == null */ if (this.isNull() || other.isNull()) { if (!isNull()) return -1; if (!other.isNull()) return 1; return 0; // both null } return SQLBinary.compare(getBytes(), other.getBytes()); } /* * CloneableObject interface */ /** From CloneableObject * Shallow clone a StreamStorable without objectifying. This is used to avoid * unnecessary objectifying of a stream object. The only difference of this method * from getClone is this method does not objectify a stream. beetle 4896 */ public final Object cloneObject() { if (stream == null) return getClone(); SQLBinary self = (SQLBinary) getNewNull(); self.setValue(stream, streamValueLength); return self; } /* * DataValueDescriptor interface */ /** @see DataValueDescriptor#getClone */ public final DataValueDescriptor getClone() { try { DataValueDescriptor cloneDVD = getNewNull(); cloneDVD.setValue(getValue()); return cloneDVD; } catch (StandardException se) { if (SanityManager.DEBUG) SanityManager.THROWASSERT("Unexpected exception", se); return null; } } /* * DataValueDescriptor interface */ /* * StreamStorable interface : */ public final InputStream returnStream() { return stream; } /** * Set me to the value represented by this stream. * The format of the stream is the on-disk format * described in this class's javadoc. That is the * length is encoded in the first few bytes of the * stream. */ public final void setStream(InputStream newStream) { this.dataValue = null; this.stream = newStream; streamValueLength = -1; } public final void loadStream() throws StandardException { getValue(); } /* * class interface */ boolean objectNull(Object o) { if (o == null) { setToNull(); return true; } return false; } /** * Set the value from the stream which is in the on-disk format. * @param theStream On disk format of the stream * @param valueLength length of the logical value in bytes. */ public final void setValue(InputStream theStream, int valueLength) { dataValue = null; stream = theStream; this.streamValueLength = valueLength; } protected final void setFrom(DataValueDescriptor theValue) throws StandardException { if (theValue instanceof SQLBinary) { SQLBinary theValueBinary = (SQLBinary) theValue; dataValue = theValueBinary.dataValue; stream = theValueBinary.stream; streamValueLength = theValueBinary.streamValueLength; } else { setValue(theValue.getBytes()); } } /* ** SQL Operators */ /** * The = operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the = * @param right The value on the right side of the = * is not. * @return A SQL boolean value telling whether the two parameters are equal * * @exception StandardException Thrown on error */ public final BooleanDataValue equals(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { boolean isEqual; if (left.isNull() || right.isNull()) { isEqual = false; } else { isEqual = SQLBinary.compare(left.getBytes(), right.getBytes()) == 0; } return SQLBoolean.truthValue(left, right, isEqual); } /** * The <> operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the <> * @param right The value on the right side of the <> * * @return A SQL boolean value telling whether the two parameters * are not equal * * @exception StandardException Thrown on error */ public final BooleanDataValue notEquals(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { boolean isNotEqual; if (left.isNull() || right.isNull()) { isNotEqual = false; } else { isNotEqual = SQLBinary.compare(left.getBytes(), right.getBytes()) != 0; } return SQLBoolean.truthValue(left, right, isNotEqual); } /** * The < operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the < * @param right The value on the right side of the < * * @return A SQL boolean value telling whether the first operand is * less than the second operand * * @exception StandardException Thrown on error */ public final BooleanDataValue lessThan(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { boolean isLessThan; if (left.isNull() || right.isNull()) { isLessThan = false; } else { isLessThan = SQLBinary.compare(left.getBytes(), right.getBytes()) < 0; } return SQLBoolean.truthValue(left, right, isLessThan); } /** * The > operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the > * @param right The value on the right side of the > * * @return A SQL boolean value telling whether the first operand is * greater than the second operand * * @exception StandardException Thrown on error */ public final BooleanDataValue greaterThan(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { boolean isGreaterThan = false; if (left.isNull() || right.isNull()) { isGreaterThan = false; } else { isGreaterThan = SQLBinary.compare(left.getBytes(), right.getBytes()) > 0; } return SQLBoolean.truthValue(left, right, isGreaterThan); } /** * The <= operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the <= * @param right The value on the right side of the <= * * @return A SQL boolean value telling whether the first operand is * less than or equal to the second operand * * @exception StandardException Thrown on error */ public final BooleanDataValue lessOrEquals(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { boolean isLessEquals = false; if (left.isNull() || right.isNull()) { isLessEquals = false; } else { isLessEquals = SQLBinary.compare(left.getBytes(), right.getBytes()) <= 0; } return SQLBoolean.truthValue(left, right, isLessEquals); } /** * The >= operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the >= * @param right The value on the right side of the >= * * @return A SQL boolean value telling whether the first operand is * greater than or equal to the second operand * * @exception StandardException Thrown on error */ public final BooleanDataValue greaterOrEquals(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { boolean isGreaterEquals = false; if (left.isNull() || right.isNull()) { isGreaterEquals = false; } else { isGreaterEquals = SQLBinary.compare(left.getBytes(), right.getBytes()) >= 0; } return SQLBoolean.truthValue(left, right, isGreaterEquals); } /** * * This method implements the char_length function for bit. * * @param result The result of a previous call to this method, null * if not called yet * * @return A SQLInteger containing the length of the char value * * @exception StandardException Thrown on error * * @see ConcatableDataValue#charLength */ public final NumberDataValue charLength(NumberDataValue result) throws StandardException { if (result == null) { result = new SQLInteger(); } if (this.isNull()) { result.setToNull(); return result; } result.setValue(getValue().length); return result; } /** * @see BitDataValue#concatenate * * @exception StandardException Thrown on error */ public final BitDataValue concatenate( BitDataValue left, BitDataValue right, BitDataValue result) throws StandardException { if (left.isNull() || right.isNull()) { result.setToNull(); return result; } byte[] leftData = left.getBytes(); byte[] rightData = right.getBytes(); byte[] concatData = new byte[leftData.length + rightData.length]; System.arraycopy(leftData, 0, concatData, 0, leftData.length); System.arraycopy(rightData, 0, concatData, leftData.length, rightData.length); result.setValue(concatData); return result; } /** * The SQL substr() function. * * @param start Start of substr * @param length Length of substr * @param result The result of a previous call to this method, * null if not called yet. * @param maxLen Maximum length of the result * * @return A ConcatableDataValue containing the result of the substr() * * @exception StandardException Thrown on error */ public final ConcatableDataValue substring( NumberDataValue start, NumberDataValue length, ConcatableDataValue result, int maxLen) throws StandardException { int startInt; int lengthInt; BitDataValue varbitResult; if (result == null) { result = new SQLVarbit(); } varbitResult = (BitDataValue) result; /* The result is null if the receiver (this) is null or if the length is negative. * Oracle docs don't say what happens if the start position or the length is a usernull. * We will return null, which is the only sensible thing to do. * (If the user did not specify a length then length is not a user null.) */ if (this.isNull() || start.isNull() || (length != null && length.isNull())) { varbitResult.setToNull(); return varbitResult; } startInt = start.getInt(); // If length is not specified, make it till end of the string if (length != null) { lengthInt = length.getInt(); } else lengthInt = getLength() - startInt + 1; /* DB2 Compatibility: Added these checks to match DB2. We currently enforce these * limits in both modes. We could do these checks in DB2 mode only, if needed, so * leaving earlier code for out of range in for now, though will not be exercised */ if ((startInt <= 0 || lengthInt < 0 || startInt > getLength() || lengthInt > getLength() - startInt + 1)) throw StandardException.newException(SQLState.LANG_SUBSTR_START_OR_LEN_OUT_OF_RANGE); // Return null if length is non-positive if (lengthInt < 0) { varbitResult.setToNull(); return varbitResult; } /* If startInt < 0 then we count from the right of the string */ if (startInt < 0) { startInt += getLength(); if (startInt < 0) { lengthInt += startInt; startInt = 0; } if (lengthInt + startInt > 0) { lengthInt += startInt; } else { lengthInt = 0; } } else if (startInt > 0) { /* java substr() is 0 based */ startInt--; } /* Oracle docs don't say what happens if the window is to the * left of the string. Return "" if the window * is to the left or right or if the length is 0. */ if (lengthInt == 0 || lengthInt <= 0 - startInt || startInt > getLength()) { varbitResult.setValue(new byte[0]); return varbitResult; } if (lengthInt >= getLength() - startInt) { byte[] substring = new byte[dataValue.length - startInt]; System.arraycopy(dataValue, startInt, substring, 0, substring.length); varbitResult.setValue(substring); } else { byte[] substring = new byte[lengthInt]; System.arraycopy(dataValue, startInt, substring, 0, substring.length); varbitResult.setValue(substring); } return varbitResult; } /** Host variables are rejected if their length is bigger than the declared length, regardless of if the trailing bytes are the pad character. @exception StandardException Variable is too big. */ public final void checkHostVariable(int declaredLength) throws StandardException { // stream length checking occurs at the JDBC layer int variableLength = -1; if (stream == null) { if (dataValue != null) variableLength = dataValue.length; } else { variableLength = streamValueLength; } if (variableLength != -1 && variableLength > declaredLength) throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(), "(Binary data value not displayed)", String.valueOf(declaredLength)); } /* * String display of value */ public final String toString() { if (dataValue == null) { if (stream == null) { return "NULL"; } else { if (SanityManager.DEBUG) SanityManager.THROWASSERT( "value is null, stream is not null"); return ""; } } else { return org.apache.derby.iapi.util.StringUtil.toHexString(dataValue, 0, dataValue.length); } } /* * Hash code */ public final int hashCode() { try { if (getValue() == null) { return 0; } } catch (StandardException se) { if (SanityManager.DEBUG) SanityManager.THROWASSERT("Unexpected exception", se); return 0; } /* Hash code is simply the sum of all of the bytes */ byte[] bytes = dataValue; int hashcode = 0; // Build the hash code for (int index = 0 ; index < bytes.length; index++) { byte bv = bytes[index]; if (bv != SQLBinary.PAD) hashcode += bytes[index]; } return hashcode; } private static int compare(byte[] left, byte[] right) { int minLen = left.length; byte[] longer = right; if (right.length < minLen) { minLen = right.length; longer = left; } for (int i = 0; i < minLen; i++) { int lb = left[i] & 0xff; int rb = right[i] & 0xff; if (lb == rb) continue; return lb - rb; } // complete match on all the bytes for the smallest value. // if the longer value is all pad characters // then the values are equal. for (int i = minLen; i < longer.length; i++) { byte nb = longer[i]; if (nb == SQLBinary.PAD) continue; // longer value is bigger. if (left == longer) return 1; return -1; } return 0; } /** Adding this method to ensure that super class' setInto method doesn't get called * that leads to the violation of JDBC spec( untyped nulls ) when batching is turned on. */ public void setInto(PreparedStatement ps, int position) throws SQLException, StandardException { ps.setBytes(position, getBytes()); } /** * Gets a trace representation for debugging. * * @return a trace representation of this SQL DataType. */ public final String getTraceString() throws StandardException { // Check if the value is SQL NULL. if (isNull()) { return "NULL"; } // Check if we have a stream. if (getStream() != null) { return (getTypeName() + "(" + getStream().toString() + ")"); } return (getTypeName() + ":Length=" + getLength()); } }

The table below shows all metrics for SQLBinary.java.

MetricValueDescription
BLOCKS126.00Number of blocks
BLOCK_COMMENT73.00Number of block comment lines
COMMENTS339.00Comment lines
COMMENT_DENSITY 0.72Comment density
COMPARISONS111.00Number of comparison operators
CYCLOMATIC147.00Cyclomatic complexity
DECL_COMMENTS44.00Comments in declarations
DOC_COMMENT247.00Number of javadoc comment lines
ELOC468.00Effective lines of code
EXEC_COMMENTS24.00Comments in executable code
EXITS58.00Procedure exits
FUNCTIONS43.00Number of function declarations
HALSTEAD_DIFFICULTY139.99Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY183.00Interface complexity
JAVA0001 0.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 0.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 0.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 1.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA0007 0.00JAVA0007 Should not declare public field
JAVA0008 0.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 0.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 0.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 0.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 0.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA003426.00JAVA0034 Missing braces in if statement
JAVA0035 0.00JAVA0035 Missing braces in for statement
JAVA0036 0.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 0.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 0.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA0049 0.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 0.00JAVA0058 Constructor calls super()
JAVA0059 0.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 0.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 1.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 0.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA0067 0.00JAVA0067 Array descriptor on identifier name
JAVA0068 0.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 0.00JAVA0073 Integer division in floating-point context
JAVA0074 0.00JAVA0074 Use of Object.notify()
JAVA0075 0.00JAVA0075 Method parameter hides field
JAVA007616.00JAVA0076 Use of magic number
JAVA0077 0.00JAVA0077 Private field not used in declaring class
JAVA0078 0.00JAVA0078 Floating point values compared with ==
JAVA0079 0.00JAVA0079 Use of instance to reference static member
JAVA0080 9.00JAVA0080 Import declaration not used
JAVA0081 0.00JAVA0081 Boolean literal in comparison
JAVA0082 0.00JAVA0082 Unnecessary widening cast
JAVA0083 0.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 0.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 0.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 0.00JAVA0096 Field in nested class hides outer field
JAVA0098 1.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 0.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 1.00JAVA0105 Duplicate import declaration
JAVA010611.00JAVA0106 Unnecessary import from current package
JAVA0108 2.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 0.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA011012.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 0.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 2.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 1.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 1.00JAVA0114 Incorrect javadoc: no @version tag
JAVA0115 5.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0116 0.00JAVA0116 Missing javadoc: field 'field'
JAVA0117 0.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 0.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 0.00JAVA0119 Control variable changed within body of for loop
JAVA0123 2.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 0.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 0.00JAVA0128 Public constructor in non-public class
JAVA0130 0.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 0.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 1.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 0.00JAVA0137 Non-abstract class missing constructor
JAVA0138 0.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 0.00JAVA0141 Unnecessary modifier for method in interface
JAVA0143 0.00JAVA0143 Synchronized method
JAVA0144 3.00JAVA0144 Line exceeds maximum M characters
JAVA01452094.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 0.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA0166 0.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 0.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 0.00JAVA0171 Unused local variable
JAVA0173 0.00JAVA0173 Unused method parameter
JAVA0174 0.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA0177 7.00JAVA0177 Variable declaration missing initializer
JAVA0179 0.00JAVA0179 Local variable hides visible field
JAVA0233 0.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 0.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 0.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 0.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 2.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 0.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 1.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 0.00JAVA0264 Integer math in long context - check for overflow
JAVA0265 0.00JAVA0265 Use of Throwable.printStackTrace()
JAVA0266 0.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA0270 1.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 0.00JAVA0272 Thread.run() called
JAVA0273 0.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 0.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 0.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 0.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 0.00JAVA0283 Control variable not updated in loop body
JAVA0284 0.00JAVA0284 Explicit garbage collection
JAVA0285 0.00JAVA0285 Dereference of potentially null variable
JAVA0286 0.00JAVA0286 Dereference of null variable
JAVA0287 0.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES1183.00Number of lines in the source file
LINE_COMMENT19.00Number of line comments
LOC689.00Lines of code
LOGICAL_LINES266.00Number of statements
LOOPS 5.00Number of loops
NEST_DEPTH 5.00Maximum nesting depth
OPERANDS1191.00Number of operands
OPERATORS2451.00Number of operators
PARAMS43.00Number of formal parameter declarations
PROGRAM_LENGTH3642.00Halstead program length
PROGRAM_VOCAB352.00Halstead program vocabulary
PROGRAM_VOLUME 0.00Halstead program volume
RETURNS140.00Number of return points from functions
SIZE29309.00Size of the file in bytes
UNIQUE_OPERANDS285.00Number of unique operands
UNIQUE_OPERATORS67.00Number of unique operators
WHITESPACE155.00Number of whitespace lines