001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.jems.hibernate;
023:
024: import org.jboss.portal.common.io.IOTools;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031: import java.sql.Blob;
032: import java.sql.SQLException;
033:
034: /**
035: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
036: * @version $Revision: 8784 $
037: */
038: public final class ByteArrayBlob implements Blob {
039:
040: /** . */
041: byte[] _bytes;
042:
043: public ByteArrayBlob(InputStream in) throws IOException {
044: ByteArrayOutputStream out = new ByteArrayOutputStream();
045: IOTools.copy(in, out);
046: _bytes = out.toByteArray();
047: }
048:
049: public ByteArrayBlob(ByteArrayBlob that) {
050: _bytes = that.getBytes(0, that._bytes.length);
051: }
052:
053: public ByteArrayBlob(byte[] _bytes) {
054: this ._bytes = _bytes;
055: }
056:
057: public ByteArrayBlob(Blob blob) throws SQLException {
058: _bytes = blob.getBytes(1, (int) blob.length());
059: }
060:
061: public long length() {
062: return _bytes.length;
063: }
064:
065: public byte[] getBytes(long pos, int length) {
066: byte[] bytes = new byte[length];
067: // Why are we passing in 'pos'? It results in AOOBE.
068: //System.arraycopy(_bytes, (int)pos, bytes, 0, length);
069: System.arraycopy(_bytes, 0, bytes, 0, length);
070: return bytes;
071: }
072:
073: public InputStream getBinaryStream() {
074: return new ByteArrayInputStream(_bytes);
075: }
076:
077: /*
078: * For JDK 1.6
079: * @see java.sql.Blob#getBinaryStream()
080: */
081: public InputStream getBinaryStream(long pos, long length) {
082: throw new UnsupportedOperationException();
083: }
084:
085: /*
086: * For JDK 1.6
087: * @see java.sql.Blob#free()
088: */
089: public void free() {
090: throw new UnsupportedOperationException();
091: }
092:
093: public int setBytes(long pos, byte[] bytes) {
094: throw new UnsupportedOperationException();
095: }
096:
097: public int setBytes(long pos, byte[] bytes, int offset, int len) {
098: throw new UnsupportedOperationException();
099: }
100:
101: public OutputStream setBinaryStream(long pos) {
102: throw new UnsupportedOperationException();
103: }
104:
105: public long position(byte pattern[], long start) {
106: throw new UnsupportedOperationException();
107: }
108:
109: public long position(Blob pattern, long start) {
110: throw new UnsupportedOperationException();
111: }
112:
113: public void truncate(long len) {
114: throw new UnsupportedOperationException();
115: }
116:
117: public static ByteArrayBlob get(Blob blob) throws IOException,
118: SQLException {
119: if (blob instanceof ByteArrayBlob) {
120: return (ByteArrayBlob) blob;
121: } else {
122: InputStream binaryStream = blob.getBinaryStream();
123: return new ByteArrayBlob(binaryStream);
124: }
125: }
126:
127: public static ByteArrayBlob create(Blob blob) throws IOException,
128: SQLException {
129: if (blob instanceof ByteArrayBlob) {
130: return new ByteArrayBlob((ByteArrayBlob) blob);
131: } else {
132: InputStream binaryStream = blob.getBinaryStream();
133: return new ByteArrayBlob(binaryStream);
134: }
135: }
136:
137: }
|