📄 embedblob.java
字号:
synchronized (getConnectionSynchronization()) { pushStack = !getEmbedConnection().isClosed(); if (pushStack) setupContextStack(); setPosition(0); return biStream; } } } catch (Throwable t) { throw handleMyExceptions(t); } finally { if (pushStack) restoreContextStack(); } } /** * Determines the byte position at which the specified byte * <code>pattern</code> begins within the <code>BLOB</code> * value that this <code>Blob</code> object represents. The * search for <code>pattern</code. begins at position * <code>start</code> * @param pattern the byte array for which to search * @param start the position at which to begin searching; the * first position is 1 * @return the position at which the pattern appears, else -1. * @exception SQLException if there is an error accessing the * <code>BLOB</code> */ public long position(byte[] pattern, long start) throws SQLException { boolean pushStack = false; try { if (start < 1) throw StandardException.newException( SQLState.BLOB_BAD_POSITION, new Long(start)); if (pattern == null) throw StandardException.newException(SQLState.BLOB_NULL_PATTERN); if (pattern.length == 0) return start; // match DB2's SQL LOCATE function synchronized (getConnectionSynchronization()) { pushStack = !getEmbedConnection().isClosed(); if (pushStack) setupContextStack(); setPosition(start-1); // look for first character int lookFor = pattern[0]; long curPos; int c; while (true) { c = read(); if (c == -1) // run out of stream return -1; if (c == lookFor) { curPos = pos; if (checkMatch(pattern)) return curPos; else setPosition(curPos); } } } } catch (StandardException e) { // if this is a setPosition exception then not found if (e.getMessageId().equals(SQLState.BLOB_SETPOSITION_FAILED)) return -1; else throw handleMyExceptions(e); } catch (Throwable t) { throw handleMyExceptions(t); } finally { if (pushStack) restoreContextStack(); } } /* check whether pattern (starting from the second byte) appears inside posStream (at the current position) @param posStream the stream to search inside @param pattern the byte array passed in by the user to search with @return true if match, false otherwise */ private boolean checkMatch(byte[] pattern) throws IOException { // check whether rest matches // might improve performance by reading more for (int i = 1; i < pattern.length; i++) { int b = read(); if ((b < 0) || (b != pattern[i])) // mismatch or stream runs out return false; } return true; } /** * Determines 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> */ public long position(Blob pattern, long start) throws SQLException { boolean pushStack = false; try { if (start < 1) throw StandardException.newException( SQLState.BLOB_BAD_POSITION, new Long(start)); if (pattern == null) throw StandardException.newException(SQLState.BLOB_NULL_PATTERN); synchronized (getConnectionSynchronization()) { pushStack = !getEmbedConnection().isClosed(); if (pushStack) setupContextStack(); setPosition(start-1); // look for first character byte[] b; try { // pattern is not necessarily a cloudscape Blob b = pattern.getBytes(1,1); } catch (SQLException e) { throw StandardException.newException(SQLState.BLOB_UNABLE_TO_READ_PATTERN); } if (b == null || b.length < 1) // the 'empty' blob return start; // match DB2's SQL LOCATE function int lookFor = b[0]; int c; long curPos; while (true) { c = read(); if (c == -1) // run out of stream return -1; if (c == lookFor) { curPos = pos; if (checkMatch(pattern)) return curPos; else setPosition(curPos); } } } } catch (StandardException e) { // if this is a setPosition exception then not found if (e.getMessageId().equals(SQLState.BLOB_SETPOSITION_FAILED)) return -1; else throw handleMyExceptions(e); } catch (Throwable t) { throw handleMyExceptions(t); } finally { if (pushStack) restoreContextStack(); } } /* check whether pattern (starting from the second byte) appears inside posStream (at the current position) @param posStream the stream to search inside @param pattern the blob passed in by the user to search with @return true if match, false otherwise */ private boolean checkMatch(Blob pattern) throws IOException { // check whether rest matches // might improve performance by reading buffer at a time InputStream pStream; try { pStream = pattern.getBinaryStream(); } catch (SQLException e) { return false; } if (pStream == null) return false; // throw away first character since we already read it in the calling // method int b1 = pStream.read(); if (b1 < 0) return false; while (true) { b1 = pStream.read(); if (b1 < 0) // search blob runs out return true; int b2 = read(); if ((b1 != b2) || (b2 < 0)) // mismatch or stream runs out return false; } } /* Convert exceptions where needed before calling handleException to convert them to SQLExceptions. */ private SQLException handleMyExceptions(Throwable t) throws SQLException { if (t instanceof StandardException) { // container closed means the blob or clob was accessed after commit if (((StandardException) t).getMessageId().equals(SQLState.DATA_CONTAINER_CLOSED)) { t = StandardException.newException(SQLState.BLOB_ACCESSED_AFTER_COMMIT); } } return handleException(t); } /* If we have a stream, release the resources associated with it. */ protected void finalize() { if (!isBytes) ((Resetable)myStream).closeStream(); } /** Following methods are for the new JDBC 3.0 methods in java.sql.Blob (see the JDBC 3.0 spec). We have the JDBC 3.0 methods in Local20 package, so we don't have to have a new class in Local30. The new JDBC 3.0 methods don't make use of any new JDBC3.0 classes and so this will work fine in jdbc2.0 configuration. */ ///////////////////////////////////////////////////////////////////////// // // JDBC 3.0 - New public methods // ///////////////////////////////////////////////////////////////////////// /** * JDBC 3.0 * * Writes the given array of bytes to the BLOB value that this Blob object * represents, starting at position pos, and returns the number of bytes written. * * @param pos - the position in the BLOB object at which to start writing * @param bytes - the array of bytes to be written to the BLOB value that this * Blob object represents * @return the number of bytes written * @exception SQLException Feature not implemented for now. */ public int setBytes(long pos, byte[] bytes) throws SQLException { throw Util.notImplemented(); } /** * JDBC 3.0 * * Writes all or part of the given array of byte array to the BLOB value that * this Blob object represents and returns the number of bytes written. * Writing starts at position pos in the BLOB value; len bytes from the given * byte array are written. * * @param pos - the position in the BLOB object at which to start writing * @param bytes - the array of bytes to be written to the BLOB value that this * Blob object represents * @param offset - the offset into the array bytes at which to start reading * the bytes to be set * @param len - the number of bytes to be written to the BLOB value from the * array of bytes bytes * @return the number of bytes written * @exception SQLException Feature not implemented for now. */ public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException { throw Util.notImplemented(); } /** * JDBC 3.0 * * Retrieves a stream that can be used to write to the BLOB value that this * Blob object represents. The stream begins at position pos. * * @param pos - the position in the BLOB object at which to start writing * @return a java.io.OutputStream object to which data can be written * @exception SQLException Feature not implemented for now. */ public java.io.OutputStream setBinaryStream(long pos) throws SQLException { throw Util.notImplemented(); } /** * JDBC 3.0 * * Truncates the BLOB value that this Blob object represents to be len bytes * in length. * * @param len - the length, in bytes, to which the BLOB value that this Blob * object represents should be truncated * @exception SQLException Feature not implemented for now. */ public void truncate(long len) throws SQLException { throw Util.notImplemented(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -