001: package net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: import java.sql.Blob;
023: import java.util.Arrays;
024:
025: import net.sourceforge.squirrel_sql.fw.util.StringManager;
026: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
027:
028: /**
029: * @author gwg
030: *
031: * This is the object that is actually stored in the ContentsTab table
032: * for a BLOB field.
033: * The data in a BLOB is handled differently than other data types.
034: * When the row in the DB is read, what is returned is actually a
035: * java.sql.Blob object that points to the data rather than the data itself.
036: * Since BLOBs can be very large (and thus take a long time to read), we
037: * provide the user the flexibility to read only part of the BLOB data, or
038: * to not read any of it. We use the user selected options in various operations
039: * such as deciding if the field is editable or not.
040: * These options are set in the Session Parameters,
041: * and since those parameters can be changed after the data has been read, we
042: * make a copy of the appropriate information here.
043: */
044: public class BlobDescriptor {
045:
046: /**
047: * The java.sql.Blob object that was read.
048: */
049: Blob _blob;
050:
051: /**
052: * The data read from the Blob.
053: */
054: byte[] _data = null;
055:
056: /**
057: * If <TT>_blobRead</TT> is <TT>true</TT> then at least some
058: * of the data in the BLOB should have been read. If <TT>false</TT>,
059: * then we have not even tried to read the data.
060: */
061: private boolean _blobRead = false;
062:
063: /**
064: * If <TT>_wholeBlobRead</TT> is <TT>true</TT> then all of the
065: * data in this BLOB has been read into _data..
066: */
067: private boolean _wholeBlobRead = false;
068:
069: /**
070: * If <TT>_wholeBlobRead</TT> is false, this is the size limit
071: * set by the user for how much to read.
072: */
073: private int _userSetBlobLimit;
074:
075: /** Internationalized strings for this class. */
076: private static final StringManager s_stringMgr = StringManagerFactory
077: .getStringManager(BlobDescriptor.class);
078:
079: public static interface i18n {
080: String BLOB_LABEL = s_stringMgr
081: .getString("BlobDescriptor.blob");
082: }
083:
084: /**
085: * Ctor
086: */
087: public BlobDescriptor(Blob blob, byte[] data, boolean blobRead,
088: boolean wholeBlobRead, int userSetBlobLimit) {
089: _blob = blob;
090: _data = data;
091: _blobRead = blobRead;
092: _wholeBlobRead = wholeBlobRead;
093: _userSetBlobLimit = userSetBlobLimit;
094: }
095:
096: /**
097: * Equals for Blobs means that the internal byte arrays are identical,
098: * including their length.
099: * We need to account for the fact that one or both of them may not
100: * have actually had their data read. If both have not had their data read,
101: * then they are "equal", in a wierd kind of way.
102: */
103: public boolean equals(BlobDescriptor c) {
104: if (c == null) {
105: // the other obj is null, so see if this non-null obj contains
106: // a null value, which is equivilent.
107: // Assume that if we have read some of the data and we still have
108: // _data == null, then the value in the DB is actually null.
109: if (_blobRead == true && _data == null)
110: return true;
111: else
112: return false;
113: }
114:
115: if (c.getBlobRead() == false) {
116: // the other obj has not read the data yet.
117: if (_blobRead == true)
118: return false; // we have read data, so that is not the same state
119: else
120: return true; // odd-ball case: assume if neither has read data that they are equal
121: }
122:
123: // the other object has real data
124: if (_blobRead == false)
125: return false; // this one does not, so they are not equal
126:
127: // both have actual data, so compare the strings
128: // Note that if one has read all of the data and the other has read only part
129: // of the data that we will say that they are NOT equal.
130: return Arrays.equals(c.getData(), _data);
131: }
132:
133: /**
134: * toString means print the data string, unless the data has not been
135: * read at all.
136: */
137: public String toString() {
138: if (_blobRead) {
139: if (_data == null)
140: return null;
141:
142: // Convert the data into an ascii representation
143: // using the standard convention
144: Byte[] useValue = new Byte[_data.length];
145: for (int i = 0; i < _data.length; i++)
146: useValue[i] = Byte.valueOf(_data[i]);
147: String outString = BinaryDisplayConverter.convertToString(
148: useValue, BinaryDisplayConverter.HEX, false);
149: if (_wholeBlobRead || _userSetBlobLimit > _data.length)
150: return outString; // we have the whole contents of the BLOB
151: else
152: return outString + "..."; // tell user the data is truncated
153: } else
154: return i18n.BLOB_LABEL;
155: }
156:
157: /*
158: * Getters and Setters
159: */
160:
161: public Blob getBlob() {
162: return _blob;
163: }
164:
165: public void setBlob(Blob blob) {
166: _blob = blob;
167: }
168:
169: public byte[] getData() {
170: return _data;
171: }
172:
173: public void setData(byte[] data) {
174: _data = data;
175: }
176:
177: public boolean getBlobRead() {
178: return _blobRead;
179: }
180:
181: public void setBlobRead(boolean blobRead) {
182: _blobRead = blobRead;
183: }
184:
185: public boolean getWholeBlobRead() {
186: return _wholeBlobRead;
187: }
188:
189: public void setWholeBlobRead(boolean wholeBlobRead) {
190: _wholeBlobRead = wholeBlobRead;
191: }
192:
193: public int getUserSetBlobLimit() {
194: return _userSetBlobLimit;
195: }
196:
197: public void setUserSetBlobLimit(int userSetBlobLimit) {
198: _userSetBlobLimit = userSetBlobLimit;
199: }
200:
201: }
|