Search in sources :

Example 1 with CountdownInputStream

use of org.hsqldb_voltpatches.lib.CountdownInputStream in project voltdb by VoltDB.

the class SessionData method allocateLobForResult.

/**
     * allocate storage for a new LOB
     */
public void allocateLobForResult(ResultLob result, InputStream inputStream) {
    long resultLobId = result.getLobID();
    CountdownInputStream countStream;
    switch(result.getSubType()) {
        case ResultLob.LobResultTypes.REQUEST_CREATE_BYTES:
            {
                long blobId;
                long blobLength = result.getBlockLength();
                if (inputStream == null) {
                    blobId = resultLobId;
                    inputStream = result.getInputStream();
                } else {
                    BlobData blob = session.createBlob(blobLength);
                    blobId = blob.getId();
                    resultLobs.put(resultLobId, blobId);
                }
                countStream = new CountdownInputStream(inputStream);
                countStream.setCount(blobLength);
                database.lobManager.setBytesForNewBlob(blobId, countStream, result.getBlockLength());
                break;
            }
        case ResultLob.LobResultTypes.REQUEST_CREATE_CHARS:
            {
                long clobId;
                long clobLength = result.getBlockLength();
                if (inputStream == null) {
                    clobId = resultLobId;
                    if (result.getReader() != null) {
                        inputStream = new ReaderInputStream(result.getReader());
                    } else {
                        inputStream = result.getInputStream();
                    }
                } else {
                    ClobData clob = session.createClob(clobLength);
                    clobId = clob.getId();
                    resultLobs.put(resultLobId, clobId);
                }
                countStream = new CountdownInputStream(inputStream);
                countStream.setCount(clobLength * 2);
                database.lobManager.setCharsForNewClob(clobId, countStream, result.getBlockLength());
                break;
            }
    }
}
Also used : ReaderInputStream(org.hsqldb_voltpatches.lib.ReaderInputStream) ClobData(org.hsqldb_voltpatches.types.ClobData) BlobData(org.hsqldb_voltpatches.types.BlobData) CountdownInputStream(org.hsqldb_voltpatches.lib.CountdownInputStream)

Example 2 with CountdownInputStream

use of org.hsqldb_voltpatches.lib.CountdownInputStream in project voltdb by VoltDB.

the class JDBCPreparedStatement method setUnicodeStream.

/**
     * <!-- start generic documentation -->
     * Sets the designated parameter to the given input stream, which
     * will have the specified number of bytes.
     * (JDBC4 deleted:)
     * [A Unicode character has two bytes, with the first byte being the high
     * byte, and the second being the low byte.] <p>
     *
     * When a very large Unicode value is input to a <code>LONGVARCHAR</code>
     * parameter, it may be more practical to send it via a
     * <code>java.io.InputStream</code> object. The data will be read from the
     * stream as needed until end-of-file is reached.  The JDBC driver will
     * do any necessary conversion from Unicode to the database char format.
     *
     * (JDBC4 added:)
     * The byte format of the Unicode stream must be a Java UTF-8, as defined in the
     * Java Virtual Machine Specification.
     *
     * <P><B>Note:</B> This stream object can either be a standard
     * Java stream object or your own subclass that implements the
     * standard interface.
     * <!-- end generic documentation -->
     *
     * <!-- start release-specific documentation -->
     * <div class="ReleaseSpecificDocumentation">
     * <h3>HSQLDB-Specific Information:</h3> <p>
     *
     * From 1.7.0 to 1.8.0.x, this method complies with behavior as defined by
     * the JDBC3 specification (the stream is treated as though it has UTF16
     * encoding). <p>
     *
     * Starting with 1.9.0, this method behaves according to the JDBC4
     * specification (the stream is treated as though it has UTF-8
     * encoding, as defined in the Java Virtual Machine Specification) when
     * built under JDK 1.6+; otherwise, it behaves as defined by the JDBC3
     * specification.  Regardless, this method is deprecated: please use
     * setCharacterStream(...) instead.
     * </div>
     * <!-- end release-specific documentation -->
     *
     * @param parameterIndex the first parameter is 1, the second is 2, ...
     * @param x a <code>java.io.InputStream</code> object that contains the
     *        Unicode parameter value
     * (JDBC4 deleted:)
     *       [as two-byte Unicode characters]
     * @param length the number of bytes in the stream
     * @exception SQLException if a database access error occurs or
     * this method is called on a closed <code>PreparedStatement</code>
     * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
     * this method
     * @deprecated
     *      Sun does not include a reason, but presumably
     *      this is because setCharacterStream is now prefered
     */
//#ifdef DEPRECATEDJDBC
public synchronized void setUnicodeStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException {
    checkSetParameterIndex(parameterIndex, true);
    String msg = null;
    final int ver = JDBCDatabaseMetaData.JDBC_MAJOR;
    if (x == null) {
        throw Util.nullArgument("x");
    }
    // CHECKME:  Is JDBC4 clarification of UNICODE stream format retroactive?
    if ((ver < 4) && (length % 2 != 0)) {
        msg = "Odd length argument for UTF16 encoded stream: " + length;
        throw Util.invalidArgument(msg);
    }
    String encoding = (ver < 4) ? "UTF16" : "UTF8";
    StringWriter writer = new StringWriter();
    try {
        CountdownInputStream cis = new CountdownInputStream(x);
        InputStreamReader reader = new InputStreamReader(cis, encoding);
        char[] buff = new char[1024];
        int charsRead;
        cis.setCount(length);
        while (-1 != (charsRead = reader.read(buff))) {
            writer.write(buff, 0, charsRead);
        }
    } catch (IOException ex) {
        throw Util.sqlException(ErrorCode.SERVER_TRANSFER_CORRUPTED, ex.toString());
    }
    setParameter(parameterIndex, writer.toString());
}
Also used : StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) CountdownInputStream(org.hsqldb_voltpatches.lib.CountdownInputStream)

Aggregations

CountdownInputStream (org.hsqldb_voltpatches.lib.CountdownInputStream)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StringWriter (java.io.StringWriter)1 ReaderInputStream (org.hsqldb_voltpatches.lib.ReaderInputStream)1 BlobData (org.hsqldb_voltpatches.types.BlobData)1 ClobData (org.hsqldb_voltpatches.types.ClobData)1