📄 blobfromlocator.java
字号:
try { pStmt.close(); } catch (SQLException sqlEx) { ; // do nothing } pStmt = null; } } } /** * Finds the position of the given pattern in this BLOB. * * @param pattern * the pattern to find * @param start * where to start finding the pattern * * @return the position where the pattern is found in the BLOB, -1 if not * found * * @throws SQLException * if a database error occurs */ public long position(java.sql.Blob pattern, long start) throws SQLException { return position(pattern.getBytes(0, (int) pattern.length()), start); } /** * @see java.sql.Blob#position(byte[], long) */ public long position(byte[] pattern, long start) throws SQLException { java.sql.ResultSet blobRs = null; java.sql.PreparedStatement pStmt = null; // FIXME: Needs to use identifiers for column/table names StringBuffer query = new StringBuffer("SELECT LOCATE("); query.append("?, "); query.append(this.blobColumnName); query.append(", "); query.append(start); query.append(") FROM "); query.append(this.tableName); query.append(" WHERE "); query.append((String) this.primaryKeyColumns.get(0)); query.append(" = ?"); for (int i = 1; i < this.numPrimaryKeys; i++) { query.append(" AND "); query.append((String) this.primaryKeyColumns.get(i)); query.append(" = ?"); } try { // FIXME: Have this passed in instead pStmt = this.creatorResultSet.connection.prepareStatement(query .toString()); pStmt.setBytes(1, pattern); for (int i = 0; i < this.numPrimaryKeys; i++) { pStmt.setString(i + 2, (String) this.primaryKeyValues.get(i)); } blobRs = pStmt.executeQuery(); if (blobRs.next()) { return blobRs.getLong(1); } throw SQLError.createSQLException( "BLOB data not found! Did primary keys change?", SQLError.SQL_STATE_GENERAL_ERROR); } finally { if (blobRs != null) { try { blobRs.close(); } catch (SQLException sqlEx) { ; // do nothing } blobRs = null; } if (pStmt != null) { try { pStmt.close(); } catch (SQLException sqlEx) { ; // do nothing } pStmt = null; } } } /** * @see Blob#truncate(long) */ public void truncate(long length) throws SQLException { java.sql.PreparedStatement pStmt = null; // FIXME: Needs to use identifiers for column/table names StringBuffer query = new StringBuffer("UPDATE "); query.append(this.tableName); query.append(" SET "); query.append(this.blobColumnName); query.append(" = LEFT("); query.append(this.blobColumnName); query.append(", "); query.append(length); query.append(") WHERE "); query.append((String) this.primaryKeyColumns.get(0)); query.append(" = ?"); for (int i = 1; i < this.numPrimaryKeys; i++) { query.append(" AND "); query.append((String) this.primaryKeyColumns.get(i)); query.append(" = ?"); } try { // FIXME: Have this passed in instead pStmt = this.creatorResultSet.connection.prepareStatement(query .toString()); for (int i = 0; i < this.numPrimaryKeys; i++) { pStmt.setString(i + 1, (String) this.primaryKeyValues.get(i)); } int rowsUpdated = pStmt.executeUpdate(); if (rowsUpdated != 1) { throw SQLError.createSQLException( "BLOB data not found! Did primary keys change?", SQLError.SQL_STATE_GENERAL_ERROR); } } finally { if (pStmt != null) { try { pStmt.close(); } catch (SQLException sqlEx) { ; // do nothing } pStmt = null; } } } java.sql.PreparedStatement createGetBytesStatement() throws SQLException { StringBuffer query = new StringBuffer("SELECT SUBSTRING("); query.append(this.blobColumnName); query.append(", "); query.append("?"); query.append(", "); query.append("?"); query.append(") FROM "); query.append(this.tableName); query.append(" WHERE "); query.append((String) this.primaryKeyColumns.get(0)); query.append(" = ?"); for (int i = 1; i < this.numPrimaryKeys; i++) { query.append(" AND "); query.append((String) this.primaryKeyColumns.get(i)); query.append(" = ?"); } return this.creatorResultSet.connection.prepareStatement(query .toString()); } byte[] getBytesInternal(java.sql.PreparedStatement pStmt, long pos, int length) throws SQLException { java.sql.ResultSet blobRs = null; try { pStmt.setLong(1, pos); pStmt.setInt(2, length); for (int i = 0; i < this.numPrimaryKeys; i++) { pStmt.setString(i + 3, (String) this.primaryKeyValues.get(i)); } blobRs = pStmt.executeQuery(); if (blobRs.next()) { return ((com.mysql.jdbc.ResultSetImpl) blobRs).getBytes(1, true); } throw SQLError.createSQLException( "BLOB data not found! Did primary keys change?", SQLError.SQL_STATE_GENERAL_ERROR); } finally { if (blobRs != null) { try { blobRs.close(); } catch (SQLException sqlEx) { ; // do nothing } blobRs = null; } } } class LocatorInputStream extends InputStream { long currentPositionInBlob = 0; long length = 0; java.sql.PreparedStatement pStmt = null; LocatorInputStream() throws SQLException { length = length(); pStmt = createGetBytesStatement(); } LocatorInputStream(long pos, long len) throws SQLException { length = pos + len; currentPositionInBlob = pos; long blobLength = length(); if (pos + len > blobLength) { throw SQLError.createSQLException( Messages.getString("Blob.invalidStreamLength", new Object[] {new Long(blobLength), new Long(pos), new Long(len)}), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } if (pos < 1) { throw SQLError.createSQLException(Messages.getString("Blob.invalidStreamPos"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } if (pos > blobLength) { throw SQLError.createSQLException(Messages.getString("Blob.invalidStreamPos"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } } public int read() throws IOException { if (currentPositionInBlob + 1 > length) { return -1; } try { byte[] asBytes = getBytesInternal(pStmt, (currentPositionInBlob++) + 1, 1); if (asBytes == null) { return -1; } return asBytes[0]; } catch (SQLException sqlEx) { throw new IOException(sqlEx.toString()); } } /* * (non-Javadoc) * * @see java.io.InputStream#read(byte[], int, int) */ public int read(byte[] b, int off, int len) throws IOException { if (currentPositionInBlob + 1 > length) { return -1; } try { byte[] asBytes = getBytesInternal(pStmt, (currentPositionInBlob) + 1, len); if (asBytes == null) { return -1; } System.arraycopy(asBytes, 0, b, off, asBytes.length); currentPositionInBlob += asBytes.length; return asBytes.length; } catch (SQLException sqlEx) { throw new IOException(sqlEx.toString()); } } /* * (non-Javadoc) * * @see java.io.InputStream#read(byte[]) */ public int read(byte[] b) throws IOException { if (currentPositionInBlob + 1 > length) { return -1; } try { byte[] asBytes = getBytesInternal(pStmt, (currentPositionInBlob) + 1, b.length); if (asBytes == null) { return -1; } System.arraycopy(asBytes, 0, b, 0, asBytes.length); currentPositionInBlob += asBytes.length; return asBytes.length; } catch (SQLException sqlEx) { throw new IOException(sqlEx.toString()); } } /* * (non-Javadoc) * * @see java.io.InputStream#close() */ public void close() throws IOException { if (pStmt != null) { try { pStmt.close(); } catch (SQLException sqlEx) { throw new IOException(sqlEx.toString()); } } super.close(); } } public void free() throws SQLException { this.creatorResultSet = null; this.primaryKeyColumns = null; this.primaryKeyValues = null; } public InputStream getBinaryStream(long pos, long length) throws SQLException { return new LocatorInputStream(pos, length); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -