KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > persistentsystem > DBlobUpdatable


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem;
2
3 import java.io.*;
4 import java.sql.*;
5 import java.util.*;
6 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
7 import com.daffodilwoods.daffodildb.utils.*;
8 import com.daffodilwoods.daffodildb.utils.byteconverter.*;
9 import com.daffodilwoods.daffodildb.utils.field.*;
10 import com.daffodilwoods.database.resource.*;
11
12 /**
13  *
14  * it is used to insert data in blob type column,data is set in DBlobUpdatable which has to insert and
15  * data of blob column can be retrieved through DBlobUpdatable
16  */

17 public class DBlobUpdatable extends FieldBase implements Blob,Serializable,_LobUpdatable {
18     private int StartingBlobLength;
19     protected int lengthOfBlob = -1;
20
21     /**
22      * bytes which has to insert
23      */

24     private transient byte[] bytes ;
25
26     /**
27      * total bytes length
28      */

29     private int startPoint ;
30
31     /**
32      * To read and write data in Cluster
33      */

34
35     private transient DBlob blob ;
36     private transient TreeSet mappings;
37
38     /**
39      * First cluster address
40      */

41     private int startClusterAddress = -1;
42
43     private short recordNumber;
44
45     /**
46      * If data has to retrieve then it is set true
47      */

48     public boolean isDBLob;
49
50     /**
51      * Constructs DBlobUpdatable with DBlob. it isused For retrieving and updating data
52      *
53      * @param blob DBlob to retieve and update bytes in cluster
54      */

55
56
57     DBlobUpdatable(DBlob blob) {
58         this.blob = blob;
59         mappings = new TreeSet();
60         isDBLob = true;
61     }
62
63     /**
64      * Constructs DBlobUpdatable with bytes which has to insert.
65
66      * @param bytes which has to insert
67      */

68
69     public DBlobUpdatable(byte[] bytes) {
70         lengthOfBlob = bytes == null ? 0 : bytes.length;
71         this.bytes = bytes;
72         startPoint = lengthOfBlob;
73         StartingBlobLength = lengthOfBlob;
74         if(lengthOfBlob<0){
75          ;//// Removed By Program ** System.out.println(" lengthOfBlob from constructor DBlobUpdatable(byte[] bytes) "+lengthOfBlob);
76
}
77
78       }
79
80     public DBlobUpdatable(String JavaDoc inputString) {
81         this(inputString.getBytes());
82     }
83
84     InputStream stream;
85     boolean isStream;
86     boolean isNull;
87
88     /**
89      * Constructs DBlobUpdatable with stream from which data has to insert
90
91      * @param stream0 from which data has to insert
92      */

93
94     public DBlobUpdatable(InputStream stream0) {
95         stream = stream0;
96         isStream = true;
97     }
98
99     /**
100      * in case of CLOB
101      */

102
103     public DBlobUpdatable() {
104     }
105
106     public DBlobUpdatable(boolean isNull0) {
107         isNull = isNull0;
108     }
109
110     /**
111      * retrurns isStream , If data has to insert through stream then it returns true
112      * @return
113      */

114
115     public boolean isStream() {
116         return isStream;
117     }
118
119     public boolean isDBlob(){
120         return isDBLob;
121     }
122
123     public InputStream getStream() {
124         return stream;
125     }
126
127     protected void initialize() throws SQLException{
128         try {
129             if(lengthOfBlob == -1 ){
130                 lengthOfBlob = blob == null ? isStream ? stream.available() : 0 : (int)blob.length();
131                 StartingBlobLength = lengthOfBlob;
132                 startPoint = lengthOfBlob;
133                 if(lengthOfBlob<0){
134          ;//// Removed By Program ** System.out.println(" lengthOfBlob From DBLOBUPDATABLE "+lengthOfBlob);
135
}
136               }
137         }
138         catch (Exception JavaDoc ex) {
139             throw new SQLException(ex.getMessage());
140         }
141
142     }
143
144     public long length() throws SQLException{
145         initialize();
146         return lengthOfBlob;
147     }
148
149     /**
150      * returns bytes from specified position and of given length
151      *
152      * @param pos position from where string is required
153      * @param length length of string which is required
154      *
155      * @return bytes from specified position and of given length
156      *
157      * @throws Exception If position is more than total length of bytes inserted
158      *
159      */

160
161     public byte[] getBytes(long pos, int length) throws SQLException{
162         if(stream != null && blob == null){
163             try {
164                 byte[] bytes = new byte [length];
165                 long skip = stream.skip(pos-1);
166                 if(skip != pos-1){
167                     throw new SQLException(" POSITION "+ pos +" IS MORE THAN THE LENGTH OF BLOB LENGTH "+skip);
168                 }
169                 int res = stream.read(bytes);
170                 if(res==-1)
171                   return new byte[0];
172                 if(res < length){
173                     byte[] bytesGot = new byte[res];
174                     System.arraycopy(bytes,0,bytesGot,0,res);
175                     return bytesGot;
176                 }
177                 return bytes;
178             }
179             catch (IOException ex) {
180             }
181         }
182         initialize();
183         if(length == 0) return new byte[0];
184         if(pos > lengthOfBlob){
185            DException de = new DException("DSE939", new Object JavaDoc[]{new Long JavaDoc(pos),new Integer JavaDoc(lengthOfBlob)});
186            throw de.getSqlException(null );
187         }
188         if((pos + length - 1) > lengthOfBlob){
189             length = lengthOfBlob - (int)pos + 1;
190         }
191         if(blob == null){
192            byte[] bytesToReturn = new byte [length];
193            System.arraycopy(bytes,(int)pos-1,bytesToReturn,0,length);
194            return bytesToReturn;
195         }
196
197         try{
198             if(mappings == null || mappings.size() == 0)
199                 return blob.getBytes(pos,length);
200             return updateBytes((int)pos,length);
201         }
202         catch(DException de){
203             throw de.getSqlException(null);
204         }
205     }
206
207     public InputStream getBinaryStream() throws SQLException{
208         initialize();
209         return new java.io.ByteArrayInputStream JavaDoc(getBytes(1,lengthOfBlob));
210     }
211
212     public long position(byte[] pattern, long start) throws SQLException {
213         initialize();
214         String JavaDoc targetString = new String JavaDoc(getBytes(start,lengthOfBlob-(int)start));
215         return targetString.indexOf( new String JavaDoc(pattern));
216     }
217
218
219
220     public long position(Blob pattern, long start) throws SQLException {
221         try {
222             initialize();
223             return position(((DBlob)pattern).getBytes(start,(int)(pattern.length() - start)),0);
224         }catch(DException ex){
225             throw ex.getSqlException(null);
226         }
227     }
228
229     /**
230      * Update bytes from given position
231      *
232      * @param pos position from where bytes has to update
233      * @param bytes new bytes
234      *
235      * @return number of bytes which are updated
236      *
237      * @throws Exception If position is more than total length of bytes inserted
238
239      */

240
241     public int setBytes(long pos, byte[] bytes) throws SQLException {
242         initialize();
243         if(pos > lengthOfBlob){
244             DException de = new DException("DSE939", new Object JavaDoc[]{new Long JavaDoc(pos),new Integer JavaDoc(lengthOfBlob)});
245             throw de.getSqlException(null);
246         }
247         updateMapping(new Mapping((int)pos,bytes));
248         return bytes.length;
249     }
250
251     public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException{
252         initialize();
253         byte[] temp = new byte[len];
254         System.arraycopy(bytes,offset,temp,0,len);
255         return setBytes(pos,temp);
256     }
257
258     public OutputStream setBinaryStream(long pos) throws SQLException{
259         initialize();
260         return null;
261     }
262
263     public void truncate(long len) throws SQLException{
264         initialize();
265         if(len > lengthOfBlob){
266             DException de = new DException("DSE1017", new Object JavaDoc[]{new Long JavaDoc(len),new Integer JavaDoc(lengthOfBlob)});
267             throw de.getSqlException(null);
268         }
269         if(lengthOfBlob == len)
270             return;
271         lengthOfBlob = (int)len;
272     }
273
274
275     class Mapping implements Comparable JavaDoc{
276         int position;
277         byte[] bytes;
278
279         public Mapping(int position,byte[] bytes) {
280             this.position = position;
281             this.bytes = bytes;
282         }
283
284         public int compareTo(Object JavaDoc o) {
285             Mapping mp = (Mapping)o;
286             return position - mp.position;
287         }
288
289         public String JavaDoc toString() {
290             return " " + position + " :: " + (position + bytes.length -1);
291         }
292     }
293
294
295     private byte[] updateBytes(int pos,int length) throws SQLException { //java.sql.SQLException
296
initialize();
297         byte[] bytes = null;
298         try{
299             bytes = blob.getBytes(pos,length);
300         } catch(DException ex) {
301             throw ex.getSqlException(null);
302         }
303
304         Mapping[] mapIterator = new Mapping[0];
305         try{
306             mapIterator = (Mapping[])mappings.toArray(new Mapping[0]);
307         }
308         catch(NullPointerException JavaDoc npe){
309             mappings = new TreeSet();
310         }
311         int k = 0;
312         for(int i = 0 ; i < mapIterator.length ; i++){
313             if( mapIterator[i].position < pos ){
314                 int len = (mapIterator[i].position + mapIterator[i].bytes.length - 1) <= (pos + length) ? mapIterator[i].bytes.length - (pos - mapIterator[i].position) : pos + length - mapIterator[i].position ;
315                 k = 0;
316                 if(mapIterator[i].position + mapIterator[i].bytes.length - 1 > pos){
317                     System.arraycopy(mapIterator[i].bytes,pos - mapIterator[i].position,bytes,k,len);
318                 }
319             }
320             else{
321                 int len = mapIterator[i].bytes.length;
322                 k = mapIterator[i].position - pos;
323                 if(mapIterator[i].position < (pos + length)){
324                     System.arraycopy(mapIterator[i].bytes,0,bytes,k,len);
325                 }
326             }
327         }
328         return bytes;
329     }
330
331     private void updateMapping(Mapping mapp) throws SQLException {
332         initialize();
333         Mapping[] mapIterator = new Mapping[0];
334         try{
335             mapIterator = (Mapping[])mappings.toArray(new Mapping[0]);
336         }catch(NullPointerException JavaDoc npe){
337             mappings = new TreeSet();
338         }
339         if(mappings.size() == 0){
340             mappings.add(mapp);
341             return;
342         }
343         int[] array1 = search(mapp.position,mapIterator);
344         int[] array2 = search(mapp.position + mapp.bytes.length - 1,mapIterator);
345         int min = 0;
346         int max = 0;
347         min = array1[0] == -1 ? mapIterator[array1[1]].position : mapp.position;
348         max = array2[0] == -1 ? mapIterator[array2[1]].position + mapIterator[array2[1]].bytes.length - 1 : mapp.position + mapp.bytes.length - 1;
349         byte[] temp = new byte[max - min + 1];
350          min = array1[0] == -1 ? array1[1] : (array1[0] == LESSTHANMIN) ? 0 : (array1[0] == MORETHANMAX) ? mapIterator.length + 1 : array1[0] + 1;
351          max = array2[0] == -1 ? array2[1] : (array2[0] == LESSTHANMIN) ? -1 : (array2[0] == MORETHANMAX) ? mapIterator.length - 1 : array2[0];
352         if(array1[0] == -1){
353             System.arraycopy(mapIterator[array1[1]].bytes,0,temp,0,mapp.position - mapIterator[array1[1]].position);
354             System.arraycopy(mapp.bytes,0,temp,mapp.position - mapIterator[array1[1]].position,mapp.bytes.length);
355             if( array2[0] == -1 ){
356                 int len = mapIterator[array2[1]].position + mapIterator[array2[1]].bytes.length - 1 - (mapp.position + mapp.bytes.length - 1);
357                 System.arraycopy(mapIterator[array2[1]].bytes,mapp.position + mapp.bytes.length - 1 - mapIterator[array2[1]].position,temp, mapp.bytes.length + mapp.position - 1 - mapIterator[array1[1]].position ,len);
358             }
359             mapp.position = mapIterator[array1[1]].position;
360             for( ; min <= max ; min++)
361                 mappings.remove(mapIterator[min]);
362         }
363         else{
364             System.arraycopy(mapp.bytes,0,temp,0,mapp.bytes.length);
365             if( array2[0] == -1 ){
366                 System.arraycopy( mapIterator[array2[1]].bytes,mapp.position + mapp.bytes.length - mapIterator[array2[1]].position,temp,mapp.bytes.length,mapIterator[array2[1]].position + mapIterator[array2[1]].bytes.length - mapp.position - mapp.bytes.length );
367             }
368             for( ; min <= max ; min++)
369                 mappings.remove(mapIterator[min]);
370         }
371         mapp.bytes = temp;
372         mappings.add(mapp);
373     }
374
375     int LESSTHANMIN = -2;
376     int MORETHANMAX = -3;
377
378     private int[] search(int position,Mapping[] mapIterator) throws SQLException {
379         initialize();
380         int[] positions = new int[2];
381         if(position < mapIterator[0].position){
382             positions[0] = LESSTHANMIN;
383             return positions;
384         }
385         if(position > mapIterator[mapIterator.length - 1].position + mapIterator[mapIterator.length - 1].bytes.length - 1){
386             positions[0] = MORETHANMAX;
387             return positions;
388         }
389         for( int i = 0 ; i < mapIterator.length ; i++ ){
390             if( position >= mapIterator[i].position && position <= (mapIterator[i].position + mapIterator[i].bytes.length - 1)){
391                 positions[0] = -1;
392                 positions[1] = i;
393                 return positions;
394             }
395             else if( position > (mapIterator[i].position + mapIterator[i].bytes.length - 1) && position < mapIterator[i+1].position ){
396                 positions[0] = i;
397                 positions[1] = i + 1;
398                 return positions;
399             }
400         }
401         return null;
402     }
403
404
405     public void setStartingClusterAddress(int startClusterAddress0){
406         startClusterAddress = startClusterAddress0;
407     }
408
409     public int getStartingClusterAddress(){
410         return startClusterAddress;
411     }
412
413     public byte [] getBytes(){
414         try {
415             if( blob != null && bytes == null){
416                 byte[] blobBytes = blob.getBytes(1,(int) blob.length());
417                    return blobBytes;
418             }else if(isStream){
419               return getBytes(1, stream != null ? stream.available() : 0);
420             }
421         }
422         catch (Exception JavaDoc ex) {
423         }
424         return bytes;
425     }
426
427     public void setStream(InputStream stream0) {
428         stream = stream0;
429         isStream = true;
430     }
431
432     public void setDBLob(boolean flag) {
433         isDBLob = flag;
434     }
435
436     public Object JavaDoc getObject() throws DException{
437         return isNull ? null : this;//new Long(startClusterAddress);
438
}
439
440     public BufferRange getBufferRange() {
441         if(isNull)
442             return FieldUtility.NULLBUFFERRANGE;
443         byte[] bytes = new byte[6];
444         System.arraycopy(CCzufDpowfsufs.getBytes(startClusterAddress),0,bytes,0,Datatype.INTSIZE);
445         System.arraycopy(CCzufDpowfsufs.getBytes(recordNumber),0,bytes,4,Datatype.SHORTSIZE);
446         return new BufferRange(bytes);
447     }
448
449     public void setBufferRange(BufferRange bufferRange) {
450         if(!bufferRange.getNull()) {
451             startClusterAddress = CCzufDpowfsufs.getIntValue(bufferRange.getBytes(),0);
452             isNull = false;
453         }
454     }
455
456
457     public void setNull(boolean flag){
458         isNull = flag;
459     }
460
461     public boolean getNull() {
462         return isNull;
463     }
464
465     public boolean isNull() {
466         return isNull;
467     }
468
469     public int getDatatype() throws DException{
470         return Datatype.BLOB;
471     }
472     public String JavaDoc toString(){
473         return "java.sql.Blob";
474     }
475
476
477     public void setRecordNumber(short rec) {
478         recordNumber = rec;
479     }
480
481     public short getRecordNumber() {
482         return recordNumber;
483     }
484
485     public LobManager getLobManager(){
486       return blob == null ? null : blob.getLobManager();
487     }
488
489     public int getLength() {
490       try {
491         return (int) length();
492       }
493       catch (SQLException ex) {
494         return -1;
495       }
496  }
497
498  public byte[] readBytes(long pos,int len ) throws DException {
499   try {
500     return getBytes(pos+1, len);
501   }
502   catch (SQLException ex) {
503      throw new DException("DSE0",new Object JavaDoc[] {ex.getMessage()});
504   }
505  }
506
507       public void writeInFile(_DatabaseUser user) throws SQLException {
508           try{
509               initialize();
510               if(StartingBlobLength != lengthOfBlob){
511                   blob.truncate(lengthOfBlob);
512                   StartingBlobLength = lengthOfBlob;
513               }
514               if(mappings == null)
515                   return;
516               Iterator iter = mappings.iterator();
517               while(iter.hasNext()){
518                   Mapping mp = (Mapping)iter.next();
519                   blob.setBytes(user,mp.position,mp.bytes);
520               }
521               mappings.clear();
522           }catch(DException ex){
523               throw ex.getSqlException(null);
524           }
525       }
526 }
527
528 /*
529       if(bytes = null){
530          if(blob == null)
531             return null;
532          bytes = blob.getBytes(pos,length);
533          startPoint = (int)pos;
534          return bytes;
535       }
536       if(pos <= startPoint){
537          byte[] temp = blob.getBytes(pos,startPoint-pos);
538          byte[] temp1 = new byte[temp.length+bytes.length];
539          System.arraycopy(temp,0,temp1,0,temp.length);
540          System.arraycopy(bytes,0,temp1,temp.length,bytes.length);
541          bytes = temp1;
542          startPoint = pos;
543          if(temp1.length >= length){
544             temp1 = new byte[length];
545             System.arraycopy(bytes,0,temp1,0,length);
546          }
547          else{
548             int st = length - temp1.length;
549             temp = blob.getBytes(pos + temp1.length ,st);
550             byte[] temp1 = new byte[temp.length+bytes.length];
551             System.arraycopy(bytes,0,temp1,0,bytes.length);
552             System.arraycopy(temp,0,temp1,bytes.length,temp.length);
553             bytes = temp1;
554          }
555          return temp1;
556       }
557       else {
558          int tl = startPoint+bytes.length;
559          if(tl < pos){
560             byte[] temp = blob.getBytes(tl,length + pos - tl);
561             byte[] temp1 = new byte[temp.length + bytes.length];
562             System.arraycopy(bytes,0,temp1,0,bytes.length);
563             System.arraycopy(temp,0,temp1,bytes.length,temp.length);
564             bytes = temp1;
565             temp1 = new byte[length];
566             System.arraycopy(bytes,pos-startPoint,temp1,0,length);
567             return temp1;
568          }
569          else{
570             int st = pos - tl;
571             if(length - st <= 0){
572                temp1 = new byte[length];
573                System.arraycopy(bytes,pos-startPoint,temp1,0,length);
574                return temp1;
575             }
576             byte[] temp = blob.getBytes(tl,length - st);
577             byte[] temp1 = new byte[temp.length + bytes.length];
578             System.arraycopy(bytes,0,temp1,0,bytes.length);
579             System.arraycopy(temp,0,temp1,bytes.length,temp.length);
580             bytes = temp1;
581             temp1 = new byte[length];
582             System.arraycopy(bytes,pos-startPoint,temp1,0,length);
583             return temp1;
584          }
585       }
586 */

587
Popular Tags