001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.sql;
024:
025: import java.io.ByteArrayInputStream;
026: import java.io.ByteArrayOutputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.OutputStream;
030: import java.sql.Blob;
031: import java.sql.SQLException;
032:
033: import org.isqlviewer.util.BasicUtilities;
034:
035: /**
036: * Utility Implementation for dealing with BLOB objects in memory.
037: * <p>
038: * This is very easy implementation of a BLOB using the ByteArrayInputStream. This class is designed for easily creating
039: * BLOB objects at runtime using a byte array.
040: *
041: * @see java.io.ByteArrayInputStream
042: * @author Markus A. Kobold <mkobold at sprintpcs dot com>
043: */
044: public class ByteArrayBlob implements Blob {
045:
046: protected ByteArrayOutputStream blob = new ByteArrayOutputStream();
047:
048: public ByteArrayBlob() {
049:
050: }
051:
052: public ByteArrayBlob(byte[] data) throws IOException {
053:
054: blob.write(data);
055: }
056:
057: /**
058: * @param binaryStream
059: */
060: public ByteArrayBlob(InputStream binaryStream) throws IOException {
061:
062: BasicUtilities.copyStream(binaryStream, blob);
063: }
064:
065: public long length() {
066:
067: return blob.size();
068: }
069:
070: public byte[] getBytes(long pos, int length) throws SQLException {
071:
072: try {
073: byte[] copy = new byte[length];
074: System.arraycopy(blob.toByteArray(), (int) pos, copy, 0,
075: length);
076: return copy;
077: } catch (Throwable t) {
078: throw new SQLException(t.getMessage());
079: }
080: }
081:
082: public InputStream getBinaryStream() throws SQLException {
083:
084: try {
085: return new ByteArrayInputStream(blob.toByteArray());
086: } catch (Throwable t) {
087: throw new SQLException(t.getMessage());
088: }
089: }
090:
091: public long position(byte[] pattern, long start)
092: throws SQLException {
093:
094: try {
095: String s = new String(pattern);
096: String b = blob.toString();
097: return b.indexOf(s);
098: } catch (Throwable t) {
099: throw new SQLException(t.getMessage());
100: }
101: }
102:
103: public long position(Blob pattern, long start) throws SQLException {
104:
105: return position(pattern.getBytes(0, (int) pattern.length()),
106: start);
107: }
108:
109: public void truncate(long length) throws SQLException {
110:
111: try {
112: int len = (int) length;
113: byte[] copy = new byte[len];
114: System.arraycopy(blob.toByteArray(), 0, copy, 0, len);
115: blob.reset();
116: blob.write(copy);
117: } catch (Throwable t) {
118: throw new SQLException(t.getMessage());
119: }
120: }
121:
122: public OutputStream setBinaryStream(long pos) throws SQLException {
123:
124: return blob;
125: }
126:
127: public int setBytes(long pos, byte[] bytes, int offset, int len)
128: throws SQLException {
129:
130: try {
131: byte[] copy = blob.toByteArray();
132: System.arraycopy(blob.toByteArray(), 0, copy, 0, len);
133: blob.reset();
134: blob.write(copy);
135: return len;
136: } catch (Throwable t) {
137: throw new SQLException(t.getMessage());
138: }
139: }
140:
141: public int setBytes(long pos, byte[] bytes) throws SQLException {
142:
143: return setBytes(pos, bytes, 0, bytes.length);
144: }
145: }
|