Search in sources :

Example 1 with CharacterStreamDescriptor

use of org.apache.derby.iapi.jdbc.CharacterStreamDescriptor in project derby by apache.

the class EmbedResultSet method getCharacterStream.

/**
 * JDBC 2.0
 *
 * <p>Get the value of a column in the current row as a java.io.Reader.
 *
 * @exception SQLException database error.
 */
public final java.io.Reader getCharacterStream(int columnIndex) throws SQLException {
    checkIfClosed("getCharacterStream");
    int lmfs;
    int colType = getColumnType(columnIndex);
    switch(colType) {
        case Types.CHAR:
        case Types.VARCHAR:
        case Types.LONGVARCHAR:
            lmfs = maxFieldSize;
            break;
        case // Embedded and JCC extension - CLOB is not subject to max field size.
        Types.CLOB:
            lmfs = 0;
            break;
        // match JCC which treats the bytes as a UTF16-BE stream
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
        case Types.BLOB:
            try {
                java.io.InputStream is = getBinaryStream(columnIndex);
                if (is == null)
                    return null;
                java.io.Reader r = new java.io.InputStreamReader(is, "UTF-16BE");
                currentStream = r;
                return r;
            } catch (java.io.UnsupportedEncodingException uee) {
                throw new SQLException(uee.getMessage());
            }
        default:
            throw dataTypeConversion("java.io.Reader", columnIndex);
    }
    Object syncLock = getConnectionSynchronization();
    synchronized (syncLock) {
        boolean pushStack = false;
        try {
            useStreamOrLOB(columnIndex);
            StringDataValue dvd = (StringDataValue) getColumn(columnIndex);
            if (wasNull = dvd.isNull()) {
                return null;
            }
            pushStack = true;
            setupContextStack();
            // The reader we will return to the user
            java.io.Reader ret;
            if (dvd.hasStream()) {
                CharacterStreamDescriptor csd = dvd.getStreamWithDescriptor();
                // See if we have to enforce a max field size.
                if (lmfs > 0) {
                    csd = new CharacterStreamDescriptor.Builder().copyState(csd).maxCharLength(lmfs).build();
                }
                ret = new UTF8Reader(csd, this, syncLock);
            } else {
                String val = dvd.getString();
                if (lmfs > 0) {
                    if (val.length() > lmfs)
                        val = val.substring(0, lmfs);
                }
                ret = new java.io.StringReader(val);
            }
            currentStream = ret;
            return ret;
        } catch (Throwable t) {
            throw noStateChangeException(t);
        } finally {
            if (pushStack) {
                restoreContextStack();
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Reader(java.io.Reader) CharacterStreamDescriptor(org.apache.derby.iapi.jdbc.CharacterStreamDescriptor) StringDataValue(org.apache.derby.iapi.types.StringDataValue) InputStream(java.io.InputStream)

Example 2 with CharacterStreamDescriptor

use of org.apache.derby.iapi.jdbc.CharacterStreamDescriptor in project derby by apache.

the class EmbedCallableStatement method getCharacterStream.

// JDBC 4.0 methods
/**
 * Retrieves the value of the designated parameter as a
 * <code>java.io.Reader</code> object in the Java programming language.
 * Introduced in JDBC 4.0.
 *
 * @param parameterIndex the first parameter is 1, the second is 2, ...
 * @return a <code>java.io.Reader</code> object that contains the parameter
 * value; if the value is SQL <code>NULL</code>, the value returned
 * is <code>null</code> in the Java programming language.
 * @throws SQLException if a database access error occurs or this method is
 * called on a closed <code>CallableStatement</code>
 */
public final Reader getCharacterStream(int parameterIndex) throws SQLException {
    checkStatus();
    // Make sure the specified parameter has mode OUT or IN/OUT.
    switch(getParms().getParameterMode(parameterIndex)) {
        case (ParameterMetaData.parameterModeIn):
        case (ParameterMetaData.parameterModeUnknown):
            throw newSQLException(SQLState.LANG_NOT_OUTPUT_PARAMETER, Integer.toString(parameterIndex));
    }
    Reader reader = null;
    int paramType = getParameterJDBCType(parameterIndex);
    switch(paramType) {
        // Handle character/string types.
        case Types.CHAR:
        case Types.VARCHAR:
        case Types.LONGVARCHAR:
        case Types.CLOB:
            boolean pushStack = false;
            Object syncObject = getConnectionSynchronization();
            synchronized (syncObject) {
                try {
                    StringDataValue param = (StringDataValue) getParms().getParameterForGet(parameterIndex - 1);
                    if (param.isNull()) {
                        break;
                    }
                    pushStack = true;
                    setupContextStack();
                    if (param.hasStream()) {
                        CharacterStreamDescriptor csd = param.getStreamWithDescriptor();
                        reader = new UTF8Reader(csd, this, syncObject);
                    } else {
                        reader = new StringReader(param.getString());
                    }
                } catch (Throwable t) {
                    throw EmbedResultSet.noStateChangeException(t);
                } finally {
                    if (pushStack) {
                        restoreContextStack();
                    }
                }
            }
            // End synchronized block
            break;
        // Match JCC which treats the bytes as a UTF-16BE stream.
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
        case Types.BLOB:
            try {
                InputStream is = getBinaryStream(parameterIndex);
                if (is != null) {
                    reader = new InputStreamReader(is, "UTF-16BE");
                }
                break;
            } catch (UnsupportedEncodingException uee) {
                throw newSQLException(uee.getMessage());
            }
        default:
            throw newSQLException(SQLState.LANG_DATA_TYPE_GET_MISMATCH, "java.io.Reader", Util.typeName(paramType));
    }
    // Update wasNull.
    wasNull = (reader == null);
    return reader;
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CharacterStreamDescriptor(org.apache.derby.iapi.jdbc.CharacterStreamDescriptor) StringDataValue(org.apache.derby.iapi.types.StringDataValue)

Example 3 with CharacterStreamDescriptor

use of org.apache.derby.iapi.jdbc.CharacterStreamDescriptor in project derby by apache.

the class BiggerStoreStreamClobTest method setUp.

public void setUp() throws Exception {
    super.initialCharLength = CLOBLENGTH;
    super.headerLength = 2 + 3;
    // The fake stream uses ascii. Add header and EOF marker.
    super.initialByteLength = CLOBLENGTH + headerLength;
    super.bytesPerChar = BYTES_PER_CHAR;
    EmbedStatement embStmt = (EmbedStatement) createStatement();
    InputStream is = new FakeStoreStream(CLOBLENGTH);
    CharacterStreamDescriptor csd = new CharacterStreamDescriptor.Builder().stream(is).charLength(initialCharLength).byteLength(0L).curCharPos(CharacterStreamDescriptor.BEFORE_FIRST).dataOffset(2L).build();
    iClob = new StoreStreamClob(csd, embStmt);
    assertEquals(CLOBLENGTH, iClob.getCharLength());
}
Also used : InputStream(java.io.InputStream) CharacterStreamDescriptor(org.apache.derby.iapi.jdbc.CharacterStreamDescriptor)

Example 4 with CharacterStreamDescriptor

use of org.apache.derby.iapi.jdbc.CharacterStreamDescriptor in project derby by apache.

the class CharacterStreamDescriptorTest method testSetValues.

public void testSetValues() {
    final long charLength = 1023;
    final long byteLength = 1023 * 2;
    final long curBytePos = 4;
    final long curCharPos = 2;
    final long dataOffset = 2;
    final long maxCharLen = 2459;
    InputStream emptyStream = new ByteArrayInputStream(new byte[] {});
    CharacterStreamDescriptor.Builder b = new CharacterStreamDescriptor.Builder().bufferable(true).byteLength(byteLength).charLength(charLength).curBytePos(curBytePos).curCharPos(curCharPos).dataOffset(dataOffset).maxCharLength(maxCharLen).stream(emptyStream);
    CharacterStreamDescriptor csd = b.build();
    // Test the values.
    assertEquals(true, csd.isBufferable());
    assertEquals(false, csd.isPositionAware());
    assertEquals(dataOffset, csd.getDataOffset());
    assertEquals(curBytePos, csd.getCurBytePos());
    assertEquals(curCharPos, csd.getCurCharPos());
    assertEquals(byteLength, csd.getByteLength());
    assertEquals(charLength, csd.getCharLength());
    assertEquals(maxCharLen, csd.getMaxCharLength());
    PositionedStream emptyPS = new PositionedTestStream(curBytePos);
    // Set only a few values.
    csd = new CharacterStreamDescriptor.Builder().bufferable(true).positionAware(true).maxCharLength(maxCharLen).stream(emptyPS.asInputStream()).build();
    assertEquals(true, csd.isBufferable());
    assertEquals(true, csd.isPositionAware());
    assertEquals(maxCharLen, csd.getMaxCharLength());
    // Set data offset and update the character position accordingly.
    csd = new CharacterStreamDescriptor.Builder().bufferable(true).positionAware(true).dataOffset(dataOffset).curCharPos(CharacterStreamDescriptor.BEFORE_FIRST).stream(emptyPS.asInputStream()).build();
    assertEquals(true, csd.isBufferable());
    assertEquals(true, csd.isPositionAware());
    assertEquals(dataOffset, csd.getDataOffset());
    assertEquals(CharacterStreamDescriptor.BEFORE_FIRST, csd.getCurCharPos());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CharacterStreamDescriptor(org.apache.derby.iapi.jdbc.CharacterStreamDescriptor) PositionedStream(org.apache.derby.iapi.types.PositionedStream)

Example 5 with CharacterStreamDescriptor

use of org.apache.derby.iapi.jdbc.CharacterStreamDescriptor in project derby by apache.

the class CharacterStreamDescriptorTest method testDefaultValues.

/**
 * Tests the default values set by the builder.
 */
public void testDefaultValues() {
    InputStream emptyStream = new ByteArrayInputStream(new byte[] {});
    CharacterStreamDescriptor.Builder b = new CharacterStreamDescriptor.Builder().stream(emptyStream);
    CharacterStreamDescriptor csd = b.build();
    // Test the default values.
    assertEquals(false, csd.isBufferable());
    assertEquals(false, csd.isPositionAware());
    assertEquals(0, csd.getDataOffset());
    assertEquals(0, csd.getCurBytePos());
    assertEquals(1, csd.getCurCharPos());
    assertEquals(0, csd.getByteLength());
    assertEquals(0, csd.getCharLength());
    assertEquals(Long.MAX_VALUE, csd.getMaxCharLength());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CharacterStreamDescriptor(org.apache.derby.iapi.jdbc.CharacterStreamDescriptor)

Aggregations

InputStream (java.io.InputStream)7 CharacterStreamDescriptor (org.apache.derby.iapi.jdbc.CharacterStreamDescriptor)7 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Reader (java.io.Reader)2 StringDataValue (org.apache.derby.iapi.types.StringDataValue)2 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SQLException (java.sql.SQLException)1 PositionedStream (org.apache.derby.iapi.types.PositionedStream)1