Search in sources :

Example 1 with CloseFilterInputStream

use of org.apache.derby.iapi.services.io.CloseFilterInputStream in project derby by apache.

the class EmbedResultSet method getBinaryStream.

/**
 * Get the column as an InputStream. If the column is already of type
 *	   InputStream then just return it, otherwise convert the column to a set
 *	   of bytes and create a stream out of the bytes.
 *
 * @param columnIndex the first column is 1, the second is 2, ...
 * @return a Java input stream that delivers the database column value
 * as a stream of uninterpreted bytes.  If the value is SQL NULL
 * then the result is null.
 * @exception SQLException thrown on failure.
 */
public final InputStream getBinaryStream(int columnIndex) throws SQLException {
    checkIfClosed("getBinaryStream");
    int lmfs;
    int colType = getColumnType(columnIndex);
    switch(colType) {
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            lmfs = maxFieldSize;
            break;
        case Types.BLOB:
            lmfs = 0;
            break;
        default:
            throw dataTypeConversion("java.io.InputStream", columnIndex);
    }
    Object syncLock = getConnectionSynchronization();
    synchronized (syncLock) {
        boolean pushStack = false;
        try {
            useStreamOrLOB(columnIndex);
            DataValueDescriptor dvd = getColumn(columnIndex);
            if (wasNull = dvd.isNull()) {
                return null;
            }
            pushStack = true;
            setupContextStack();
            // The stream we will return to the user
            InputStream stream;
            if (dvd.hasStream()) {
                stream = new BinaryToRawStream(dvd.getStream(), dvd);
            } else {
                stream = new ByteArrayInputStream(dvd.getBytes());
            }
            if (lmfs > 0) {
                // Just wrap the InputStream with a LimitInputStream class
                LimitInputStream limitResultIn = new LimitInputStream(stream);
                limitResultIn.setLimit(lmfs);
                stream = limitResultIn;
            }
            // Wrap in a stream throwing exception on invocations when closed.
            stream = new CloseFilterInputStream(stream);
            currentStream = stream;
            return stream;
        } catch (Throwable t) {
            throw noStateChangeException(t);
        } finally {
            if (pushStack) {
                restoreContextStack();
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) LimitInputStream(org.apache.derby.iapi.services.io.LimitInputStream) CloseFilterInputStream(org.apache.derby.iapi.services.io.CloseFilterInputStream) InputStream(java.io.InputStream) CloseFilterInputStream(org.apache.derby.iapi.services.io.CloseFilterInputStream) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) LimitInputStream(org.apache.derby.iapi.services.io.LimitInputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 CloseFilterInputStream (org.apache.derby.iapi.services.io.CloseFilterInputStream)1 LimitInputStream (org.apache.derby.iapi.services.io.LimitInputStream)1 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)1