KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > jdbc > MBlob


1 /**
2  * com.mckoi.database.jdbc.MBlob 14 Oct 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

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 /**
32  * An implementation of an sql.Blob object. This implementation keeps the
33  * entire Blob in memory.
34  * <p>
35  * <strong>NOTE:</strong> java.sql.Blob is only available in JDBC 2.0
36  *
37  * @author Tobias Downer
38  */

39
40 class MBlob implements Blob {
41
42   /**
43    * The ByteLongObject that is a container for the data in this blob.
44    */

45   private ByteLongObject blob;
46
47   /**
48    * Constructs the blob.
49    */

50   MBlob(ByteLongObject blob) {
51     this.blob = blob;
52   }
53
54   // ---------- Implemented from Blob ----------
55

56   public long length() throws SQLException {
57     return blob.length();
58   }
59
60   public byte[] getBytes(long pos, int length) throws SQLException {
61     // First byte is at position 1 according to JDBC Spec.
62
--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       // Look for first byte...
84
while (i <= max && buf[i] != pattern[0]) {
85         ++i;
86       }
87       // Reached end so exit..
88
if (i > max) {
89         return -1;
90       }
91
92       // Found first character, so look for the rest...
93
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     // Optimize if MBlob,
113
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   //#IFDEF(JDBC3.0)
123

124   // -------------------------- JDBC 3.0 -----------------------------------
125

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 JavaDoc 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   //#ENDIF
144

145 }
146
Popular Tags