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:edu.umass.cs.gigapaxos.SQLPaxosLogger.java

private static byte[] lobToBytes(Blob blob) throws SQLException, IOException {
    if (blob == null)
        return null;
    byte[] blobBytes = blob.getBytes(1L, (int) blob.length());
    assert (blobBytes != null);
    return inflate(blobBytes);
    // return new String(inflate(blobBytes), CHARSET);
}

From source file:com.znsx.cms.web.controller.TmDeviceController.java

@InterfaceDescription(logon = false, method = "Get_Vms_Command", cmd = "3017")
@RequestMapping("/get_vms_command.xml")
public void getVmsCommand(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Document requestDoc = RequestUtil.parseRequest(request);
    Element reqRoot = requestDoc.getRootElement();
    Element e = reqRoot.getChild("CommandId");
    if (null == e) {
        throw new BusinessException(ErrorCode.PARAMETER_NOT_FOUND, "missing [CommandId]");
    }// w ww  . ja v a2  s. co  m

    String commandId = e.getText();
    if (StringUtils.isBlank(commandId)) {
        throw new BusinessException(ErrorCode.PARAMETER_NOT_FOUND, "missing [CommandId]");
    }

    CmsCommand command = tmDeviceManager.getCmsCommand(commandId);
    Blob blob = command.getContent();
    long length = blob.length();
    if (length >= Integer.MAX_VALUE) {
        throw new BusinessException(ErrorCode.ERROR,
                "Image length too long, can not encode by BASE64 encoding !");
    }
    int intLength = (int) length;

    BaseDTO dto = new BaseDTO();
    dto.setCmd("3017");
    dto.setMethod("Get_Vms_Command");
    Document doc = new Document();
    Element root = ElementUtil.createElement("Response", dto);
    doc.setRootElement(root);

    Element commandElement = new Element("Command");
    commandElement
            .setText(new String(Base64.encodeBase64(command.getContent().getBytes(1, intLength)), "utf8"));
    root.addContent(commandElement);

    writePageWithContentLength(response, doc);
}

From source file:org.pentaho.di.core.database.Database.java

public RowMetaAndData callProcedure(String[] arg, String[] argdir, int[] argtype, String resultname,
        int resulttype) throws KettleDatabaseException {
    RowMetaAndData ret;//from   w w  w  .  jav  a 2  s  .c  om
    try {
        boolean moreResults = cstmt.execute();
        ret = new RowMetaAndData();
        int pos = 1;
        if (resultname != null && resultname.length() != 0) {
            ValueMeta vMeta = new ValueMeta(resultname, resulttype);
            Object v = null;
            switch (resulttype) {
            case ValueMetaInterface.TYPE_BOOLEAN:
                v = Boolean.valueOf(cstmt.getBoolean(pos));
                break;
            case ValueMetaInterface.TYPE_NUMBER:
                v = new Double(cstmt.getDouble(pos));
                break;
            case ValueMetaInterface.TYPE_BIGNUMBER:
                v = cstmt.getBigDecimal(pos);
                break;
            case ValueMetaInterface.TYPE_INTEGER:
                v = Long.valueOf(cstmt.getLong(pos));
                break;
            case ValueMetaInterface.TYPE_STRING:
                v = cstmt.getString(pos);
                break;
            case ValueMetaInterface.TYPE_BINARY:
                if (databaseMeta.supportsGetBlob()) {
                    Blob blob = cstmt.getBlob(pos);
                    if (blob != null) {
                        v = blob.getBytes(1L, (int) blob.length());
                    } else {
                        v = null;
                    }
                } else {
                    v = cstmt.getBytes(pos);
                }
                break;
            case ValueMetaInterface.TYPE_DATE:
                if (databaseMeta.supportsTimeStampToDateConversion()) {
                    v = cstmt.getTimestamp(pos);
                } else {
                    v = cstmt.getDate(pos);
                }
                break;
            default:
                break;
            }
            ret.addValue(vMeta, v);
            pos++;
        }
        for (int i = 0; i < arg.length; i++) {
            if (argdir[i].equalsIgnoreCase("OUT") || argdir[i].equalsIgnoreCase("INOUT")) {
                ValueMetaInterface vMeta = ValueMetaFactory.createValueMeta(arg[i], argtype[i]);
                Object v = null;
                switch (argtype[i]) {
                case ValueMetaInterface.TYPE_BOOLEAN:
                    v = Boolean.valueOf(cstmt.getBoolean(pos + i));
                    break;
                case ValueMetaInterface.TYPE_NUMBER:
                    v = new Double(cstmt.getDouble(pos + i));
                    break;
                case ValueMetaInterface.TYPE_BIGNUMBER:
                    v = cstmt.getBigDecimal(pos + i);
                    break;
                case ValueMetaInterface.TYPE_INTEGER:
                    v = Long.valueOf(cstmt.getLong(pos + i));
                    break;
                case ValueMetaInterface.TYPE_STRING:
                    v = cstmt.getString(pos + i);
                    break;
                case ValueMetaInterface.TYPE_BINARY:
                    if (databaseMeta.supportsGetBlob()) {
                        Blob blob = cstmt.getBlob(pos + i);
                        if (blob != null) {
                            v = blob.getBytes(1L, (int) blob.length());
                        } else {
                            v = null;
                        }
                    } else {
                        v = cstmt.getBytes(pos + i);
                    }
                    break;
                case ValueMetaInterface.TYPE_DATE:
                    if (databaseMeta.supportsTimeStampToDateConversion()) {
                        v = cstmt.getTimestamp(pos + i);
                    } else {
                        v = cstmt.getDate(pos + i);
                    }
                    break;
                default:
                    break;
                }
                ret.addValue(vMeta, v);
            }
        }
        ResultSet rs = null;
        int updateCount = -1;

        // CHE: Iterate through the result sets and update counts
        // to receive all error messages from within the stored procedure.
        // This is only the first step to ensure that the stored procedure
        // is properly executed. A future extension would be to return all
        // result sets and update counts properly.

        do {
            rs = null;
            try {
                // Save the result set
                if (moreResults) {
                    rs = cstmt.getResultSet();

                } else {
                    // Save the update count if it is available (> -1)
                    updateCount = cstmt.getUpdateCount();

                }

                moreResults = cstmt.getMoreResults();

            } finally {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
            }

        } while (moreResults || (updateCount > -1));

        return ret;
    } catch (Exception ex) {
        throw new KettleDatabaseException("Unable to call procedure", ex);
    }

}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

/**
 * Convert the specified column of the SQL ResultSet to the proper
 * java type./*from   w ww .  ja v a 2s . c o m*/
 */
public Object getBlobObject(ResultSet rs, int column, JDBCStore store) throws SQLException {
    InputStream in = null;
    if (useGetBytesForBlobs || useGetObjectForBlobs) {
        byte[] bytes = getBytes(rs, column);
        if (bytes != null && bytes.length > 0)
            in = new ByteArrayInputStream(bytes);
    } else {
        Blob blob = getBlob(rs, column);
        if (blob != null && blob.length() > 0)
            in = blob.getBinaryStream();
    }
    if (in == null)
        return null;

    try {
        if (store == null)
            return Serialization.deserialize(in, null);
        return Serialization.deserialize(in, store.getContext());
    } finally {
        try {
            in.close();
        } catch (IOException ioe) {
        }
    }
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

/**
 * Convert the specified column of the SQL ResultSet to the proper
 * java type./*from  w  w w .j a  v  a2 s .  c  o  m*/
 */
public byte[] getBytes(ResultSet rs, int column) throws SQLException {
    if (useGetBytesForBlobs)
        return rs.getBytes(column);
    if (useGetObjectForBlobs)
        return (byte[]) rs.getObject(column);

    Blob blob = getBlob(rs, column);
    if (blob == null)
        return null;
    int length = (int) blob.length();
    if (length == 0)
        return null;
    return blob.getBytes(1, length);
}

From source file:com.pari.nm.utils.db.InventoryDBHelper.java

public static String loadStartupConfig(int nodeId) {
    ResultSet rs = null;/*from   w  w  w .  j  a  va2s.  co  m*/
    try {
        rs = DBHelper.executeQuery("select config from node_current_config where id=" + nodeId
                + " AND conf_type=" + Constants.STARTUP_CONF);
        if (rs.next()) {
            byte b[] = null;
            if (ServerProperties.getInstance().isPostgres()) {
                b = rs.getBytes(1);
            } else {
                Blob blob = rs.getBlob(1);
                if (blob != null) {
                    b = blob.getBytes(1, (int) blob.length());
                }
            }

            if (b != null) {
                return CompressionUtils.getUncompressedString(b);

            }
        }
    } catch (Exception ex) {
        logger.error("Exception while loading startup config for device: " + nodeId, ex);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception ex) {
            }
        }
    }
    return "";
}

From source file:com.pari.nm.utils.db.InventoryDBHelper.java

public static String loadRunningConfig(int nodeId) {
    ResultSet rs = null;/*from  www. j a va2 s . c  o  m*/
    Connection c = null;
    PreparedStatement ps = null;
    try {
        c = DBHelper.getConnection();
        ps = c.prepareStatement(
                "select config from node_current_config where id= ? AND conf_type=" + Constants.RUN_CONF);
        // rs =
        // DBHelper.executeQuery("select config from node_current_config where id="
        // + nodeId +
        // " AND conf_type="
        // + Constants.STARTUP_CONF);
        ps.setInt(1, nodeId);
        rs = ps.executeQuery();
        if (rs.next()) {
            byte b[] = null;
            if (ServerProperties.getInstance().isPostgres()) {
                b = rs.getBytes(1);
            } else {
                Blob blob = rs.getBlob(1);
                if (blob != null) {
                    b = blob.getBytes(1, (int) blob.length());
                }
            }
            if (b != null) {
                return CompressionUtils.getUncompressedString(b);
            }
        }
    } catch (Exception ex) {
        logger.error("Exception while loading running config for device: " + nodeId, ex);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception ex) {
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception e) {

            }
        }
        DBHelper.releaseConnection(c);
    }
    return "";
}

From source file:com.pari.nm.utils.db.InventoryDBHelper.java

public static String getShowCli(int nodeId, String cli) {
    ResultSet rs = null;//from w ww  . j  a  v  a 2  s .  com
    Connection c = null;
    PreparedStatement ps = null;
    String sql = "select out_put from show_commands_new where device_id=? and cli=?";
    try {
        c = DBHelper.getConnection();
        ps = c.prepareStatement(sql);
        ps.setInt(1, nodeId);
        ps.setString(2, cli);
        rs = ps.executeQuery();
        byte b[] = null;
        if (rs != null && rs.next()) {
            if (ServerProperties.getInstance().isPostgres()) {
                b = rs.getBytes(1);
            } else {
                Blob blob = rs.getBlob(1);
                if (blob != null) {
                    b = blob.getBytes(1, (int) blob.length());
                }
            }

            if (b != null) {
                return CompressionUtils.getUncompressedString(b);
            }
        }

    } catch (Exception ee) {
        logger.warn("Error while getting show cli for the node = " + nodeId + " and cli = " + cli, ee);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception ex) {
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception ee) {
            }
        }
        if (c != null) {
            try {
                DBHelper.releaseConnection(c);
            } catch (Exception ex) {
            }
        }
    }
    return "";
}