Example usage for org.hibernate.cfg Environment useStreamsForBinary

List of usage examples for org.hibernate.cfg Environment useStreamsForBinary

Introduction

In this page you can find the example usage for org.hibernate.cfg Environment useStreamsForBinary.

Prototype

@Deprecated
public static boolean useStreamsForBinary() 

Source Link

Document

Should we use streams to bind binary types to JDBC IN parameters?

Usage

From source file:com.enonic.cms.store.hibernate.type.BinaryColumnReader.java

License:Open Source License

public static byte[] readBinary(String columnName, ResultSet resultSet) throws SQLException {
    if (Environment.useStreamsForBinary()) {
        InputStream inputStream = resultSet.getBinaryStream(columnName);
        if (inputStream == null) {
            return null;
        }//from   ww  w  .  j a  v a  2s. c  o  m

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(2048);
        byte[] buffer = new byte[2048];

        try {
            while (true) {
                int amountRead = inputStream.read(buffer);
                if (amountRead == -1) {
                    break;
                }
                outputStream.write(buffer, 0, amountRead);
            }

            inputStream.close();
            outputStream.close();
        } catch (IOException e) {
            throw new HibernateException("Failed to read xml from stream", e);
        }

        return outputStream.toByteArray();
    } else {
        return resultSet.getBytes(columnName);
    }
}

From source file:com.enonic.cms.store.hibernate.type.LazyInitializedJDOMDocumentUserType.java

License:Open Source License

public void nullSafeSet(PreparedStatement stmt, Object value, int index)
        throws HibernateException, SQLException {
    byte[] internalValue = convertToBytes((LazyInitializedJDOMDocument) value);
    if (internalValue != null) {
        if (Environment.useStreamsForBinary()) {
            stmt.setBinaryStream(index, new ByteArrayInputStream(internalValue), internalValue.length);
        } else {/*from  w ww  .java 2  s.  co  m*/
            stmt.setBytes(index, internalValue);
        }
    } else {
        stmt.setNull(index, getSqlType());
    }
}

From source file:com.enonic.cms.store.hibernate.type.XMLBytesUserType.java

License:Open Source License

public void nullSafeSet(PreparedStatement stmt, Object value, int index)
        throws HibernateException, SQLException {
    byte[] internalValue = convertToBinaryFormat((XMLBytes) value);
    if (internalValue != null) {
        if (Environment.useStreamsForBinary()) {
            stmt.setBinaryStream(index, new ByteArrayInputStream(internalValue), internalValue.length);
        } else {//from   w  w  w.  j ava2 s  .  c  o  m
            stmt.setBytes(index, internalValue);
        }
    } else {
        stmt.setNull(index, getSqlType());
    }
}

From source file:org.jasypt.hibernate.type.EncryptedBinaryType.java

License:Apache License

public Object nullSafeGet(final ResultSet rs, final String[] names, final Object owner)
        throws HibernateException, SQLException {

    checkInitialization();// w  ww  .j  a v a 2  s  .  c  o m

    byte[] encryptedValue = null;
    if (Environment.useStreamsForBinary()) {

        final InputStream inputStream = rs.getBinaryStream(names[0]);
        if (rs.wasNull()) {
            return null;
        }

        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BLOCK_SIZE);
        final byte[] inputBuff = new byte[BLOCK_SIZE];
        try {
            int readBytes = 0;
            while (readBytes != -1) {
                readBytes = inputStream.read(inputBuff);
                if (readBytes != -1) {
                    outputStream.write(inputBuff, 0, readBytes);
                }
            }
        } catch (IOException e) {
            throw new HibernateException("IOException occurred reading a binary value", e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                // exception ignored
            }
            try {
                outputStream.close();
            } catch (IOException e) {
                // exception ignored
            }
        }

        encryptedValue = outputStream.toByteArray();

    } else {

        encryptedValue = rs.getBytes(names[0]);
        if (rs.wasNull()) {
            return null;
        }

    }

    return this.encryptor.decrypt(encryptedValue);

}

From source file:org.jasypt.hibernate.type.EncryptedBinaryType.java

License:Apache License

public void nullSafeSet(final PreparedStatement st, final Object value, final int index)
        throws HibernateException, SQLException {

    checkInitialization();/* ww w. j a  v  a2 s .co m*/

    if (value == null) {
        st.setNull(index, sqlType);
    } else {
        final byte[] encryptedValue = this.encryptor.encrypt((byte[]) value);
        if (Environment.useStreamsForBinary()) {
            st.setBinaryStream(index, new ByteArrayInputStream(encryptedValue), encryptedValue.length);
        } else {
            st.setBytes(index, encryptedValue);
        }
    }

}

From source file:org.jasypt.hibernate4.type.EncryptedBinaryType.java

License:Apache License

public Object nullSafeGet(final ResultSet rs, final String[] names, final SessionImplementor session,
        final Object owner) throws HibernateException, SQLException {

    checkInitialization();//from   w ww.  j av  a 2  s  .c  om

    byte[] encryptedValue = null;
    if (Environment.useStreamsForBinary()) {

        final InputStream inputStream = rs.getBinaryStream(names[0]);
        if (rs.wasNull()) {
            return null;
        }

        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BLOCK_SIZE);
        final byte[] inputBuff = new byte[BLOCK_SIZE];
        try {
            int readBytes = 0;
            while (readBytes != -1) {
                readBytes = inputStream.read(inputBuff);
                if (readBytes != -1) {
                    outputStream.write(inputBuff, 0, readBytes);
                }
            }
        } catch (IOException e) {
            throw new HibernateException("IOException occurred reading a binary value", e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                // exception ignored
            }
            try {
                outputStream.close();
            } catch (IOException e) {
                // exception ignored
            }
        }

        encryptedValue = outputStream.toByteArray();

    } else {

        encryptedValue = rs.getBytes(names[0]);
        if (rs.wasNull()) {
            return null;
        }

    }

    return this.encryptor.decrypt(encryptedValue);

}

From source file:org.jasypt.hibernate4.type.EncryptedBinaryType.java

License:Apache License

public void nullSafeSet(final PreparedStatement st, final Object value, final int index,
        final SessionImplementor session) throws HibernateException, SQLException {

    checkInitialization();/*w w  w.  java2  s. c  o  m*/

    if (value == null) {
        st.setNull(index, sqlType);
    } else {
        final byte[] encryptedValue = this.encryptor.encrypt((byte[]) value);
        if (Environment.useStreamsForBinary()) {
            st.setBinaryStream(index, new ByteArrayInputStream(encryptedValue), encryptedValue.length);
        } else {
            st.setBytes(index, encryptedValue);
        }
    }

}

From source file:org.jasypt.hibernate5.type.EncryptedBinaryType.java

License:Apache License

public Object nullSafeGet(final ResultSet rs, final String[] names,
        final SharedSessionContractImplementor session, final Object owner)
        throws HibernateException, SQLException {

    checkInitialization();//from   www. j  a va 2  s.c  o m

    byte[] encryptedValue = null;
    if (Environment.useStreamsForBinary()) {

        final InputStream inputStream = rs.getBinaryStream(names[0]);
        if (rs.wasNull()) {
            return null;
        }

        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BLOCK_SIZE);
        final byte[] inputBuff = new byte[BLOCK_SIZE];
        try {
            int readBytes = 0;
            while (readBytes != -1) {
                readBytes = inputStream.read(inputBuff);
                if (readBytes != -1) {
                    outputStream.write(inputBuff, 0, readBytes);
                }
            }
        } catch (IOException e) {
            throw new HibernateException("IOException occurred reading a binary value", e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                // exception ignored
            }
            try {
                outputStream.close();
            } catch (IOException e) {
                // exception ignored
            }
        }

        encryptedValue = outputStream.toByteArray();

    } else {

        encryptedValue = rs.getBytes(names[0]);
        if (rs.wasNull()) {
            return null;
        }

    }

    return this.encryptor.decrypt(encryptedValue);

}

From source file:org.jasypt.hibernate5.type.EncryptedBinaryType.java

License:Apache License

public void nullSafeSet(final PreparedStatement st, final Object value, final int index,
        final SharedSessionContractImplementor session) throws HibernateException, SQLException {

    checkInitialization();//from   ww w  .  ja  v  a2 s  .  c o  m

    if (value == null) {
        st.setNull(index, sqlType);
    } else {
        final byte[] encryptedValue = this.encryptor.encrypt((byte[]) value);
        if (Environment.useStreamsForBinary()) {
            st.setBinaryStream(index, new ByteArrayInputStream(encryptedValue), encryptedValue.length);
        } else {
            st.setBytes(index, encryptedValue);
        }
    }

}