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

📄 sqlchar.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*   Derby - Class org.apache.derby.iapi.types.SQLChar   Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable.   Licensed 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.services.context.ContextService;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.io.Storable;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.types.DataTypeDescriptor;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.types.TypeId;import org.apache.derby.iapi.types.StringDataValue;import org.apache.derby.iapi.types.NumberDataValue;import org.apache.derby.iapi.types.BooleanDataValue;import org.apache.derby.iapi.types.ConcatableDataValue;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.services.cache.ClassSize;import org.apache.derby.iapi.services.io.ArrayInputStream;import org.apache.derby.iapi.util.StringUtil;import org.apache.derby.iapi.services.i18n.LocaleFinder;import org.apache.derby.iapi.db.DatabaseContext;import org.apache.derby.iapi.types.SQLInteger;import org.apache.derby.iapi.types.SQLDate;import org.apache.derby.iapi.types.SQLTime;import org.apache.derby.iapi.types.SQLTimestamp;import java.io.InputStream;import java.io.ObjectOutput;import java.io.ObjectInput;import java.io.IOException;import java.io.UTFDataFormatException;import java.io.EOFException;import java.sql.Date;import java.sql.ResultSet;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.Time;import java.sql.Timestamp;import java.text.CollationElementIterator;import java.text.RuleBasedCollator;import java.text.CollationKey;import java.text.DateFormat;import java.util.Locale;import java.util.Calendar;/** * SQLChar satisfies the DataValueDescriptor * interfaces (i.e., OrderableDataType). It implements an String holder, * e.g. for storing a column value; it can be specified * when constructed to not allow nulls. Nullability cannot be changed * after construction. * <p> * Because OrderableDataType is a subclass of DataType, * SQLChar can play a role in either a DataType/ValueRow * or a OrderableDataType/KeyRow, interchangeably. */public class SQLChar	extends DataType implements StringDataValue, StreamStorable{    /**     * threshold, that decides when we return space back to the VM     * see getString() where it is used     */    protected final static int RETURN_SPACE_THRESHOLD = 4096;        /**     * 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 reasonable growby size.     */    private final static int GROWBY_FOR_CHAR = 64;	/**		Static array that can be used for blank padding.	*/	private static final char[] BLANKS = new char[40];	static {		for (int i = 0; i < BLANKS.length; i++) {			BLANKS[i] = ' ';		}	}	public static void appendBlanks(char[] ca, int offset, int howMany) {		while (howMany > 0) {			int count = howMany > BLANKS.length ? BLANKS.length : howMany;			System.arraycopy(BLANKS, 0, ca, offset, count);			howMany -= count;			offset += count;		}	}	/*	 * DataValueDescriptor interface	 * (mostly implemented in DataType)	 * casts to the	 * numeric and date/time types as well, "for valid strings"	 */	/**	 * @see DataValueDescriptor#getBoolean	 *	 * @exception StandardException		Thrown on error	 */	public boolean getBoolean()		throws StandardException	{		if (isNull()) return false;		// match JCC, match only "0" or "false" for false. No case insensitivity.		// everything else is true.		String cleanedValue = getString().trim();		return !(cleanedValue.equals("0") || cleanedValue.equals("false"));	}	/**	 * @see DataValueDescriptor#getByte	 * @exception StandardException thrown on failure to convert	 */	public byte	getByte() throws StandardException	{		if (isNull()) return (byte)0;		try {			return Byte.parseByte(getString().trim());		} catch (NumberFormatException nfe) {			throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "byte");		}	}	/**	 * @see DataValueDescriptor#getShort	 * @exception StandardException thrown on failure to convert	 */	public short	getShort() throws StandardException	{		if (isNull()) return (short)0;		try {			return Short.parseShort(getString().trim());		} catch (NumberFormatException nfe) {			throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "short");		}	}	/**	 * @see DataValueDescriptor#getInt	 * @exception StandardException thrown on failure to convert	 */	public int	getInt() throws StandardException	{		if (isNull()) return 0;		try {			return Integer.parseInt(getString().trim());		} catch (NumberFormatException nfe) {			throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "int");		}	}	/**	 * @see DataValueDescriptor#getLong	 * @exception StandardException thrown on failure to convert	 */	public long	getLong() throws StandardException	{		if (isNull()) return 0;		try {			return Long.parseLong(getString().trim());		} catch (NumberFormatException nfe) {			throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "long");		}	}	/**	 * @see DataValueDescriptor#getFloat	 * @exception StandardException thrown on failure to convert	 */	public float	getFloat() throws StandardException	{		if (isNull()) return 0;		try {			return new Float(getString().trim()).floatValue();		} catch (NumberFormatException nfe) {			throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "float");		}	}	/**	 * @see DataValueDescriptor#getDouble	 * @exception StandardException thrown on failure to convert	 */	public double	getDouble() throws StandardException	{		if (isNull()) return 0;		try {			return new Double(getString().trim()).doubleValue();		} catch (NumberFormatException nfe) {			throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "double");		}	}	/**	 * CHAR/VARCHAR/LONG VARCHAR implementation. Convert to a BigDecimal using getString.	 */	public int typeToBigDecimal()  throws StandardException	{		return java.sql.Types.CHAR;	}	/**	 * @see DataValueDescriptor#getDate	 * @exception StandardException thrown on failure to convert	 */	public Date	getDate( Calendar cal) throws StandardException	{        return getDate( cal, getString(), getLocaleFinder());	}    public static Date getDate(java.util.Calendar cal, String str, LocaleFinder localeFinder) throws StandardException    {        if( str == null)            return null;        SQLDate internalDate = new SQLDate( str, false, localeFinder);        return internalDate.getDate( cal);    }	/**	 * @see DataValueDescriptor#getTime	 * @exception StandardException thrown on failure to convert	 */	public Time	getTime(Calendar cal) throws StandardException	{		return getTime( cal, getString(), getLocaleFinder());	}	/**	 * @exception StandardException thrown on failure to convert	 */	public static Time getTime( Calendar cal, String str, LocaleFinder localeFinder) throws StandardException	{        if( str == null)            return null;        SQLTime internalTime = new SQLTime( str, false, localeFinder, cal);        return internalTime.getTime( cal);	}	/**	 * @see DataValueDescriptor#getTimestamp	 * @exception StandardException thrown on failure to convert	 */	public Timestamp getTimestamp( Calendar cal) throws StandardException	{		return getTimestamp( cal, getString(), getLocaleFinder());	}	/**	 * @see DataValueDescriptor#getTimestamp	 * @exception StandardException thrown on failure to convert	 */	public static Timestamp	getTimestamp(java.util.Calendar cal, String str, LocaleFinder localeFinder)        throws StandardException	{        if( str == null)            return null;        SQLTimestamp internalTimestamp = new SQLTimestamp( str, false, localeFinder, cal);        return internalTimestamp.getTimestamp( cal);	}	/**	 * @exception StandardException		Thrown on error	 */	public Object	getObject() throws StandardException	{		return getString();	}	/**	 * @exception StandardException		Thrown on error	 */	public InputStream	getStream() throws StandardException	{		return stream;	}	/**	 * @exception StandardException		Thrown on error	 */	public int	getLength() throws StandardException	{		if (rawLength != -1)			return rawLength;		String tmpString = getString();		return (tmpString == null) ?			0 : tmpString.length();	}	public String getTypeName()	{		return TypeId.CHAR_NAME;	}	/**	 * If possible, use getCharArray() if you don't really	 * need a string.  getString() will cause an extra 	 * char array to be allocated when it calls the the String() 	 * constructor (the first time through), so may be	 * cheaper to use getCharArray().	 *	 * @exception StandardException		Thrown on error	 */	public String getString() throws StandardException	{		if (value == null) {			int len = rawLength;			if (len != -1) {				// data is stored in the char[] array				value = new String(rawData, 0, len);				if (len > RETURN_SPACE_THRESHOLD) {					// free up this char[] array to reduce memory usage					rawData = null;					rawLength = -1;					// clear out the int array as well, so it will stay current					intArray = null;					intLength = 0;					cKey = null;				}			} else if (stream != null) {				// data stored as a stream				try {					if (stream instanceof FormatIdInputStream) {						readExternal((FormatIdInputStream) stream);					} else {						readExternal(new FormatIdInputStream(stream));					}					stream = null;					// at this point the value is only in the char[]					// so call again to convert to a String					return getString();				} catch (IOException ioe) {					throw StandardException.newException(                            SQLState.LANG_STREAMING_COLUMN_I_O_EXCEPTION,                             ioe,                             "java.sql.String");				}			}		}		return value;	}	/**	 * Get a char array.  Typically, this is a simple	 * getter that is cheaper than getString() because	 * we always need to create a char array when	 * doing I/O.  Use this instead of getString() where	 * reasonable.	 * <p>	 * <b>WARNING</b>: may return a character array that has spare	 * characters at the end.  MUST be used in conjunction	 * with getLength() to be safe.	 * 	 * @exception StandardException		Thrown on error	 */	public char[] getCharArray() throws StandardException	{		if (isNull())		{			return (char[])null;		}		else if (rawLength != -1)		{			return rawData;		}		else		{			// this is expensive -- we are getting a			// copy of the char array that the 			// String wrapper uses.			getString();			rawData = value.toCharArray();			rawLength = rawData.length;			// clear out the int array as well, so it will stay current			intArray = null;			intLength = 0;			cKey = null;			return rawData;		}	}	/*	 * StreamStorable interface : 	 */	public InputStream returnStream()	{		return stream;	}	public void setStream(InputStream newStream)	{		this.value = null;		this.rawLength = -1;		this.stream = newStream;		// clear out the int array as well, so it will stay current		intArray = null;		intLength = 0;		cKey = null;	}	public void loadStream() throws StandardException	{		getString();	}	/*	 * Storable interface, implies Externalizable, TypedFormat	 */	/**		Return my format identifier.		@see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId	*/	public int getTypeFormatId() {		return StoredFormatIds.SQL_CHAR_ID;	}	/**	 * see if the String value is null.	 @see Storable#isNull	*/	public boolean isNull()	{		return ((value == null) && (rawLength == -1) && (stream == null));	}	/**		The maximum stored size is based upon the UTF format		used to stored the String. The format consists of		a two byte length field and a maximum number of three		bytes for each character.		<BR>		This puts an upper limit on the length of a stored		String. The maximum stored length is 65535, these leads to		the worse case of a maximum string length of 21844 ((65535 - 2) / 3).		<BR>		Strings with stored length longer than 64K is handled with		the following format:		(1) 2 byte length: will be assigned 0.		(2) UTF formated string data.		(3) terminate the string with the following 3 bytes:			first byte is:			+---+---+---+---+---+---+---+---+			| 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 |			+---+---+---+---+---+---+---+---+			second byte is:			+---+---+---+---+---+---+---+---+			| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |			+---+---+---+---+---+---+---+---+			third byte is:			+---+---+---+---+---+---+---+---+			| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |			+---+---+---+---+---+---+---+---+		The UTF format:		Writes a string to the underlying output stream using UTF-8 		encoding in a machine-independent manner. 		<p>		First, two bytes are written to the output stream as if by the 		<code>writeShort</code> method giving the number of bytes to 

⌨️ 快捷键说明

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