clob.java
来自「derby database source code.good for you.」· Java 代码 · 共 502 行 · 第 1/2 页
JAVA
502 行
/* Derby - Class org.apache.derby.client.am.Clob Copyright (c) 2001, 2005 The Apache Software Foundation or its licensors, where 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.client.am;public class Clob extends Lob implements java.sql.Clob { //---------------------navigational members----------------------------------- //-----------------------------state------------------------------------------ protected String string_ = null; // Only used for input purposes. For output, each getXXXStream call // must generate an independent stream. protected java.io.InputStream asciiStream_ = null; protected java.io.InputStream unicodeStream_ = null; protected java.io.Reader characterStream_ = null; // used for input // Therefore, we always convert a String to UTF-8 before we flow it for input protected byte[] utf8String_; // the length of the clob returned by the LENGTH function. protected long lengthInBytes_ = 0; private PreparedStatement internalLengthStmt_ = null; protected String encoding_ = "UNICODE"; //---------------------constructors/finalizer--------------------------------- public Clob(Agent agent, String string) { this(agent); string_ = string; sqlLength_ = string_.length(); lengthObtained_ = true; dataType_ |= STRING; } // CTOR for output, when a btc isn't available; the encoding is public Clob(Agent agent, byte[] unconvertedBytes, String charsetName, int dataOffset) throws SqlException { this(agent); try { // check for null encoding is needed because the net layer // will no longer throw an exception if the server didn't specify // a mixed or double byte ccsid (ccsid = 0). this check for null in the // cursor is only required for types which can have mixed or double // byte ccsids. if (charsetName == null) { throw new SqlException(agent.logWriter_, "Required character converter not available for data type."); } string_ = new String(unconvertedBytes, dataOffset, unconvertedBytes.length - dataOffset, charsetName); sqlLength_ = string_.length(); lengthObtained_ = true; dataType_ |= STRING; } catch (java.io.UnsupportedEncodingException e) { throw new SqlException(agent_.logWriter_, e.getMessage()); } } // CTOR for ascii/unicode stream input //"US-ASCII", "UTF-8", or "UnicodeBigUnmarked" public Clob(Agent agent, java.io.InputStream inputStream, String encoding, int length) throws SqlException { this(agent); sqlLength_ = length; lengthObtained_ = true; if (encoding.equals("US-ASCII")) { asciiStream_ = inputStream; dataType_ |= ASCII_STREAM; } else if (encoding.equals("UTF-8")) { // "UTF-8" unicodeStream_ = inputStream; dataType_ |= UNICODE_STREAM; } else if (encoding.equals("UnicodeBigUnmarked")) { // "UnicodeBigUnmarked" try { characterStream_ = new java.io.InputStreamReader(inputStream, "UnicodeBigUnmarked"); } catch (java.io.UnsupportedEncodingException e) { throw new SqlException(agent_.logWriter_, e.getMessage()); } dataType_ |= CHARACTER_STREAM; sqlLength_ = length / 2; } } // CTOR for character stream input // THE ENCODING IS ASSUMED TO BE "UTF-16BE" public Clob(Agent agent, java.io.Reader reader, int length) { this(agent); sqlLength_ = length; lengthObtained_ = true; characterStream_ = reader; dataType_ |= CHARACTER_STREAM; } private Clob(Agent agent) { super(agent); } protected void finalize() throws java.lang.Throwable { super.finalize(); if (internalLengthStmt_ != null) { internalLengthStmt_.closeX(); } } // ---------------------------jdbc 2------------------------------------------ // Create another method lengthX for internal calls public long length() throws SqlException { synchronized (agent_.connection_) { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "length"); } if (lengthObtained_) { return sqlLength_; } lengthInBytes_ = super.sqlLength(); if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "length", sqlLength_); } return sqlLength_; } } public String getSubString(long pos, int length) throws SqlException { synchronized (agent_.connection_) { String retVal = null; if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "getSubString", (int) pos, length); } // We can also do a check for pos > length() // Defer it till FP7 so that proper testing can be performed on this if ((pos <= 0) || (length < 0)) { throw new SqlException(agent_.logWriter_, "Invalid position " + pos + " or length " + length); } retVal = getSubStringX(pos, length); if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "getSubString", retVal); } return retVal; } } private String getSubStringX(long pos, int length) throws SqlException { checkForClosedConnection(); long actualLength = Math.min(this.length() - pos + 1, (long) length); return string_.substring((int) pos - 1, (int) (pos - 1 + actualLength)); } public java.io.Reader getCharacterStream() throws SqlException { synchronized (agent_.connection_) { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "getCharacterStream"); } java.io.Reader retVal = getCharacterStreamX(); if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "getCharacterStream", retVal); } return retVal; } } private java.io.Reader getCharacterStreamX() throws SqlException { checkForClosedConnection(); if (isCharacterStream()) // this Lob is used for input { return characterStream_; } return new java.io.StringReader(string_); } public java.io.InputStream getAsciiStream() throws SqlException { synchronized (agent_.connection_) { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "getAsciiStream"); } java.io.InputStream retVal = getAsciiStreamX(); if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "getAsciiStream", retVal); } return retVal; } } private java.io.InputStream getAsciiStreamX() throws SqlException { checkForClosedConnection(); if (isAsciiStream()) // this Lob is used for input { return asciiStream_; } return new AsciiStream(string_, new java.io.StringReader(string_)); } public long position(String searchstr, long start) throws SqlException { synchronized (agent_.connection_) { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "position(String, long)", searchstr, start); } if (searchstr == null) { throw new SqlException(agent_.logWriter_, "Search string cannot be null."); } long pos = positionX(searchstr, start); if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "position(String, long)", pos); } return pos; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?