1 24 25 package com.mckoi.database.jdbc; 26 27 import java.sql.*; 28 import java.io.*; 29 import com.mckoi.database.global.ByteLongObject; 30 31 39 40 class MBlob implements Blob { 41 42 45 private ByteLongObject blob; 46 47 50 MBlob(ByteLongObject blob) { 51 this.blob = blob; 52 } 53 54 56 public long length() throws SQLException { 57 return blob.length(); 58 } 59 60 public byte[] getBytes(long pos, int length) throws SQLException { 61 --pos; 63 if (pos < 0 || pos + length > length()) { 64 throw new SQLException("Out of bounds."); 65 } 66 67 byte[] buf = new byte[length]; 68 System.arraycopy(blob.getByteArray(), (int) pos, buf, 0, length); 69 return buf; 70 } 71 72 public InputStream getBinaryStream() throws SQLException { 73 return new ByteArrayInputStream(blob.getByteArray(), 0, (int) length()); 74 } 75 76 public long position(byte[] pattern, long start) throws SQLException { 77 byte[] buf = blob.getByteArray(); 78 int len = (int) length(); 79 int max = ((int) length()) - pattern.length; 80 81 int i = (int) (start - 1); 82 while (true) { 83 while (i <= max && buf[i] != pattern[0]) { 85 ++i; 86 } 87 if (i > max) { 89 return -1; 90 } 91 92 int search_from = i; 94 int found_index = 1; 95 while ( found_index < pattern.length && 96 buf[search_from] == pattern[found_index] ) { 97 ++search_from; 98 ++found_index; 99 } 100 101 ++i; 102 if (found_index >= pattern.length) { 103 return (long) i; 104 } 105 106 } 107 108 } 109 110 public long position(Blob pattern, long start) throws SQLException { 111 byte[] buf; 112 if (pattern instanceof MBlob) { 114 buf = ((MBlob) pattern).blob.getByteArray(); 115 } 116 else { 117 buf = pattern.getBytes(0, (int) pattern.length()); 118 } 119 return position(buf, start); 120 } 121 122 124 126 public int setBytes(long pos, byte[] bytes) throws SQLException { 127 throw new SQLException("BLOB updating is not supported"); 128 } 129 130 public int setBytes(long pos, byte[] bytes, int offset, int len) 131 throws SQLException { 132 throw new SQLException("BLOB updating is not supported"); 133 } 134 135 public java.io.OutputStream setBinaryStream(long pos) throws SQLException { 136 throw new SQLException("BLOB updating is not supported"); 137 } 138 139 public void truncate(long len) throws SQLException { 140 throw new SQLException("BLOB updating is not supported"); 141 } 142 143 145 } 146 | Popular Tags |