Java Code Examples for java.sql.Blob.getBytes()

The following are Jave code examples for showing how to use getBytes() of the java.sql.Blob class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: OpenDiabetes   File: JDBCBlob.java   View Source Code Vote up 6 votes
/**
 * Retrieves the byte position in the <code>BLOB</code> value
 * designated by this <code>Blob</code> object at which
 * <code>pattern</code> begins.  The search begins at position
 * <code>start</code>.
 *
 * @param pattern the <code>Blob</code> object designating
 * the <code>BLOB</code> value for which to search
 * @param start the position in the <code>BLOB</code> value
 *        at which to begin searching; the first position is 1
 * @return the position at which the pattern begins, else -1
 * @exception SQLException if there is an error accessing the
 *            <code>BLOB</code> value or if start is less than 1
 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
 * this method
 * @since JDK 1.2, HSQLDB 1.7.2
 */
public long position(final Blob pattern, long start) throws SQLException {

    final byte[] data = getData();
    final int    dlen = data.length;

    if (start < MIN_POS) {
        throw JDBCUtil.outOfRangeArgument("start: " + start);
    } else if (start > dlen || pattern == null) {
        return -1L;
    }

    // by now, we know start <= Integer.MAX_VALUE;
    final int  startIndex = (int) start - 1;
    final long plen       = pattern.length();

    if (plen == 0 || startIndex > ((long) dlen) - plen) {
        return -1L;
    }

    // by now, we know plen <= Integer.MAX_VALUE
    final int iplen = (int) plen;
    byte[]    bytePattern;

    if (pattern instanceof JDBCBlob) {
        bytePattern = ((JDBCBlob) pattern).data();
    } else {
        bytePattern = pattern.getBytes(1L, iplen);
    }

    final int result = KMPSearchAlgorithm.search(data, bytePattern,
        KMPSearchAlgorithm.computeTable(bytePattern), startIndex);

    return (result == -1) ? -1
                          : result + 1;
}
 
Example 2
Project: OpenDiabetes   File: JDBCBlobFile.java   View Source Code Vote up 6 votes
/**
 * Retrieves the byte position in the <code>BLOB</code> value
 * designated by this <code>Blob</code> object at which
 * <code>pattern</code> begins.  The search begins at position
 * <code>start</code>.
 *
 * @param pattern the <code>Blob</code> object designating
 * the <code>BLOB</code> value for which to search
 * @param start the position in the <code>BLOB</code> value
 *        at which to begin searching; the first position is 1
 * @return the position at which the pattern begins, else -1
 * @exception SQLException if there is an error accessing the
 *            <code>BLOB</code> value or if start is less than 1
 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
 * this method
 * @since JDK 1.2
 */
public long position(final Blob pattern,
                     final long start) throws SQLException {

    long patternLength;

    if (start < 1) {
        throw JDBCUtil.outOfRangeArgument("start: " + start);
    } else if ((patternLength = (pattern == null) ? 0
                                                  : pattern.length()) == 0 || start
                                                  > length()) {
        return -1L;
    } else if (patternLength > Integer.MAX_VALUE) {
        throw JDBCUtil.outOfRangeArgument("pattern.length(): "
                                      + patternLength);
    }

    byte[] bytePattern;

    if (pattern instanceof JDBCBlob) {
        bytePattern = ((JDBCBlob) pattern).data();
    } else {
        bytePattern = pattern.getBytes(1L, (int) patternLength);
    }

    return position(bytePattern, start);
}
 
Example 3
Project: BibliotecaPS   File: BlobRegressionTest.java   View Source Code Vote up 6 votes
/**
 * @throws Exception
 */
public void testBug2670() throws Exception {
    byte[] blobData = new byte[32];

    for (int i = 0; i < blobData.length; i++) {
        blobData[i] = 1;
    }

    createTable("testBug2670", "(blobField LONGBLOB)");

    PreparedStatement pStmt = this.conn.prepareStatement("INSERT INTO testBug2670 (blobField) VALUES (?)");
    pStmt.setBytes(1, blobData);
    pStmt.executeUpdate();

    this.rs = this.stmt.executeQuery("SELECT blobField FROM testBug2670");
    this.rs.next();

    Blob blob = this.rs.getBlob(1);

    //
    // Test mid-point insertion
    //
    blob.setBytes(4, new byte[] { 2, 2, 2, 2 });

    byte[] newBlobData = blob.getBytes(1L, (int) blob.length());

    assertTrue("Blob changed length", blob.length() == blobData.length);

    assertTrue("New data inserted wrongly", ((newBlobData[3] == 2) && (newBlobData[4] == 2) && (newBlobData[5] == 2) && (newBlobData[6] == 2)));

    //
    // Test end-point insertion
    //
    blob.setBytes(32, new byte[] { 2, 2, 2, 2 });

    assertTrue("Blob length should be 3 larger", blob.length() == (blobData.length + 3));
}
 
Example 4
Project: tangyuan2   File: BlobTypeHandler.java   View Source Code Vote up 6 votes
@Override
public byte[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
	Blob blob = rs.getBlob(columnIndex);
	byte[] returnValue = null;
	if (null != blob) {
		returnValue = blob.getBytes(1, (int) blob.length());
	}
	return returnValue;
}
 
Example 5
Project: UtilsMaven   File: DBUtils.java   View Source Code Vote up 6 votes
/**
 * 用于查询记录中大数据类型值,若有多条记录符合要求则返回第一条记录的大数据
 *
 * @param sql 查询SQL语句,查询字段的类型必须为Blob类型
 * @param arg 传入的占位符的参数
 * @return 返回查询的大数据,封装在Map中,若没有符合条件的记录则返回空Map
 */
public static Map<String, byte[]> getBigData(String sql, Object... arg) {
    Map<String, byte[]> bigDataMap = new HashMap<String, byte[]>();
    Connection connection = JDBCUtils.getConnection();
    PreparedStatement ps = null;
    ResultSet result = null;

    // 填充占位符
    try {
        ps = connection.prepareStatement(sql);
        for (int i = 0; i < arg.length; i++) {
            ps.setObject(i + 1, arg[i]);
        }

        // 执行SQL语句
        result = ps.executeQuery();
        // 获取字段名
        List<String> columnList = DBUtils.getColumnLabels(result);
        // 遍历结果集
        while (result.next()) {
            // 遍历字段名获取相应大数据值
            for (String column : columnList) {
                Blob data = result.getBlob(column);
                byte[] datas = data.getBytes(1, (int) data.length());
                bigDataMap.put(column, datas);
            }
            break;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        JDBCUtils.release(result, ps, connection);
    }

    return bigDataMap;
}
 
Example 6
Project: lemon-mybatis-plus   File: BlobTypeHandler.java   View Source Code Vote up 6 votes
@Override    
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {    
    Blob blob = cs.getBlob(columnIndex);    
    byte[] returnValue = null;    
    if (null != blob) {    
        returnValue = blob.getBytes(1, (int) blob.length());    
    }    
    try {    
        return new String(returnValue, DEFAULT_CHARSET);    
    } catch (UnsupportedEncodingException e) {    
        throw new RuntimeException("Blob Encoding Error!");    
    }    
}
 
Example 7
Project: tangyuan2   File: BlobTypeHandler.java   View Source Code Vote up 6 votes
@Override
public byte[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
	Blob blob = cs.getBlob(columnIndex);
	byte[] returnValue = null;
	if (null != blob) {
		returnValue = blob.getBytes(1, (int) blob.length());
	}
	return returnValue;
}
 
Example 8
Project: lams   File: OracleLobHandler.java   View Source Code Vote up 6 votes
@Override
public byte[] getBlobAsBytes(ResultSet rs, int columnIndex) throws SQLException {
	logger.debug("Returning Oracle BLOB as bytes");
	Blob blob = rs.getBlob(columnIndex);
	initializeResourcesBeforeRead(rs.getStatement().getConnection(), blob);
	byte[] retVal = (blob != null ? blob.getBytes(1, (int) blob.length()) : null);
	releaseResourcesAfterRead(rs.getStatement().getConnection(), blob);
	return retVal;
}
 
Example 9
Project: lams   File: ByteArrayBlobType.java   View Source Code Vote up 6 votes
protected Object get(ResultSet rs, String name) throws SQLException {
	Blob blob = rs.getBlob( name );
	if ( rs.wasNull() ) return null;
	int length = (int) blob.length();
	byte[] primaryResult = blob.getBytes( 1, length );
	return wrap( primaryResult );
}
 
Example 10
Project: ProyectoPacientes   File: BlobRegressionTest.java   View Source Code Vote up 6 votes
/**
 * @throws Exception
 */
public void testBug2670() throws Exception {
    byte[] blobData = new byte[32];

    for (int i = 0; i < blobData.length; i++) {
        blobData[i] = 1;
    }

    createTable("testBug2670", "(blobField LONGBLOB)");

    PreparedStatement pStmt = this.conn.prepareStatement("INSERT INTO testBug2670 (blobField) VALUES (?)");
    pStmt.setBytes(1, blobData);
    pStmt.executeUpdate();

    this.rs = this.stmt.executeQuery("SELECT blobField FROM testBug2670");
    this.rs.next();

    Blob blob = this.rs.getBlob(1);

    //
    // Test mid-point insertion
    //
    blob.setBytes(4, new byte[] { 2, 2, 2, 2 });

    byte[] newBlobData = blob.getBytes(1L, (int) blob.length());

    assertTrue("Blob changed length", blob.length() == blobData.length);

    assertTrue("New data inserted wrongly", ((newBlobData[3] == 2) && (newBlobData[4] == 2) && (newBlobData[5] == 2) && (newBlobData[6] == 2)));

    //
    // Test end-point insertion
    //
    blob.setBytes(32, new byte[] { 2, 2, 2, 2 });

    assertTrue("Blob length should be 3 larger", blob.length() == (blobData.length + 3));
}
 
Example 11
Project: dev-courses   File: JDBCBlob.java   View Source Code Vote up 6 votes
/**
 * Retrieves the byte position in the <code>BLOB</code> value
 * designated by this <code>Blob</code> object at which
 * <code>pattern</code> begins.  The search begins at position
 * <code>start</code>.
 *
 * @param pattern the <code>Blob</code> object designating
 * the <code>BLOB</code> value for which to search
 * @param start the position in the <code>BLOB</code> value
 *        at which to begin searching; the first position is 1
 * @return the position at which the pattern begins, else -1
 * @exception SQLException if there is an error accessing the
 *            <code>BLOB</code> value or if start is less than 1
 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
 * this method
 * @since JDK 1.2, HSQLDB 1.7.2
 */
public long position(final Blob pattern, long start) throws SQLException {

    final byte[] data = getData();
    final int    dlen = data.length;

    if (start < MIN_POS) {
        throw JDBCUtil.outOfRangeArgument("start: " + start);
    } else if (start > dlen || pattern == null) {
        return -1L;
    }

    // by now, we know start <= Integer.MAX_VALUE;
    final int  startIndex = (int) start - 1;
    final long plen       = pattern.length();

    if (plen == 0 || startIndex > ((long) dlen) - plen) {
        return -1L;
    }

    // by now, we know plen <= Integer.MAX_VALUE
    final int iplen = (int) plen;
    byte[]    bytePattern;

    if (pattern instanceof JDBCBlob) {
        bytePattern = ((JDBCBlob) pattern).data();
    } else {
        bytePattern = pattern.getBytes(1L, iplen);
    }

    final int result = KMPSearchAlgorithm.search(data, bytePattern,
        KMPSearchAlgorithm.computeTable(bytePattern), startIndex);

    return (result == -1) ? -1
                          : result + 1;
}
 
Example 12
Project: tangyuan2   File: BlobTypeHandler.java   View Source Code Vote up 6 votes
@Override
public byte[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
	Blob blob = rs.getBlob(columnName);
	byte[] returnValue = null;
	if (null != blob) {
		returnValue = blob.getBytes(1, (int) blob.length());
	}
	return returnValue;
}
 
Example 13
Project: tangyuan2   File: BlobTypeHandler.java   View Source Code Vote up 6 votes
@Override
public void setResultToXCO(ResultSet rs, String columnName, String property, XCO xco) throws SQLException {
	Blob blob = rs.getBlob(columnName);
	if (blob != null && !rs.wasNull()) {
		byte[] v = blob.getBytes(1, (int) blob.length());
		if (null != v) {
			xco.setByteArrayValue(property, v);
		}
	}
}
 
Example 14
Project: neoscada   File: EventInjector.java   View Source Code Vote up 5 votes
private Event toEvent ( final ResultSet resultSet ) throws SQLException, IOException, ClassNotFoundException
{
    byte[] data;
    switch ( this.jdbcDao.dataFormat )
    {
        case JSON:
            return EventConverter.INSTANCE.toEvent ( resultSet.getString ( 4 ) );
        case BLOB:
            final Blob blob = resultSet.getBlob ( 4 );
            data = blob.getBytes ( 0, Long.valueOf ( blob.length () ).intValue () );
            blob.free ();
            break;
        case BYTES:
            //$FALL-THROUGH$
        default:
            data = resultSet.getBytes ( 4 );
            break;
    }

    logger.trace ( "Deserialize event" );

    final BundleObjectInputStream stream = new BundleObjectInputStream ( new ByteArrayInputStream ( data ), Activator.getContext ().getBundle () );
    try
    {
        final Object o = stream.readObject ();
        if ( o instanceof Event )
        {
            return (Event)o;
        }
        else if ( o == null )
        {
            logger.warn ( "Found null event" );
            return null;
        }
        else
        {
            logger.warn ( "Expected event type {} but found {}. Discarding...", Event.class, o.getClass () );
            return null;
        }
    }
    finally
    {
        stream.close ();
    }
}
 
Example 15
Project: parabuild-ci   File: jdbcBlob.java   View Source Code Vote up 5 votes
/**
 * Retrieves the byte position in the <code>BLOB</code> value
 * designated by this <code>Blob</code> object at which
 * <code>pattern</code> begins.  The search begins at position
 * <code>start</code>.
 *
 * @param pattern the <code>Blob</code> object designating
 *      the <code>BLOB</code> value for which to search
 * @param start the position in the <code>BLOB</code> value
 *        at which to begin searching; the first position is 1
 * @return the position at which the pattern begins, else -1
 * @exception SQLException if there is an error accessing the
 *        <code>BLOB</code> value
 *
 * @since JDK 1.2, HSQLDB 1.7.2
 */
public long position(final Blob pattern, long start) throws SQLException {

    final byte[] ldata = data;
    final int    dlen  = ldata.length;

    if (start > dlen || pattern == null) {
        return -1;
    } else if (start < 1) {
        start = 0;
    } else {
        start--;
    }

    final long plen = pattern.length();

    if (plen == 0 || start > ((long) dlen) - plen) {
        return -1;
    }

    // by now, we know plen <= Integer.MAX_VALUE
    final int iplen = (int) plen;
    byte[]    bap;

    if (pattern instanceof jdbcBlob) {
        bap = ((jdbcBlob) pattern).data;
    } else {
        bap = pattern.getBytes(1, iplen);
    }

    final int  stop = dlen - iplen;
    final byte b0   = bap[0];

    outer_loop:
    for (int i = (int) start; i <= stop; i++) {
        if (ldata[i] != b0) {
            continue;
        }

        int len     = iplen;
        int doffset = i;
        int poffset = 0;

        while (len-- > 0) {
            if (ldata[doffset++] != bap[poffset++]) {
                continue outer_loop;
            }
        }

        return i + 1;
    }

    return -1;
}
 
Example 16
Project: parabuild-ci   File: jdbcBlob.java   View Source Code Vote up 5 votes
/**
 * Retrieves the byte position in the <code>BLOB</code> value
 * designated by this <code>Blob</code> object at which
 * <code>pattern</code> begins.  The search begins at position
 * <code>start</code>.
 *
 * @param pattern the <code>Blob</code> object designating
 *      the <code>BLOB</code> value for which to search
 * @param start the position in the <code>BLOB</code> value
 *        at which to begin searching; the first position is 1
 * @return the position at which the pattern begins, else -1
 * @exception SQLException if there is an error accessing the
 *        <code>BLOB</code> value
 *
 * @since JDK 1.2, HSQLDB 1.7.2
 */
public long position(final Blob pattern, long start) throws SQLException {

    final byte[] ldata = data;
    final int    dlen  = ldata.length;

    if (start > dlen || pattern == null) {
        return -1;
    } else if (start < 1) {
        start = 0;
    } else {
        start--;
    }

    final long plen = pattern.length();

    if (plen == 0 || start > ((long) dlen) - plen) {
        return -1;
    }

    // by now, we know plen <= Integer.MAX_VALUE
    final int iplen = (int) plen;
    byte[]    bap;

    if (pattern instanceof jdbcBlob) {
        bap = ((jdbcBlob) pattern).data;
    } else {
        bap = pattern.getBytes(1, iplen);
    }

    final int  stop = dlen - iplen;
    final byte b0   = bap[0];

    outer_loop:
    for (int i = (int) start; i <= stop; i++) {
        if (ldata[i] != b0) {
            continue;
        }

        int len     = iplen;
        int doffset = i;
        int poffset = 0;

        while (len-- > 0) {
            if (ldata[doffset++] != bap[poffset++]) {
                continue outer_loop;
            }
        }

        return i + 1;
    }

    return -1;
}
 
Example 17
Project: s-store   File: JDBCBlob.java   View Source Code Vote up 5 votes
/**
 * Retrieves the byte position in the <code>BLOB</code> value
 * designated by this <code>Blob</code> object at which
 * <code>pattern</code> begins.  The search begins at position
 * <code>start</code>.
 *
 * @param pattern the <code>Blob</code> object designating
 * the <code>BLOB</code> value for which to search
 * @param start the position in the <code>BLOB</code> value
 *        at which to begin searching; the first position is 1
 * @return the position at which the pattern begins, else -1
 * @exception SQLException if there is an error accessing the
 *            <code>BLOB</code> value or if start is less than 1
 * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
 * this method
 * @since JDK 1.2, HSQLDB 1.7.2
 */
public long position(final Blob pattern, long start) throws SQLException {

    final byte[] ldata = data;

    checkValid(ldata);

    final int dlen = ldata.length;

    if (start < MIN_POS) {
        throw Util.outOfRangeArgument("start: " + start);
    } else if (start > dlen || pattern == null) {
        return -1L;
    } else {
        start--;
    }

    final long plen = pattern.length();

    if (plen == 0 || start > ((long) dlen) - plen) {
        return -1L;
    }

    // by now, we know plen <= Integer.MAX_VALUE
    final int iplen = (int) plen;
    byte[]    bap;

    if (pattern instanceof JDBCBlob) {
        bap = ((JDBCBlob) pattern).data();
    } else {
        bap = pattern.getBytes(1L, iplen);
    }

    final int  stop = dlen - iplen;
    final byte b0   = bap[0];

    outer_loop:
    for (int i = (int) start; i <= stop; i++) {
        if (ldata[i] != b0) {
            continue;
        }

        int len     = iplen;
        int doffset = i;
        int poffset = 0;

        while (len-- > 0) {
            if (ldata[doffset++] != bap[poffset++]) {
                continue outer_loop;
            }
        }

        return i + 1;
    }

    return -1L;
}
 
Example 18
Project: parabuild-ci   File: jdbcBlob.java   View Source Code Vote up 5 votes
/**
 * Retrieves the byte position in the <code>BLOB</code> value
 * designated by this <code>Blob</code> object at which
 * <code>pattern</code> begins.  The search begins at position
 * <code>start</code>.
 *
 * @param pattern the <code>Blob</code> object designating
 *      the <code>BLOB</code> value for which to search
 * @param start the position in the <code>BLOB</code> value
 *        at which to begin searching; the first position is 1
 * @return the position at which the pattern begins, else -1
 * @exception SQLException if there is an error accessing the
 *        <code>BLOB</code> value
 *
 * @since JDK 1.2, HSQLDB 1.7.2
 */
public long position(final Blob pattern, long start) throws SQLException {

    final byte[] ldata = data;
    final int    dlen  = ldata.length;

    if (start > dlen || pattern == null) {
        return -1;
    } else if (start < 1) {
        start = 0;
    } else {
        start--;
    }

    final long plen = pattern.length();

    if (plen == 0 || start > ((long) dlen) - plen) {
        return -1;
    }

    // by now, we know plen <= Integer.MAX_VALUE
    final int iplen = (int) plen;
    byte[]    bap;

    if (pattern instanceof jdbcBlob) {
        bap = ((jdbcBlob) pattern).data;
    } else {
        bap = pattern.getBytes(1, iplen);
    }

    final int  stop = dlen - iplen;
    final byte b0   = bap[0];

    outer_loop:
    for (int i = (int) start; i <= stop; i++) {
        if (ldata[i] != b0) {
            continue;
        }

        int len     = iplen;
        int doffset = i;
        int poffset = 0;

        while (len-- > 0) {
            if (ldata[doffset++] != bap[poffset++]) {
                continue outer_loop;
            }
        }

        return i + 1;
    }

    return -1;
}
 
Example 19
Project: parabuild-ci   File: jdbcBlob.java   View Source Code Vote up 5 votes
/**
 * Retrieves the byte position in the <code>BLOB</code> value
 * designated by this <code>Blob</code> object at which
 * <code>pattern</code> begins.  The search begins at position
 * <code>start</code>.
 *
 * @param pattern the <code>Blob</code> object designating
 *      the <code>BLOB</code> value for which to search
 * @param start the position in the <code>BLOB</code> value
 *        at which to begin searching; the first position is 1
 * @return the position at which the pattern begins, else -1
 * @exception SQLException if there is an error accessing the
 *        <code>BLOB</code> value
 *
 * @since JDK 1.2, HSQLDB 1.7.2
 */
public long position(final Blob pattern, long start) throws SQLException {

    final byte[] ldata = data;
    final int    dlen  = ldata.length;

    if (start > dlen || pattern == null) {
        return -1;
    } else if (start < 1) {
        start = 0;
    } else {
        start--;
    }

    final long plen = pattern.length();

    if (plen == 0 || start > ((long) dlen) - plen) {
        return -1;
    }

    // by now, we know plen <= Integer.MAX_VALUE
    final int iplen = (int) plen;
    byte[]    bap;

    if (pattern instanceof jdbcBlob) {
        bap = ((jdbcBlob) pattern).data;
    } else {
        bap = pattern.getBytes(1, iplen);
    }

    final int  stop = dlen - iplen;
    final byte b0   = bap[0];

    outer_loop:
    for (int i = (int) start; i <= stop; i++) {
        if (ldata[i] != b0) {
            continue;
        }

        int len     = iplen;
        int doffset = i;
        int poffset = 0;

        while (len-- > 0) {
            if (ldata[doffset++] != bap[poffset++]) {
                continue outer_loop;
            }
        }

        return i + 1;
    }

    return -1;
}
 
Example 20
Project: jaffa-framework   File: TypeDefs.java   View Source Code Vote up 4 votes
/** Gets a parameter from the ResultSet.
 * @return the parameter.
 * @param engineType The engine type as defined in init.xml
 * @param rs The ResultSet.
 * @param columnName The name of the parameter.
 * @throws SQLException if a database access error occurs.
 * @throws IOException if any error occurs in reading the data from the database.
 */
public Object getAppObject(ResultSet rs, String columnName, String engineType) throws SQLException, IOException {
    Blob blob = rs.getBlob(columnName);
    if (blob != null)
        return blob.getBytes(1, (int) blob.length());
    else
        return null;
}