⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlchar.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		follow. This value is the number of bytes actually written out, 		not the length of the string. Following the length, each character 		of the string is output, in sequence, using the UTF-8 encoding 		for the character. 		@param      str   a string to be written.		@exception  IOException  if an I/O error occurs.		@since      JDK1.0	  @exception IOException thrown by writeUTF	  @see java.io.DataInputStream	*/	public void writeExternal(ObjectOutput out) throws IOException	{		// never called when value is null		if (SanityManager.DEBUG)			SanityManager.ASSERT(!isNull());		String lvalue = null;		char[] data = null;		int strlen = rawLength;		boolean isRaw;		if (strlen < 0) {			lvalue = value;			strlen = lvalue.length();			isRaw = false;		} else {			data = rawData;			isRaw = true;		}		// byte length will always be at least string length		int utflen = strlen;		for (int i = 0 ; (i < strlen) && (utflen <= 65535); i++)		{			int c = isRaw ? data[i] : lvalue.charAt(i);			if ((c >= 0x0001) && (c <= 0x007F))			{				// 1 byte for character			}			else if (c > 0x07FF)			{				utflen += 2; // 3 bytes for character			}			else			{				utflen += 1; // 2 bytes for character			}		}		boolean isLongUTF = false;		// for length than 64K, see format description above		if (utflen > 65535)		{			isLongUTF = true;			utflen = 0;		}		out.write((utflen >>> 8) & 0xFF);		out.write((utflen >>> 0) & 0xFF);		for (int i = 0 ; i < strlen ; i++)		{			int c = isRaw ? data[i] : lvalue.charAt(i);			if ((c >= 0x0001) && (c <= 0x007F))			{				out.write(c);			}			else if (c > 0x07FF)			{				out.write(0xE0 | ((c >> 12) & 0x0F));				out.write(0x80 | ((c >>  6) & 0x3F));				out.write(0x80 | ((c >>  0) & 0x3F));			}			else			{				out.write(0xC0 | ((c >>  6) & 0x1F));				out.write(0x80 | ((c >>  0) & 0x3F));			}		}		if (isLongUTF)		{			// write the following 3 bytes to terminate the string:			// (11100000, 00000000, 00000000)			out.write(0xE0);			out.write(0);			out.write(0);		}	}    /**     * Reads in a string from the specified data input stream. The      * string has been encoded using a modified UTF-8 format.      * <p>     * The first two bytes are read as if by      * <code>readUnsignedShort</code>. This value gives the number of      * following bytes that are in the encoded string, not     * the length of the resulting string. The following bytes are then      * interpreted as bytes encoding characters in the UTF-8 format      * and are converted into characters.      * <p>     * This method blocks until all the bytes are read, the end of the      * stream is detected, or an exception is thrown.      *     * @param      in   a data input stream.     * @return     a Unicode string.     * @exception  EOFException            if the input stream reaches the end     *               before all the bytes.     * @exception  IOException             if an I/O error occurs.     * @exception  UTFDataFormatException  if the bytes do not represent a     *               valid UTF-8 encoding of a Unicode string.     * @see        java.io.DataInputStream#readUnsignedShort()	 	 * @see java.io.Externalizable#readExternal     */	public void readExternalFromArray(ArrayInputStream in)         throws IOException    {        arg_passer[0]        = rawData;        rawLength = in.readCloudscapeUTF(arg_passer);        rawData = arg_passer[0];        // restoreToNull();        value  = null;        stream = null;        // clear out the int array, so it will stay current        intArray = null;        intLength = 0;        cKey = null;    }    char[][] arg_passer = new char[1][];	public void readExternal(ObjectInput in) throws IOException    {        // if in.available() blocked at 0, use this default string size         int utflen = in.readUnsignedShort();        int requiredLength;        // minimum amount that is reasonable to grow the array        // when we know the array needs to growby at least one        // byte but we dont want to grow by one byte as that        // is not performant        int minGrowBy = growBy();        if (utflen != 0)        {            // the object was not stored as a streaming column             // we know exactly how long it is            requiredLength = utflen;        }        else        {            // the object was stored as a streaming column             // and we have a clue how much we can read unblocked             // OR            // The original string was a 0 length string.            requiredLength = in.available();            if (requiredLength < minGrowBy)                requiredLength = minGrowBy;        }        char str[];        if ((rawData == null) || (requiredLength > rawData.length)) {                        str = new char[requiredLength];        } else {            str = rawData;        }        int arrayLength = str.length;        // Set these to null to allow GC of the array if required.        rawData = null;        restoreToNull();        int count = 0;        int strlen = 0;readingLoop:        while ( ((count < utflen) || (utflen == 0)))        {            int c;            try {                c = in.readUnsignedByte();            } catch (EOFException eof) {                if (utflen != 0)                    throw new EOFException();                // This is the case for a 0 length string.                // OR the string was originally streamed in                // which puts a 0 for utflen but no trailing                // E0,0,0 markers.                break readingLoop;            }            //if (c == -1)		// read EOF            //{            //	if (utflen != 0)            //		throw new EOFException();            //	break;            //}            // change it to an unsigned byte            //c &= 0xFF;            if (strlen >= arrayLength) // the char array needs to be grown             {                int growby = in.available();                // We know that the array needs to be grown by at least one.                // However, even if the input stream wants to block on every                // byte, we don't want to grow by a byte at a time.                // Note, for large data (clob > 32k), it is performant                // to grow the array by atleast 4k rather than a small amount                // Even better maybe to grow by 32k but then may be                // a little excess(?) for small data.                 // hopefully in.available() will give a fair                // estimate of how much data can be read to grow the                 // array by larger and necessary chunks.                // This performance issue due to                 // the slow growth of this array was noticed since inserts                // on clobs was taking a really long time as                // the array here grew previously by 64 bytes each time                 // till stream was drained.  (Derby-302)                // for char, growby 64 seems reasonable, but for varchar                // clob 4k or 32k is performant and hence                // growBy() is override correctly to ensure this                if (growby < minGrowBy)                    growby = minGrowBy;                int newstrlength = arrayLength + growby;                char oldstr[] = str;                str = new char[newstrlength];                System.arraycopy(oldstr, 0, str, 0, arrayLength);                arrayLength = newstrlength;            }            /// top fours bits of the first unsigned byte that maps to a             //  1,2 or 3 byte character            //            // 0000xxxx	- 0 - 1 byte char            // 0001xxxx - 1 - 1 byte char            // 0010xxxx - 2 - 1 byte char            // 0011xxxx - 3 - 1 byte char            // 0100xxxx - 4 - 1 byte char            // 0101xxxx - 5 - 1 byte char            // 0110xxxx - 6 - 1 byte char            // 0111xxxx - 7 - 1 byte char            // 1000xxxx - 8 - error            // 1001xxxx - 9 - error            // 1010xxxx - 10 - error            // 1011xxxx - 11 - error            // 1100xxxx - 12 - 2 byte char            // 1101xxxx - 13 - 2 byte char            // 1110xxxx - 14 - 3 byte char            // 1111xxxx - 15 - error            int char2, char3;            char actualChar;            if ((c & 0x80) == 0x00)            {                // one byte character                count++;                actualChar = (char) c;            }            else if ((c & 0x60) == 0x40) // we know the top bit is set here            {                 // two byte character                count += 2;                if (utflen != 0 && count > utflen)                     throw new UTFDataFormatException();		                  char2 = in.readUnsignedByte();                if ((char2 & 0xC0) != 0x80)                    throw new UTFDataFormatException();		                  actualChar = (char)(((c & 0x1F) << 6) | (char2 & 0x3F));            }            else if ((c & 0x70) == 0x60) // we know the top bit is set here            {                // three byte character                count += 3;                if (utflen != 0 && count > utflen)                     throw new UTFDataFormatException();		                  char2 = in.readUnsignedByte();                char3 = in.readUnsignedByte();                if ((c == 0xE0) && (char2 == 0) && (char3 == 0)                    && (utflen == 0))                {                    // we reached the end of a long string,                    // that was terminated with                    // (11100000, 00000000, 00000000)                    break readingLoop;                }                if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))                    throw new UTFDataFormatException();		                                                  actualChar = (char)(((c & 0x0F) << 12) |                                           ((char2 & 0x3F) << 6) |                                           ((char3 & 0x3F) << 0));            }            else {                throw new UTFDataFormatException();            }            str[strlen++] = actualChar;        }        rawData = str;        rawLength = strlen;                                // clear out the int array, so it will stay current        intArray = null;        intLength = 0;        cKey = null;    }    /**     * returns the reasonable minimum amount by      * which the array can grow . See readExternal.      * when we know that the array needs to grow by at least     * one byte, it is not performant to grow by just one byte     * instead this amount is used to provide a resonable growby size.     * @return minimum reasonable growby size     */    protected int growBy()    {        return GROWBY_FOR_CHAR;  //seems reasonable for a char    }	/**	 * @see Storable#restoreToNull	 *	 */	public void restoreToNull()	{		value = null;		stream = null;		rawLength = -1;		// clear out the int array as well, so it will stay current		intArray = null;		intLength = 0;		cKey = null;	}	/**		@exception StandardException thrown on error	 */	public boolean compare(int op,						   DataValueDescriptor other,						   boolean orderedNulls,						   boolean unknownRV)		throws StandardException	{		if (!orderedNulls)		// nulls are unordered		{			if (this.isNull() || ((DataValueDescriptor) other).isNull())				return unknownRV;		}		/* When comparing String types to non-string types, we always		 * convert the string type to the non-string type.		 */		if (! (other instanceof SQLChar))		{			return other.compare(flip(op), this, orderedNulls, unknownRV);		}		/* Do the comparison */		return super.compare(op, other, orderedNulls, unknownRV);	}	/**		@exception StandardException thrown on error	 */	public 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));		}		// stringCompare deals with null as comparable and smallest		return stringCompare(this, (SQLChar)other);	}	/*	 * 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 Object cloneObject()	{		if (stream == null)			return getClone();		SQLChar self = (SQLChar) getNewNull();		self.copyState(this);		return self;	}	/*	 * DataValueDescriptor interface	 */	/** @see DataValueDescriptor#getClone */	public DataValueDescriptor getClone()	{		try		{			return new SQLChar(getString());		}		catch (StandardException se)		{			if (SanityManager.DEBUG)				SanityManager.THROWASSERT("Unexpected exception " + se);			return null;		}	}	/**	 * @see DataValueDescriptor#getNewNull	 *	 */	public DataValueDescriptor getNewNull()	{		return new SQLChar();	}	/** 	 * @see DataValueDescriptor#setValueFromResultSet 	 *	 * @exception SQLException		Thrown on error	 */	public final void setValueFromResultSet(ResultSet resultSet, int colNumber,									  boolean isNullable)		throws SQLException	{			setValue(resultSet.getString(colNumber));	}	/**		Set the value into a PreparedStatement.	*/	public final void setInto(PreparedStatement ps, int position) throws SQLException, StandardException {		ps.setString(position, getString());	}	/*	 * class interface	 */	/*	 * constructors	 */	/**		no-arg constructor, required by Formattable.	*/	public SQLChar()	{	}	public SQLChar(String val)	{		value = val;	}	public void setValue(String theValue)	{		stream = null;		rawLength = -1;		// clear out the int array as well, so it will stay current		intArray = null;		intLength = 0;		cKey = null;		value = theValue;	}	public void setValue(boolean theValue) throws StandardException	{		// match JCC.		setValue(theValue ? "1" : "0");	}	public void setValue(int theValue)  throws StandardException	{		setValue(Integer.toString(theValue));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -