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:it.greenvulcano.gvesb.datahandling.dbo.utils.StandardRowSetBuilder.java

public int build(Document doc, String id, ResultSet rs, Set<Integer> keyField,
        Map<String, FieldFormatter> fieldNameToFormatter, Map<String, FieldFormatter> fieldIdToFormatter)
        throws Exception {
    if (rs == null) {
        return 0;
    }/* w w w  .  j  a va2 s.  co m*/
    int rowCounter = 0;
    Element docRoot = doc.getDocumentElement();
    ResultSetMetaData metadata = rs.getMetaData();
    FieldFormatter[] fFormatters = buildFormatterArray(metadata, fieldNameToFormatter, fieldIdToFormatter);

    boolean noKey = ((keyField == null) || keyField.isEmpty());

    //boolean isNull = false;
    Element data = null;
    Element row = null;
    Element col = null;
    Text text = null;
    String textVal = null;
    String precKey = null;
    String colKey = null;
    Map<String, String> keyAttr = new HashMap<String, String>();
    while (rs.next()) {
        if (rowCounter % 10 == 0) {
            ThreadUtils.checkInterrupted(getClass().getSimpleName(), name, logger);
        }
        row = parser.createElement(doc, AbstractDBO.ROW_NAME);

        parser.setAttribute(row, AbstractDBO.ID_NAME, id);
        for (int j = 1; j <= metadata.getColumnCount(); j++) {
            FieldFormatter fF = fFormatters[j];

            //isNull = false;
            col = parser.createElement(doc, AbstractDBO.COL_NAME);
            switch (metadata.getColumnType(j)) {
            case Types.DATE: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.DATE_TYPE);
                java.sql.Date dateVal = rs.getDate(j);
                textVal = processDateTime(col, fF, dateVal, AbstractDBO.DEFAULT_DATE_FORMAT);
            }
                break;
            case Types.TIME: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.TIME_TYPE);
                java.sql.Time dateVal = rs.getTime(j);
                textVal = processDateTime(col, fF, dateVal, AbstractDBO.DEFAULT_TIME_FORMAT);
            }
                break;
            case Types.TIMESTAMP: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.TIMESTAMP_TYPE);
                Timestamp dateVal = rs.getTimestamp(j);
                textVal = processDateTime(col, fF, dateVal, AbstractDBO.DEFAULT_DATE_FORMAT);
            }
                break;
            case Types.DOUBLE: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.FLOAT_TYPE);
                double numVal = rs.getDouble(j);
                textVal = processDouble(col, fF, numVal);
            }
                break;
            case Types.FLOAT:
            case Types.REAL: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.FLOAT_TYPE);
                float numVal = rs.getFloat(j);
                textVal = processDouble(col, fF, numVal);
            }
                break;
            case Types.BIGINT: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.BIGINT_TYPE);
                long numVal = rs.getLong(j);
                parser.setAttribute(col, AbstractDBO.NULL_NAME, "false");
                textVal = String.valueOf(numVal);
            }
                break;
            case Types.INTEGER: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.INTEGER_TYPE);
                int numVal = rs.getInt(j);
                parser.setAttribute(col, AbstractDBO.NULL_NAME, "false");
                textVal = String.valueOf(numVal);
            }
                break;
            case Types.SMALLINT:
            case Types.TINYINT: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.SMALLINT_TYPE);
                short numVal = rs.getShort(j);
                parser.setAttribute(col, AbstractDBO.NULL_NAME, "false");
                textVal = String.valueOf(numVal);
            }
                break;
            case Types.NUMERIC:
            case Types.DECIMAL: {
                BigDecimal bigdecimal = rs.getBigDecimal(j);
                boolean isNull = bigdecimal == null;
                parser.setAttribute(col, AbstractDBO.NULL_NAME, String.valueOf(isNull));
                if (isNull) {
                    if (metadata.getScale(j) > 0) {
                        parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.DECIMAL_TYPE);
                    } else {
                        parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.NUMERIC_TYPE);
                    }
                    textVal = "";
                } else {
                    if (fF != null) {
                        parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.DECIMAL_TYPE);
                        parser.setAttribute(col, AbstractDBO.FORMAT_NAME, fF.getNumberFormat());
                        parser.setAttribute(col, AbstractDBO.GRP_SEPARATOR_NAME, fF.getGroupSeparator());
                        parser.setAttribute(col, AbstractDBO.DEC_SEPARATOR_NAME, fF.getDecSeparator());
                        textVal = fF.formatNumber(bigdecimal);
                    } else if (metadata.getScale(j) > 0) {
                        parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.DECIMAL_TYPE);
                        parser.setAttribute(col, AbstractDBO.FORMAT_NAME, numberFormat);
                        parser.setAttribute(col, AbstractDBO.GRP_SEPARATOR_NAME, groupSeparator);
                        parser.setAttribute(col, AbstractDBO.DEC_SEPARATOR_NAME, decSeparator);
                        textVal = numberFormatter.format(bigdecimal);
                    } else {
                        parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.NUMERIC_TYPE);
                        textVal = bigdecimal.toString();
                    }
                }
            }
                break;
            case Types.BOOLEAN: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.BOOLEAN_TYPE);
                boolean bVal = rs.getBoolean(j);
                parser.setAttribute(col, AbstractDBO.NULL_NAME, "false");
                textVal = String.valueOf(bVal);
            }
                break;
            case Types.SQLXML: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.XML_TYPE);
                SQLXML xml = rs.getSQLXML(j);
                boolean isNull = xml == null;
                parser.setAttribute(col, AbstractDBO.NULL_NAME, String.valueOf(isNull));
                if (isNull) {
                    textVal = "";
                } else {
                    textVal = xml.getString();
                }
            }
                break;
            case Types.NCHAR:
            case Types.NVARCHAR: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.NSTRING_TYPE);
                textVal = rs.getNString(j);
                if (textVal == null) {
                    textVal = "";
                }
            }
                break;
            case Types.CHAR:
            case Types.VARCHAR: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.STRING_TYPE);
                textVal = rs.getString(j);
                boolean isNull = textVal == null;
                parser.setAttribute(col, AbstractDBO.NULL_NAME, String.valueOf(isNull));
                if (isNull) {
                    textVal = "";
                }
            }
                break;
            case Types.NCLOB: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.LONG_NSTRING_TYPE);
                NClob clob = rs.getNClob(j);
                if (clob != null) {
                    Reader is = clob.getCharacterStream();
                    StringWriter str = new StringWriter();

                    IOUtils.copy(is, str);
                    is.close();
                    textVal = str.toString();
                } else {
                    textVal = "";
                }
            }
                break;
            case Types.CLOB: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.LONG_STRING_TYPE);
                Clob clob = rs.getClob(j);
                if (clob != null) {
                    Reader is = clob.getCharacterStream();
                    StringWriter str = new StringWriter();

                    IOUtils.copy(is, str);
                    is.close();
                    textVal = str.toString();
                } else {
                    textVal = "";
                }
            }
                break;
            case Types.BLOB: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.BASE64_TYPE);
                Blob blob = rs.getBlob(j);
                boolean isNull = blob == null;
                parser.setAttribute(col, AbstractDBO.NULL_NAME, String.valueOf(isNull));
                if (isNull) {
                    textVal = "";
                } else {
                    InputStream is = blob.getBinaryStream();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    IOUtils.copy(is, baos);
                    is.close();
                    try {
                        byte[] buffer = Arrays.copyOf(baos.toByteArray(), (int) blob.length());
                        textVal = Base64.getEncoder().encodeToString(buffer);
                    } catch (SQLFeatureNotSupportedException exc) {
                        textVal = Base64.getEncoder().encodeToString(baos.toByteArray());
                    }
                }
            }
                break;
            default: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.DEFAULT_TYPE);
                textVal = rs.getString(j);
                boolean isNull = textVal == null;
                parser.setAttribute(col, AbstractDBO.NULL_NAME, String.valueOf(isNull));
                if (isNull) {
                    textVal = "";
                }
            }
            }
            if (textVal != null) {
                text = doc.createTextNode(textVal);
                col.appendChild(text);
            }
            if (!noKey && keyField.contains(new Integer(j))) {
                if (textVal != null) {
                    if (colKey == null) {
                        colKey = textVal;
                    } else {
                        colKey += "##" + textVal;
                    }
                    keyAttr.put("key_" + j, textVal);
                }
            } else {
                row.appendChild(col);
            }
        }
        if (noKey) {
            if (data == null) {
                data = parser.createElement(doc, AbstractDBO.DATA_NAME);
                parser.setAttribute(data, AbstractDBO.ID_NAME, id);
            }
        } else if ((colKey != null) && !colKey.equals(precKey)) {
            if (data != null) {
                docRoot.appendChild(data);
            }
            data = parser.createElement(doc, AbstractDBO.DATA_NAME);
            parser.setAttribute(data, AbstractDBO.ID_NAME, id);
            for (Entry<String, String> keyAttrEntry : keyAttr.entrySet()) {
                parser.setAttribute(data, keyAttrEntry.getKey(), keyAttrEntry.getValue());
            }
            keyAttr.clear();
            precKey = colKey;
        }
        colKey = null;
        data.appendChild(row);
        rowCounter++;
    }
    if (data != null) {
        docRoot.appendChild(data);
    }

    return rowCounter;
}

From source file:com.healthmarketscience.jackcess.Column.java

/**
 * @return an appropriate byte[] representation of the given object.
 * @usage _advanced_method_/*from  w  w  w.  j a v a 2 s .co  m*/
 */
public static byte[] toByteArray(Object value) throws IOException {
    if (value == null) {
        return null;
    } else if (value instanceof byte[]) {
        return (byte[]) value;
    } else if (value instanceof Blob) {
        try {
            Blob b = (Blob) value;
            // note, start pos is 1-based
            return b.getBytes(1L, (int) b.length());
        } catch (SQLException e) {
            throw (IOException) (new IOException(e.getMessage())).initCause(e);
        }
    }

    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    if (value instanceof InputStream) {
        byte[] buf = new byte[8 * 1024];
        InputStream in = (InputStream) value;
        int read = 0;
        while ((read = in.read(buf)) != -1) {
            bout.write(buf, 0, read);
        }
    } else {
        // if all else fails, serialize it
        ObjectOutputStream oos = new ObjectOutputStream(bout);
        oos.writeObject(value);
        oos.close();
    }

    return bout.toByteArray();
}

From source file:net.sourceforge.msscodefactory.v1_10.MSSBamPg8.MSSBamPg8TableBlobTable.java

protected MSSBamTableBlobBuff unpackTableBlobResultSetToBuff(ResultSet resultSet) throws SQLException {
    final String S_ProcName = "unpackTableBlobResultSetToBuff";
    int idxcol = 1;
    String classCode = resultSet.getString(idxcol);
    idxcol++;//from  w w w.j  a v a 2s  . c o  m
    MSSBamTableBlobBuff buff;
    if (classCode.equals("TBLB")) {
        buff = schema.getFactoryTableBlob().newBuff();
    } else {
        throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                "Unrecognized class code \"" + classCode + "\"");
    }
    buff.setRequiredId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredTenantId(resultSet.getLong(idxcol));
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalScopeId(null);
        } else {
            buff.setOptionalScopeId(colVal);
        }
    }
    idxcol++;
    buff.setRequiredName(resultSet.getString(idxcol));
    idxcol++;
    {
        String colVal = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalShortName(null);
        } else {
            buff.setOptionalShortName(colVal);
        }
    }
    idxcol++;
    {
        String colVal = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalLabel(null);
        } else {
            buff.setOptionalLabel(colVal);
        }
    }
    idxcol++;
    {
        String colVal = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalShortDescription(null);
        } else {
            buff.setOptionalShortDescription(colVal);
        }
    }
    idxcol++;
    {
        String colVal = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalDescription(null);
        } else {
            buff.setOptionalDescription(colVal);
        }
    }
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalAuthorId(null);
        } else {
            buff.setOptionalAuthorId(colVal);
        }
    }
    idxcol++;
    buff.setRequiredValueContainerId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredIsNullable(resultSet.getBoolean(idxcol));
    idxcol++;
    {
        boolean colVal = resultSet.getBoolean(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalGenerateId(null);
        } else {
            buff.setOptionalGenerateId(colVal);
        }
    }
    idxcol++;
    {
        short colVal = resultSet.getShort(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalDataScopeId(null);
        } else {
            buff.setOptionalDataScopeId(colVal);
        }
    }
    idxcol++;
    {
        short colVal = resultSet.getShort(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalViewAccessSecurityId(null);
        } else {
            buff.setOptionalViewAccessSecurityId(colVal);
        }
    }
    idxcol++;
    {
        short colVal = resultSet.getShort(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalEditAccessSecurityId(null);
        } else {
            buff.setOptionalEditAccessSecurityId(colVal);
        }
    }
    idxcol++;
    {
        short colVal = resultSet.getShort(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalViewAccessFrequencyId(null);
        } else {
            buff.setOptionalViewAccessFrequencyId(colVal);
        }
    }
    idxcol++;
    {
        short colVal = resultSet.getShort(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalEditAccessFrequencyId(null);
        } else {
            buff.setOptionalEditAccessFrequencyId(colVal);
        }
    }
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalPrevId(null);
        } else {
            buff.setOptionalPrevId(colVal);
        }
    }
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalNextId(null);
        } else {
            buff.setOptionalNextId(colVal);
        }
    }
    idxcol++;
    {
        String colVal = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalDbName(null);
        } else {
            buff.setOptionalDbName(colVal);
        }
    }
    idxcol++;
    buff.setRequiredMaxLen(resultSet.getInt(idxcol));
    idxcol++;
    {
        Blob binding = resultSet.getBlob(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalInitValue(null);
        } else {
            long bindLen = binding.length();
            if (bindLen > Integer.MAX_VALUE) {
                throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName,
                        idxcol, "InitValue", bindLen, (long) (Integer.MAX_VALUE));
            }
            byte[] colVal = binding.getBytes(0, (int) bindLen);
            buff.setOptionalInitValue(colVal);
        }
    }
    idxcol++;
    {
        Blob binding = resultSet.getBlob(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalDefaultValue(null);
        } else {
            long bindLen = binding.length();
            if (bindLen > Integer.MAX_VALUE) {
                throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName,
                        idxcol, "DefaultValue", bindLen, (long) (Integer.MAX_VALUE));
            }
            byte[] colVal = binding.getBytes(0, (int) bindLen);
            buff.setOptionalDefaultValue(colVal);
        }
    }
    idxcol++;
    {
        Blob binding = resultSet.getBlob(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalNullValue(null);
        } else {
            long bindLen = binding.length();
            if (bindLen > Integer.MAX_VALUE) {
                throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName,
                        idxcol, "NullValue", bindLen, (long) (Integer.MAX_VALUE));
            }
            byte[] colVal = binding.getBytes(0, (int) bindLen);
            buff.setOptionalNullValue(colVal);
        }
    }
    idxcol++;
    {
        Blob binding = resultSet.getBlob(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalUnknownValue(null);
        } else {
            long bindLen = binding.length();
            if (bindLen > Integer.MAX_VALUE) {
                throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName,
                        idxcol, "UnknownValue", bindLen, (long) (Integer.MAX_VALUE));
            }
            byte[] colVal = binding.getBytes(0, (int) bindLen);
            buff.setOptionalUnknownValue(colVal);
        }
    }
    idxcol++;
    buff.setRequiredContainerId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredRevision(resultSet.getInt(idxcol));
    return (buff);
}

From source file:net.sourceforge.msscodefactory.v1_10.MSSBamPg8.MSSBamPg8SchemaBlobTable.java

protected MSSBamSchemaBlobBuff unpackSchemaBlobResultSetToBuff(ResultSet resultSet) throws SQLException {
    final String S_ProcName = "unpackSchemaBlobResultSetToBuff";
    int idxcol = 1;
    String classCode = resultSet.getString(idxcol);
    idxcol++;/*from w w w . ja  v a2  s .  c o m*/
    MSSBamSchemaBlobBuff buff;
    if (classCode.equals("SBLB")) {
        buff = schema.getFactorySchemaBlob().newBuff();
    } else {
        throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                "Unrecognized class code \"" + classCode + "\"");
    }
    buff.setRequiredId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredTenantId(resultSet.getLong(idxcol));
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalScopeId(null);
        } else {
            buff.setOptionalScopeId(colVal);
        }
    }
    idxcol++;
    buff.setRequiredName(resultSet.getString(idxcol));
    idxcol++;
    {
        String colVal = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalShortName(null);
        } else {
            buff.setOptionalShortName(colVal);
        }
    }
    idxcol++;
    {
        String colVal = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalLabel(null);
        } else {
            buff.setOptionalLabel(colVal);
        }
    }
    idxcol++;
    {
        String colVal = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalShortDescription(null);
        } else {
            buff.setOptionalShortDescription(colVal);
        }
    }
    idxcol++;
    {
        String colVal = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalDescription(null);
        } else {
            buff.setOptionalDescription(colVal);
        }
    }
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalAuthorId(null);
        } else {
            buff.setOptionalAuthorId(colVal);
        }
    }
    idxcol++;
    buff.setRequiredValueContainerId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredIsNullable(resultSet.getBoolean(idxcol));
    idxcol++;
    {
        boolean colVal = resultSet.getBoolean(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalGenerateId(null);
        } else {
            buff.setOptionalGenerateId(colVal);
        }
    }
    idxcol++;
    {
        short colVal = resultSet.getShort(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalDataScopeId(null);
        } else {
            buff.setOptionalDataScopeId(colVal);
        }
    }
    idxcol++;
    {
        short colVal = resultSet.getShort(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalViewAccessSecurityId(null);
        } else {
            buff.setOptionalViewAccessSecurityId(colVal);
        }
    }
    idxcol++;
    {
        short colVal = resultSet.getShort(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalEditAccessSecurityId(null);
        } else {
            buff.setOptionalEditAccessSecurityId(colVal);
        }
    }
    idxcol++;
    {
        short colVal = resultSet.getShort(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalViewAccessFrequencyId(null);
        } else {
            buff.setOptionalViewAccessFrequencyId(colVal);
        }
    }
    idxcol++;
    {
        short colVal = resultSet.getShort(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalEditAccessFrequencyId(null);
        } else {
            buff.setOptionalEditAccessFrequencyId(colVal);
        }
    }
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalPrevId(null);
        } else {
            buff.setOptionalPrevId(colVal);
        }
    }
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalNextId(null);
        } else {
            buff.setOptionalNextId(colVal);
        }
    }
    idxcol++;
    {
        String colVal = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalDbName(null);
        } else {
            buff.setOptionalDbName(colVal);
        }
    }
    idxcol++;
    buff.setRequiredMaxLen(resultSet.getInt(idxcol));
    idxcol++;
    {
        Blob binding = resultSet.getBlob(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalInitValue(null);
        } else {
            long bindLen = binding.length();
            if (bindLen > Integer.MAX_VALUE) {
                throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName,
                        idxcol, "InitValue", bindLen, (long) (Integer.MAX_VALUE));
            }
            byte[] colVal = binding.getBytes(0, (int) bindLen);
            buff.setOptionalInitValue(colVal);
        }
    }
    idxcol++;
    {
        Blob binding = resultSet.getBlob(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalDefaultValue(null);
        } else {
            long bindLen = binding.length();
            if (bindLen > Integer.MAX_VALUE) {
                throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName,
                        idxcol, "DefaultValue", bindLen, (long) (Integer.MAX_VALUE));
            }
            byte[] colVal = binding.getBytes(0, (int) bindLen);
            buff.setOptionalDefaultValue(colVal);
        }
    }
    idxcol++;
    {
        Blob binding = resultSet.getBlob(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalNullValue(null);
        } else {
            long bindLen = binding.length();
            if (bindLen > Integer.MAX_VALUE) {
                throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName,
                        idxcol, "NullValue", bindLen, (long) (Integer.MAX_VALUE));
            }
            byte[] colVal = binding.getBytes(0, (int) bindLen);
            buff.setOptionalNullValue(colVal);
        }
    }
    idxcol++;
    {
        Blob binding = resultSet.getBlob(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalUnknownValue(null);
        } else {
            long bindLen = binding.length();
            if (bindLen > Integer.MAX_VALUE) {
                throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName,
                        idxcol, "UnknownValue", bindLen, (long) (Integer.MAX_VALUE));
            }
            byte[] colVal = binding.getBytes(0, (int) bindLen);
            buff.setOptionalUnknownValue(colVal);
        }
    }
    idxcol++;
    buff.setRequiredContainerId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredRevision(resultSet.getInt(idxcol));
    return (buff);
}

From source file:org.apache.jackrabbit.core.persistence.bundle.BundleDbPersistenceManager.java

/**
 * {@inheritDoc}// ww  w .  java2 s  .  c om
 */
protected synchronized NodePropBundle loadBundle(NodeId id) throws ItemStateException {
    ResultSet rs = null;
    InputStream in = null;
    try {
        Statement stmt = connectionManager.executeStmt(bundleSelectSQL, getKey(id.getUUID()));
        rs = stmt.getResultSet();
        if (!rs.next()) {
            return null;
        }
        Blob b = rs.getBlob(1);
        // JCR-1039: pre-fetch/buffer blob data
        long length = b.length();
        byte[] bytes = new byte[(int) length];
        in = b.getBinaryStream();
        int read, pos = 0;
        while ((read = in.read(bytes, pos, bytes.length - pos)) > 0) {
            pos += read;
        }
        DataInputStream din = new DataInputStream(new ByteArrayInputStream(bytes));
        NodePropBundle bundle = binding.readBundle(din, id);
        bundle.setSize(length);
        return bundle;
    } catch (Exception e) {
        String msg = "failed to read bundle: " + id + ": " + e;
        log.error(msg);
        throw new ItemStateException(msg, e);
    } finally {
        IOUtils.closeQuietly(in);
        closeResultSet(rs);
    }
}

From source file:org.apache.jackrabbit.core.persistence.bundle.BundleDbPersistenceManager.java

/**
 * Loads a bundle from the underlying system and optionally performs
 * a check on the bundle first./*  w  ww . j  ava 2 s. c  o m*/
 *
 * @param id the node id of the bundle
 * @param checkBeforeLoading check the bundle before loading it and log
 *                           detailed informations about it (slower)
 * @return the loaded bundle or <code>null</code> if the bundle does not
 *         exist.
 * @throws ItemStateException if an error while loading occurs.
 */
protected synchronized NodePropBundle loadBundle(NodeId id, boolean checkBeforeLoading)
        throws ItemStateException {
    ResultSet rs = null;
    InputStream in = null;
    byte[] bytes = null;
    try {
        Statement stmt = connectionManager.executeStmt(bundleSelectSQL, getKey(id.getUUID()));
        rs = stmt.getResultSet();
        if (!rs.next()) {
            return null;
        }
        Blob b = rs.getBlob(1);
        // JCR-1039: pre-fetch/buffer blob data
        long length = b.length();
        bytes = new byte[(int) length];
        in = b.getBinaryStream();
        int read, pos = 0;
        while ((read = in.read(bytes, pos, bytes.length - pos)) > 0) {
            pos += read;
        }
        DataInputStream din = new DataInputStream(new ByteArrayInputStream(bytes));

        if (checkBeforeLoading) {
            if (binding.checkBundle(din)) {
                // reset stream for readBundle()
                din = new DataInputStream(new ByteArrayInputStream(bytes));
            } else {
                // gets wrapped as proper ItemStateException below
                throw new Exception("invalid bundle, see previous BundleBinding error log entry");
            }
        }

        NodePropBundle bundle = binding.readBundle(din, id);
        bundle.setSize(length);
        return bundle;
    } catch (Exception e) {
        String msg = "failed to read bundle: " + id + ": " + e;
        log.error(msg);
        throw new ItemStateException(msg, e);
    } finally {
        IOUtils.closeQuietly(in);
        closeResultSet(rs);
    }
}

From source file:org.apache.jackrabbit.core.persistence.bundle.BundleDbPersistenceManager.java

/**
 * Reads the blob's bytes and returns it. this is a helper method to
 * circumvent issue JCR-1039 and JCR-1474
 * @param blob blob to read/*from  w w  w. j  ava  2  s  . c  o m*/
 * @return bytes of the blob
 * @throws SQLException if an SQL error occurs
 * @throws IOException if an I/O error occurs
 */
private byte[] getBytes(Blob blob) throws SQLException, IOException {
    InputStream in = null;
    try {
        long length = blob.length();
        byte[] bytes = new byte[(int) length];
        in = blob.getBinaryStream();
        int read, pos = 0;
        while ((read = in.read(bytes, pos, bytes.length - pos)) > 0) {
            pos += read;
        }
        return bytes;
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:code.Servlet.java

private void send_image(BufferedReader reader, HttpServletResponse response, Statement stmt, PrintWriter out)
        throws SQLException, IOException {
    String line = reader.readLine();
    String idStep = line.substring(12, line.length() - 2);

    Blob test = null;
    byte[] bytesEncoded;
    String sql;/*ww  w.j  a v  a  2  s .c om*/
    if (idStep.compareTo("'-1") == 0) // treasure found!
    {
        sql = "select answer, correct, image, question from question join answer where question.idQuestion=answer.Question_idQuestion and question.idQuestion='6' order by correct desc";
    } else {
        sql = "select answer, correct, image, question from (step join question) join answer where step.idStep="
                + idStep
                + "' and step.Question_idQuestion=question.idQuestion and question.idQuestion=answer.Question_idQuestion order by correct desc";
    }
    ResultSet rs = stmt.executeQuery(sql);
    if (!rs.next()) {
        System.out.println("vuoto blob!");
    }

    String mex = rs.getString("question") + ";";
    int num_risp = 0;
    do {
        test = rs.getBlob(3);
        bytesEncoded = Base64.encodeBase64(test.getBytes(1, (int) test.length()));
        mex += rs.getString("answer") + "," + rs.getString("correct") + "," + new String(bytesEncoded) + ";";
        num_risp++;
    } while (rs.next());
    mex += "" + num_risp;

    System.out.println("fine send_image, inviate " + num_risp + " risposte");

    response.setContentType("text/plain");
    response.setContentLength(mex.length());
    PrintWriter reply = response.getWriter();
    reply.println(mex);
    out.close(); //non so se serve out
    out.flush();
}

From source file:org.apache.qpid.server.store.derby.DerbyMessageStore.java

private String blobToString(Blob blob) throws SQLException {
    byte[] bytes = blob.getBytes(1, (int) blob.length());
    return new String(bytes, UTF8_CHARSET);
}

From source file:org.apache.qpid.server.store.derby.DerbyMessageStore.java

public int getContent(long messageId, int offset, ByteBuffer dst) {
    Connection conn = null;//from w  ww  .j a va  2  s . c o  m
    PreparedStatement stmt = null;

    try {
        conn = newAutoCommitConnection();

        stmt = conn.prepareStatement(SELECT_FROM_MESSAGE_CONTENT);
        stmt.setLong(1, messageId);
        ResultSet rs = stmt.executeQuery();

        int written = 0;

        if (rs.next()) {

            Blob dataAsBlob = rs.getBlob(1);

            final int size = (int) dataAsBlob.length();
            byte[] dataAsBytes = dataAsBlob.getBytes(1, size);

            if (offset > size) {
                throw new RuntimeException("Offset " + offset + " is greater than message size " + size
                        + " for message id " + messageId + "!");

            }

            written = size - offset;
            if (written > dst.remaining()) {
                written = dst.remaining();
            }

            dst.put(dataAsBytes, offset, written);
        }

        return written;

    } catch (SQLException e) {
        throw new RuntimeException("Error retrieving content from offset " + offset + " for message "
                + messageId + ": " + e.getMessage(), e);
    } finally {
        closePreparedStatement(stmt);
        closeConnection(conn);
    }

}