KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > ByteArrayBlob


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.sql.Blob JavaDoc;
29
30 /**
31  * The representation (mapping) in the Java<sup><font size=-2>TM</font></sup>
32  * programming language of an SQL <code>BLOB</code> value to an array of bytes.
33  * A ByteArrayBlob contains an internal buffer that contains bytes that may be
34  * read from the stream. The <code>Blob</code> interface provides methods for
35  * getting the length of an SQL <code>BLOB</code> (Binary Large Object) value,
36  * for materializing a <code>BLOB</code> value on the client, and for
37  * determining the position of a pattern of bytes within a <code>BLOB</code>
38  * value. The ByteArrayBlob has static factory methods for construting an
39  * <code>BLOB</code> using either an existing serializable object, or an array
40  * of bytes. This is a nice way to store serialized objects in a relational
41  * field of type SQL <code>BLOB</code>.
42  *
43  * @author <a HREF="mailto:amccullo@sourceforge.new">Andrew McCulloch</a>
44  * @version $Revision: 37459 $
45  */

46 public final class ByteArrayBlob implements Blob JavaDoc
47 {
48    /**
49     * The internal buffer for the bytes of the Blob.
50     */

51    private final byte[] mBytes;
52
53    public ByteArrayBlob(byte[] bytes)
54    {
55       if (bytes == null)
56       {
57          bytes = new byte[0];
58       }
59
60       mBytes = bytes;
61    }
62
63    public InputStream JavaDoc getBinaryStream() throws SQLException JavaDoc
64    {
65       return new ByteArrayInputStream JavaDoc(mBytes);
66    }
67
68    public byte[] getBytes(long pos, int length) throws SQLException JavaDoc
69    {
70       // Defensive code, parameter checks.
71
if (length < 0 || length > mBytes.length || pos > mBytes.length)
72       {
73          return new byte[0];
74       }
75
76       if (pos <= 0)
77       {
78          pos = 1; // One since the copy starts at pos.
79
}
80
81       byte[] buffer = new byte[length];
82
83       System.arraycopy(mBytes, (int)pos - 1, buffer, 0, length);
84       return buffer;
85    }
86
87    public long length() throws SQLException JavaDoc
88    {
89       return mBytes.length;
90    }
91
92    public long position(Blob JavaDoc pattern , long start) throws SQLException JavaDoc
93    {
94       return position(pattern.getBytes(0, (int)pattern.length()), start);
95    }
96
97    public long position(byte pattern[], long start) throws SQLException JavaDoc
98    {
99       // Small optimization, no need to look beyond this.
100
int max = mBytes.length - pattern.length;
101
102       if (start < 0)
103       {
104          start = 0; // Cannot start negative, so put it at the beginning.
105
} else if (start >= mBytes.length)
106       {
107          return -1; // Out of bounds, start was past the end of the buffer.
108
}
109
110       if (pattern.length == 0)
111       {
112          return -1; // Indicate that the pattern was not found.
113
}
114
115       byte first = pattern[0];
116       int i = (int)start;
117
118       while (true)
119       {
120          // Look for the first character.
121
while (i <= max && mBytes[i] != first)
122          {
123             i++;
124          }
125
126          if (i > max)
127          {
128             return -1; // Went to far, reject the pattern.
129
}
130
131          // Found the first character, now look for remainder of v2.
132
int j = i + 1;
133          int end = j + pattern.length - 1;
134          int k = 1;
135          boolean cont = true;
136
137          // While the bytes remain equal and the end of v1 is not reached
138
// continue the either rejecting this match, or accepting it.
139
while (cont && j < end)
140          {
141             if (mBytes[j++] != pattern[k++])
142             {
143                i++;
144                cont = false;
145             }
146          } // If cont == false then the pattern was found.
147

148          if (cont)
149          {
150             return i;
151          }
152       }
153    }
154
155    public OutputStream JavaDoc setBinaryStream(long pos)
156       throws SQLException JavaDoc
157    {
158       throw new UnsupportedOperationException JavaDoc("ByteArrayBlob is immutable");
159    }
160    public int setBytes(long pos, byte[] bytes)
161       throws SQLException JavaDoc
162    {
163       throw new UnsupportedOperationException JavaDoc("ByteArrayBlob is immutable");
164    }
165    public int setBytes(long pos, byte[] bytes, int offset, int length)
166       throws SQLException JavaDoc
167    {
168       throw new UnsupportedOperationException JavaDoc("ByteArrayBlob is immutable");
169    }
170    public void truncate(long length)
171       throws SQLException JavaDoc
172    {
173       throw new UnsupportedOperationException JavaDoc("ByteArrayBlob is immutable");
174    }
175 }
176
177
Popular Tags