Example usage for java.sql Blob length

List of usage examples for java.sql Blob length

Introduction

In this page you can find the example usage for java.sql Blob length.

Prototype

long length() throws SQLException;

Source Link

Document

Returns the number of bytes in the BLOB value designated by this Blob object.

Usage

From source file:com.formkiq.core.dao.AbstractDaoImpl.java

@Override
public byte[] toBytesArray(final Blob blob) {

    try {//from  w  w w.jav  a2 s . c o  m
        int blobLength = (int) blob.length();
        byte[] blobAsBytes = blob.getBytes(1, blobLength);
        return blobAsBytes;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.beangle.ems.avatar.service.DataBaseAvatarBase.java

protected byte[] getBytes(String name) {
    SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql, name);
    Blob obj = null;
    if (rowSet.next()) {
        obj = (Blob) rowSet.getObject(1);
        try {//from  w ww. j  av a2 s  .c  o  m
            return obj.getBytes(1L, (int) obj.length());
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    } else {
        return null;
    }
}

From source file:com.npower.dm.hibernate.entity.SoftwarePackageEntity.java

public int getSize() throws Exception {
    int size = 0;
    DMBinary b = this.getBinary();
    if (b != null) {
        Blob blob = b.getBinaryBlob();
        if (blob != null) {
            size = (int) blob.length();
        }// ww w. j  a  v  a  2s . c o  m
    }
    return size;
}

From source file:org.jboss.dashboard.database.hibernate.BinaryBlobType.java

public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws HibernateException, SQLException {
    if (log.isDebugEnabled())
        log.debug("Getting value with names " + Arrays.asList(names));

    Object o = rs.getObject(names[0]);
    if (o == null) {
        return null;
    } else if (o instanceof Blob) {
        final Blob blob = (Blob) o;
        if (blob == null)
            return null;
        return blob.getBytes(1, (int) blob.length());
    } else if (o instanceof byte[]) {
        return o;
    } else {/*  w w  w .j  a va 2 s. com*/
        throw new IllegalArgumentException(
                "Unexpected value read. Must be Blob or byte[], but it is " + o.getClass());
    }
}

From source file:org.jboss.dashboard.database.hibernate.StringBlobType.java

public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws HibernateException, SQLException {
    if (log.isDebugEnabled())
        log.debug("Getting value with names " + Arrays.asList(names));
    Object o = rs.getObject(names[0]);
    if (o == null) {
        return null;
    } else if (o instanceof Blob) {
        final Blob blob = (Blob) o;
        try {/*w w  w  .j a v  a2s .c  o  m*/
            byte bytes[] = blob.getBytes(1, (int) blob.length());
            if (bytes == null)
                return null;
            return new String(bytes, STRING_ENCODING);
        } catch (UnsupportedEncodingException e) {
            log.error("Error:", e);
            return new String(blob.getBytes(1, (int) blob.length()));
        }
    } else if (o instanceof String) {
        return o;
    } else if (o instanceof byte[]) {
        try {
            return new String((byte[]) o, STRING_ENCODING);
        } catch (UnsupportedEncodingException e) {
            log.error("Error: ", e);
            return null;
        }
    } else if (o instanceof Clob) {
        //added for H2 support
        final Clob clob = (Clob) o;
        return clob.getSubString(1, (int) clob.length());
    } else {
        throw new IllegalArgumentException(
                "Unexpected value read. Must be Blob or String, but it is " + o.getClass());
    }
}

From source file:org.quartz.impl.jdbcjobstore.WebLogicDelegate.java

/**
 * <p>/*from  w  ww. j  a  v a  2  s.  com*/
 * This method should be overridden by any delegate subclasses that need
 * special handling for BLOBs. The default implementation uses standard
 * JDBC <code>java.sql.Blob</code> operations.
 * </p>
 * 
 * @param rs
 *          the result set, already queued to the correct row
 * @param colName
 *          the column name for the BLOB
 * @return the deserialized Object from the ResultSet BLOB
 * @throws ClassNotFoundException
 *           if a class found during deserialization cannot be found
 * @throws IOException
 *           if deserialization causes an error
 */
protected Object getObjectFromBlob(ResultSet rs, String colName)
        throws ClassNotFoundException, IOException, SQLException {

    Object obj = null;

    Blob blobLocator = rs.getBlob(colName);
    InputStream binaryInput = null;
    try {
        if (null != blobLocator && blobLocator.length() > 0) {
            binaryInput = blobLocator.getBinaryStream();
        }
    } catch (Exception ignore) {
    }

    if (null != binaryInput) {
        ObjectInputStream in = new ObjectInputStream(binaryInput);
        try {
            obj = in.readObject();
        } finally {
            in.close();
        }
    }

    return obj;
}

From source file:org.pentaho.platform.repository.hibernate.usertypes.BlobtoByteArrayUserType.java

public Object nullSafeGet(final ResultSet arg0, final String[] arg1, final Object arg2)
        throws HibernateException, SQLException {

    Blob blob = arg0.getBlob(arg1[0]);
    if (blob != null) {
        byte[] bytes = blob.getBytes(1, (int) blob.length());
        return bytes;
    }// w  w  w.  j  a v a 2  s .  c  om
    return null;
}

From source file:org.quartz.impl.jdbcjobstore.WebLogicDelegate.java

protected Object getJobDetailFromBlob(ResultSet rs, String colName)
        throws ClassNotFoundException, IOException, SQLException {

    if (canUseProperties()) {
        Blob blobLocator = rs.getBlob(colName);
        InputStream binaryInput = null;
        try {/*w w  w .jav  a 2  s.  com*/
            if (null != blobLocator && blobLocator.length() > 0) {
                binaryInput = blobLocator.getBinaryStream();
            }
        } catch (Exception ignore) {
        }
        return binaryInput;
    }

    return getObjectFromBlob(rs, colName);
}

From source file:com.mirth.connect.server.mule.transformers.ResultMapToXML.java

public Object doTransform(Object source) throws TransformerException {
    if (source instanceof Map) {
        Map data = (Map) source;

        try {// w ww.  j a  v  a 2 s  .co m
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            Element root = document.createElement("result");
            document.appendChild(root);

            for (Iterator iter = data.keySet().iterator(); iter.hasNext();) {
                String key = (String) iter.next();
                Element child = document.createElement(key);
                String value = new String();
                Object objectValue = data.get(key);
                if (objectValue != null) {
                    if (objectValue instanceof byte[]) {
                        value = new String((byte[]) objectValue);
                    } else if (objectValue instanceof java.sql.Clob) {
                        // convert it to a string
                        java.sql.Clob clobValue = (java.sql.Clob) objectValue;
                        Reader reader = clobValue.getCharacterStream();
                        if (reader == null) {
                            value = "";
                        }
                        StringBuffer sb = new StringBuffer();
                        try {
                            char[] charbuf = new char[(int) clobValue.length()];
                            for (int i = reader.read(charbuf); i > 0; i = reader.read(charbuf)) {
                                sb.append(charbuf, 0, i);
                            }
                        } catch (IOException e) {
                            logger.error("Error reading clob value.\n" + ExceptionUtils.getStackTrace(e));

                        }
                        value = sb.toString();
                    } else if (objectValue instanceof java.sql.Blob) {
                        try {
                            java.sql.Blob blobValue = (java.sql.Blob) objectValue;
                            value = new String(blobValue.getBytes(1, (int) blobValue.length()));
                        } catch (Exception ex) {
                            logger.error("Error reading blob value.\n" + ExceptionUtils.getStackTrace(ex));
                        }
                    } else {
                        value = objectValue.toString();
                    }

                }
                child.appendChild(document.createTextNode(value));
                root.appendChild(child);
            }

            DocumentSerializer docSerializer = new DocumentSerializer();
            return docSerializer.toXML(document);
        } catch (Exception e) {
            throw new TransformerException(
                    org.mule.config.i18n.Message.createStaticMessage("Failed to parse result map"), this);
        }
    } else if (source instanceof String) {
        return source.toString();
    } else {
        throw new TransformerException(
                org.mule.config.i18n.Message.createStaticMessage("Unregistered result type"), this);
    }
}

From source file:com.trackplus.ddl.GenericStringValueConverter.java

protected String extractColumnValue(ResultSet resultSet, int columnIdx, int jdbcType)
        throws SQLException, DDLException {
    String value = resultSet.getString(columnIdx);
    if (value != null) {
        switch (jdbcType) {
        case Types.NUMERIC:
        case Types.DECIMAL:
            break;
        case Types.BIT:
        case Types.BOOLEAN:
        case Types.TINYINT:
        case Types.SMALLINT:
        case Types.INTEGER:
        case Types.BIGINT:
        case Types.REAL:
        case Types.FLOAT:
        case Types.DOUBLE: {
            break;
        }// w ww .j  a va  2s . co  m

        case Types.CHAR:
        case Types.VARCHAR:
        case Types.LONGVARCHAR:
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.TIME:
        case Types.CLOB:
        case Types.ARRAY:
        case Types.REF: {
            value = "'" + value.replaceAll("'", "''") + "'";
            break;
        }
        case Types.DATE:
        case Types.TIMESTAMP: {
            Date d = resultSet.getDate(columnIdx);
            Calendar cal = Calendar.getInstance();
            cal.setTime(d);
            int year = cal.get(Calendar.YEAR);
            if (year < 1900) {
                throw new DDLException("Invalid date:" + d);
            } else {
                value = "'" + value + "'";
            }
            break;
        }
        case Types.BLOB:
        case Types.LONGVARBINARY: {
            Blob blobValue = resultSet.getBlob(columnIdx);
            String str = new String(Base64.encodeBase64(blobValue.getBytes(1l, (int) blobValue.length())));
            value = "'" + str + "'";
            break;
        }
        default:
            break;
        }
    }
    return value;
}