File: OPLHeapClob.java

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (688 lines) | stat: -rw-r--r-- 20,608 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
 *  $Id: OPLHeapClob.java,v 1.2.2.1 2010/02/02 23:03:58 source Exp $
 *
 *  Implementation of the JDBC Clob class
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2010 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

package openlink.util;

import java.sql.Clob;
import java.io.*;
import java.sql.SQLException;

public class OPLHeapClob implements Clob, Serializable {

  static final long serialVersionUID = 6296947263965593908L;

  private char[] blobData = null;
  private long  blobLength = 0;
  private final static int buf_size = 0x8000;
  private Object lck;

  public OPLHeapClob() {
    lck = this;
    blobData = new char[1];
  }

  public OPLHeapClob(String b) {
    lck = this;
    blobData = b.toCharArray();
    blobLength = blobData.length;
  }

  public OPLHeapClob(char[] b) {
    lck = this;
    blobData = new char[b.length];
    System.arraycopy(b, 0, blobData, 0, b.length);
    blobLength = b.length;
  }

  public OPLHeapClob(Reader is) throws SQLException {
    lck = this;
    try {
      CharArrayWriter out = new CharArrayWriter(buf_size);
      BufferedReader in = new BufferedReader(is, buf_size);
      char[] tmp = new char[buf_size];
      int sz = in.read(tmp, 0, buf_size);

      while( sz != -1 ) {
  	out.write(tmp, 0, sz);
	sz = in.read(tmp, 0, buf_size);
      }
      blobData = out.toCharArray();
      blobLength = blobData.length;
    } catch( IOException e ) {
      throw OPLMessage_u.makeException(e);
    }
  }

  /**
   * Retrieves the number of characters
   * in the <code>CLOB</code> value
   * designated by this <code>Clob</code> object.
   *
   * @return length of the <code>CLOB</code> in characters
   * @exception SQLException if there is an error accessing the
   *            length of the <code>CLOB</code> value
   * @since 1.2
   */
  public long length() throws SQLException {
    ensureOpen();
    synchronized(lck) {
      return blobLength;
    }
  }

  /**
   * Retrieves a copy of the specified substring
   * in the <code>CLOB</code> value
   * designated by this <code>Clob</code> object.
   * The substring begins at position
   * <code>pos</code> and has up to <code>length</code> consecutive
   * characters.
   *
   * @param pos the first character of the substring to be extracted.
   *            The first character is at position 1.
   * @param length the number of consecutive characters to be copied
   * @return a <code>String</code> that is the specified substring in
   *         the <code>CLOB</code> value designated by this <code>Clob</code> object
   * @exception SQLException if there is an error accessing the
   *            <code>CLOB</code> value
   * @since 1.2
   */
  public String getSubString(long pos, int len) throws SQLException {
    ensureOpen();
    synchronized(lck) {
      pos--;
      if ( pos >= blobLength )
        throw OPLMessage_u.makeException(OPLMessage_u.erru_Invalid_start_position);

      if ( len > blobLength - pos )
        len = (int)(blobLength - pos);

      return new String(blobData, (int)pos, len);
    }
  }

  /**
   * Retrieves the <code>CLOB</code> value designated by this <code>Clob</code>
   * object as a <code>java.io.Reader</code> object (or as a stream of
   * characters).
   *
   * @return a <code>java.io.Reader</code> object containing the
   *         <code>CLOB</code> data
   * @exception SQLException if there is an error accessing the
   *            <code>CLOB</code> value
   * @see #setCharacterStream
   * @since 1.2
   */
  public Reader getCharacterStream() throws SQLException {
    ensureOpen();
    return new OPLHeapClob.BlobInputReader(lck);
  }

  /**
   * Retrieves the <code>CLOB</code> value designated by this <code>Clob</code>
   * object as an ascii stream.
   *
   * @return a <code>java.io.InputStream</code> object containing the
   *         <code>CLOB</code> data
   * @exception SQLException if there is an error accessing the
   *            <code>CLOB</code> value
   * @see #setAsciiStream
   * @since 1.2
   */
  public InputStream getAsciiStream() throws SQLException {
    ensureOpen();
    return new BlobInputStream(getCharacterStream());
  }

  /**
   * Retrieves the character position at which the specified substring
   * <code>searchstr</code> appears in the SQL <code>CLOB</code> value
   * represented by this <code>Clob</code> object.  The search
   * begins at position <code>start</code>.
   *
   * @param searchstr the substring for which to search
   * @param start the position at which to begin searching; the first position
   *              is 1
   * @return the position at which the substring appears or -1 if it is not
   *         present; the first position is 1
   * @exception SQLException if there is an error accessing the
   *            <code>CLOB</code> value
   * @since 1.2
   */
  public long position(String searchstr, long start) throws SQLException {
    ensureOpen();
    synchronized(lck) {
      if ( start < 1 )
        throw OPLMessage_u.makeException(OPLMessage_u.erru_Invalid_start_position);

      start--;
      boolean match;

      if (start > blobLength)
        return -1;

      for(int i=(int)start; i<blobLength; i++) {
        if( searchstr.length() > (blobLength-i) )
            break;

        if( blobData[i] == searchstr.charAt(0) ) {
  	    match = true;

	    for(int j=1; j<searchstr.length(); j++) {
	      if( blobData[i+j] != searchstr.charAt(j) ) {
	 	 match = false;
		 break;
              }
            }
	    if( match )
	       return i+1;
        }
      }
    }
    return -1;
  }

  /**
   * Retrieves the character position at which the specified
   * <code>Clob</code> object <code>searchstr</code> appears in this
   * <code>Clob</code> object.  The search begins at position
   * <code>start</code>.
   *
   * @param searchstr the <code>Clob</code> object for which to search
   * @param start the position at which to begin searching; the first
   *              position is 1
   * @return the position at which the <code>Clob</code> object appears
   *              or -1 if it is not present; the first position is 1
   * @exception SQLException if there is an error accessing the
   *            <code>CLOB</code> value
   * @since 1.2
   */
  public long position(Clob searchstr, long start) throws SQLException {
    ensureOpen();
    if( start < 1 )
        throw OPLMessage_u.makeException(OPLMessage_u.erru_Invalid_start_position);

    return position(searchstr.getSubString(0, (int)searchstr.length()), start);
  }

    //---------------------------- jdbc 3.0 -----------------------------------

#if JDK_VER >= 14

    /**
     * Writes the given Java <code>String</code> to the <code>CLOB</code>
     * value that this <code>Clob</code> object designates at the position
     * <code>pos</code>.
     *
     * @param pos the position at which to start writing to the <code>CLOB</code>
     *         value that this <code>Clob</code> object represents
     * @param str the string to be written to the <code>CLOB</code>
     *        value that this <code>Clob</code> designates
     * @return the number of characters written
     * @exception SQLException if there is an error accessing the
     *            <code>CLOB</code> value
     *
     * @since 1.4
     */
  public int setString(long pos, String str) throws SQLException {
    ensureOpen();
    return setString(pos, str, 0, str.length());
  }

    /**
     * Writes <code>len</code> characters of <code>str</code>, starting
     * at character <code>offset</code>, to the <code>CLOB</code> value
     * that this <code>Clob</code> represents.
     *
     * @param pos the position at which to start writing to this
     *        <code>CLOB</code> object
     * @param str the string to be written to the <code>CLOB</code>
     *        value that this <code>Clob</code> object represents
     * @param offset the offset into <code>str</code> to start reading
     *        the characters to be written
     * @param len the number of characters to be written
     * @return the number of characters written
     * @exception SQLException if there is an error accessing the
     *            <code>CLOB</code> value
     *
     * @since 1.4
     */
  public int setString(long pos, String str, int offset, int len) throws SQLException {
    ensureOpen();
    synchronized (lck) {
      Writer os = new BlobOutputWriter(lck, pos);
      try {
        os.write(str, offset, len);
      } catch (IOException e) {
        OPLMessage_u.makeException(e);
      }
    }
    return len;
  }

    /**
     * Retrieves a stream to be used to write Ascii characters to the
     * <code>CLOB</code> value that this <code>Clob</code> object represents,
     * starting at position <code>pos</code>.
     *
     * @param pos the position at which to start writing to this
     *        <code>CLOB</code> object
     * @return the stream to which ASCII encoded characters can be written
     * @exception SQLException if there is an error accessing the
     *            <code>CLOB</code> value
     * @see #getAsciiStream
     *
     * @since 1.4
     */
  public java.io.OutputStream setAsciiStream(long pos) throws SQLException {
    ensureOpen();
    return new BlobOutputStream(new BlobOutputWriter(lck, pos));
  }

    /**
     * Retrieves a stream to be used to write a stream of Unicode characters
     * to the <code>CLOB</code> value that this <code>Clob</code> object
     * represents, at position <code>pos</code>.
     *
     * @param  pos the position at which to start writing to the
     *        <code>CLOB</code> value
     *
     * @return a stream to which Unicode encoded characters can be written
     * @exception SQLException if there is an error accessing the
     *            <code>CLOB</code> value
     * @see #getCharacterStream
     *
     * @since 1.4
     */
  public java.io.Writer setCharacterStream(long pos) throws SQLException {
    ensureOpen();
    return new BlobOutputWriter(lck, pos);
  }

    /**
     * Truncates the <code>CLOB</code> value that this <code>Clob</code>
     * designates to have a length of <code>len</code>
     * characters.
     * @param len the length, in bytes, to which the <code>CLOB</code> value
     *        should be truncated
     * @exception SQLException if there is an error accessing the
     *            <code>CLOB</code> value
     *
     * @since 1.4
     */
  public void truncate(long len) throws SQLException {
    ensureOpen();
    synchronized(lck) {
      int newLen = (int)len;
      if (newLen < 0 || newLen > blobData.length)
         throw OPLMessage_u.makeException(OPLMessage_u.erru_Invalid_length);
      if (newLen < blobData.length) {
        char[] newbuf = new char[newLen];
        System.arraycopy(blobData, 0, newbuf, 0, newLen);
        blobData = newbuf;
      }
      blobLength = len;
    }
  }

#if JDK_VER >= 16
    /**
     * This method frees the <code>Clob</code> object and releases the resources the resources
     * that it holds.  The object is invalid once the <code>free</code> method
     * is called.
     * <p>
     * After <code>free</code> has been called, any attempt to invoke a
     * method other than <code>free</code> will result in a <code>SQLException</code>
     * being thrown.  If <code>free</code> is called multiple times, the subsequent
     * calls to <code>free</code> are treated as a no-op.
     * <p>
     * @throws SQLException if an error occurs releasing
     * the Clob's resources
     *
     * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
     * this method
     * @since 1.6
     */
  public void free() throws SQLException
  {
    synchronized (lck) {
        blobData = null;
    }
  }

    /**
     * Returns a <code>Reader</code> object that contains a partial <code>Clob</code> value, starting
     * with the character specified by pos, which is length characters in length.
     *
     * @param pos the offset to the first character of the partial value to
     * be retrieved.  The first character in the Clob is at position 1.
     * @param length the length in characters of the partial value to be retrieved.
     * @return <code>Reader</code> through which the partial <code>Clob</code> value can be read.
     * @throws SQLException if pos is less than 1 or if pos is greater than the number of
     * characters in the <code>Clob</code> or if pos + length is greater than the number of
     * characters in the <code>Clob</code>
     *
     * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
     * this method
     * @since 1.6
     */
  public Reader getCharacterStream(long pos, long length) throws SQLException
  {
    ensureOpen();
    return new OPLHeapClob.BlobInputReader(lck, pos, length);
  }


#endif
#endif

  private void ensureOpen() throws SQLException
  {
    if (blobData == null)
       throw OPLMessage_u.makeException(OPLMessage_u.erru_Blob_is_freed);
  }


  // === Inner class ===========================================
  protected class BlobInputReader extends Reader {

    private boolean isClosed = false;
    private int pos = 0;
    private long length = 0;

    protected BlobInputReader(Object lck) {
      super(lck);
      length = blobData.length;
    }

    protected BlobInputReader(Object lck, long pos, long length) {
      super(lck);
      this.pos = (int)pos;
      this.length = length;
    }

    public int read() throws IOException {
      synchronized (lock) {
        ensureOpen();
        return (pos < length) ? blobData[pos++] : -1;
      }
    }

    public int read(char b[], int off, int len) throws IOException {
      synchronized (lock) {
	ensureOpen();
	if (b == null)
	    throw new NullPointerException();

	if ((off < 0) || (off > b.length) || (len < 0) ||
		   ((off + len) > b.length) || ((off + len) < 0))
	    throw new IndexOutOfBoundsException();

	if (pos >= length)
	    return -1;

	if (pos + len > length)
	    len = (int)(length - pos);

        if (len <= 0)
           return 0;

	System.arraycopy(blobData, pos, b, off, len);
	pos += len;
	return len;
      }
    }

    public synchronized long skip(long n) throws IOException {
	synchronized (lock) {
	    ensureOpen();
	    if (pos + n > length)
		n = length - pos;

	    if (n < 0)
		return 0;

	    pos += n;
	    return n;
	}
    }

    public boolean ready() throws IOException {
      synchronized (lock) {
        ensureOpen();
        return true;
      }
    }

    public void close() {
	isClosed = true;
    }

    private void ensureOpen() throws IOException {
        if (isClosed || blobData == null)
          throw new IOException(OPLMessage_u.getMessage(OPLMessage_u.erru_Stream_is_closed));
    }
  }

  // === Inner class ===========================================
  protected class BlobInputStream extends InputStream {
    private static final int defBufSize = 32;
    private byte[] buf;
    private char[] cbuf;
    private int pos = 0;
    private int count = 0;
    private Reader in;

    BlobInputStream(Reader in) {
      buf = new byte[defBufSize];
      cbuf = new char[defBufSize / 4];
      this.in = in;
    }

    private void ensureOpen() throws IOException {
      if (in == null || blobData == null)
         throw new IOException(OPLMessage_u.getMessage(OPLMessage_u.erru_Stream_is_closed));
    }

    private void fill() throws IOException {
      count = pos = 0;
      int cnt;

      if ((cnt = in.read(cbuf)) == -1)
         return;//EOF

      byte[] tmp = (new String(cbuf)).getBytes();
      System.arraycopy(tmp, 0, buf, 0, tmp.length);
      count = tmp.length;
    }

    public synchronized int read() throws IOException {
      ensureOpen();
      if (pos >= count) {
          fill();
          if (pos >= count)
	      return -1;
      }
      return buf[pos++] & 0xff;
    }

    private int read1(byte[] b, int off, int len) throws IOException {
      int avail = count - pos;
      if (avail <= 0) {
          fill();
          avail = count - pos;
          if (avail <= 0) return -1;
      }
      int cnt = (avail < len) ? avail : len;
      System.arraycopy(buf, pos, b, off, cnt);
      pos += cnt;
      return cnt;
    }

    public synchronized int read(byte b[], int off, int len)
	  throws IOException
    {
      ensureOpen();
      if ((off | len | (off + len) | (b.length - (off + len))) < 0)
        throw new IndexOutOfBoundsException();
      else if (len == 0)
        return 0;

      int n = read1(b, off, len);
      if (n <= 0) return n;
      while (n < len) {
          int n1 = read1(b, off + n, len - n);
          if (n1 <= 0) break;
          n += n1;
      }
      return n;
    }

    public synchronized int available() throws IOException {
      ensureOpen();
      return (count - pos);
    }

    public synchronized void close() throws IOException
    {
      buf = null;
      cbuf = null;
      in.close();
      in = null;
    }

  }

#if JDK_VER >= 14
  // === Inner class ===========================================
  protected class BlobOutputWriter extends Writer {

    protected int count;
    private boolean isClosed = false;

    protected BlobOutputWriter(Object lck, long pos) {
      super(lck);
      count = (int)pos;
    }

    public void write(int c) throws IOException {
      ensureOpen();
      synchronized (lock) {
	int newcount = count + 1;
	if (newcount > blobData.length) {
	   char newbuf[] = new char[Math.max(blobData.length + buf_size, newcount)];
	   System.arraycopy(blobData, 0, newbuf, 0, count);
	   blobData = newbuf;
        }
	blobData[count] = (char)c;
	count = newcount;
      }
    }

    public void write(char c[], int off, int len) throws IOException {
      ensureOpen();
      if ((off < 0) || (off > c.length) || (len < 0) ||
         ((off + len) > c.length) || ((off + len) < 0)) {
	 throw new IndexOutOfBoundsException();
      } else if (len == 0) {
	 return;
      }
      synchronized (lock) {
	int newcount = count + len;
	if (newcount > blobData.length) {
          char newbuf[] = new char[Math.max(blobData.length + buf_size, newcount)];
	  System.arraycopy(blobData, 0, newbuf, 0, count);
	  blobData = newbuf;
        }
	System.arraycopy(c, off, blobData, count, len);
	count = newcount;
      }
    }

    public void write(String str, int off, int len) throws IOException {
      ensureOpen();
      synchronized (lock) {
	int newcount = count + len;
	if (newcount > blobData.length) {
	  char newbuf[] = new char[Math.max(blobData.length + buf_size, newcount)];
  	  System.arraycopy(blobData, 0, newbuf, 0, count);
	  blobData = newbuf;
        }
	str.getChars(off, off + len, blobData, count);
	count = newcount;
      }
    }

    public void flush() { }

    public synchronized void close() {
      synchronized (lock) {
	isClosed = true;
      }
    }

    private void ensureOpen() throws IOException {
        if (isClosed || blobData == null)
          throw new IOException(OPLMessage_u.getMessage(OPLMessage_u.erru_Stream_is_closed ));
    }
  }


  // === Inner class ===========================================
  protected class BlobOutputStream extends OutputStream {

    private Writer out;
    private boolean isClosed = false;

    protected BlobOutputStream(Writer out) {
      this.out = out;
    }

    public synchronized void write(int b) throws IOException {
      ensureOpen();
      byte[] tmp = {(byte)b};
      out.write(new String(tmp));
    }

    public void write(byte b[], int off, int len) throws IOException {
      ensureOpen();
      if (len == 0)
	 return;
      out.write(new String(b, off, len));
    }

    public void close() throws IOException{
      isClosed = true;
      out.close();
    }

    private void ensureOpen() throws IOException {
      if (isClosed || blobData == null)
         throw new IOException(OPLMessage_u.getMessage(OPLMessage_u.erru_Stream_is_closed));
    }

  }
#endif

}