KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > jdbc > MStreamableClob


1 /**
2  * com.mckoi.database.jdbc.MStreamableClob 31 Jan 2003
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.jdbc;
26
27 import java.io.*;
28 import java.sql.Clob JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 /**
32  * A Clob that is a large object that may be streamed from the server directly
33  * to this object. A clob that is streamable is only alive for the lifetime of
34  * the result set it is part of. If the underlying result set that contains
35  * this streamable clob is closed then this clob is no longer valid.
36  *
37  * @author Tobias Downer
38  */

39
40 class MStreamableClob extends AbstractStreamableObject implements Clob JavaDoc {
41
42   /**
43    * Constructs the Clob.
44    */

45   MStreamableClob(MConnection connection, int result_set_id, byte type,
46                  long streamable_object_id, long size) {
47     super(connection, result_set_id, type, streamable_object_id, size);
48   }
49
50   // ---------- Implemented from Blob ----------
51

52   public long length() throws SQLException JavaDoc {
53     if (getType() == 4) {
54       return rawSize() / 2;
55     }
56     return rawSize();
57   }
58
59   public String JavaDoc getSubString(long pos, int length) throws SQLException JavaDoc {
60     int p = (int) (pos - 1);
61     Reader reader = getCharacterStream();
62     try {
63       reader.skip(p);
64       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(length);
65       for (int i = 0; i < length; ++i) {
66         int c = reader.read();
67         buf.append((char) c);
68       }
69       return new String JavaDoc(buf);
70     }
71     catch (IOException e) {
72       e.printStackTrace(System.err);
73       throw new SQLException JavaDoc("IO Error: " + e.getMessage());
74     }
75   }
76
77   public Reader getCharacterStream() throws SQLException JavaDoc {
78     if (getType() == 3) {
79       return new AsciiReader(new StreamableObjectInputStream(rawSize()));
80     }
81     else if (getType() == 4) {
82       return new BinaryToUnicodeReader(
83                              new StreamableObjectInputStream(rawSize()));
84     }
85     else {
86       throw new SQLException JavaDoc("Unknown type.");
87     }
88   }
89
90   public java.io.InputStream JavaDoc getAsciiStream() throws SQLException JavaDoc {
91     if (getType() == 3) {
92       return new StreamableObjectInputStream(rawSize());
93     }
94     else if (getType() == 4) {
95       return new AsciiInputStream(getCharacterStream());
96     }
97     else {
98       throw new SQLException JavaDoc("Unknown type.");
99     }
100   }
101
102   public long position(String JavaDoc searchstr, long start) throws SQLException JavaDoc {
103     throw MSQLException.unsupported();
104   }
105   
106   public long position(Clob JavaDoc searchstr, long start) throws SQLException JavaDoc {
107     throw MSQLException.unsupported();
108   }
109
110   //#IFDEF(JDBC3.0)
111

112   //---------------------------- JDBC 3.0 -----------------------------------
113

114   public int setString(long pos, String JavaDoc str) throws SQLException JavaDoc {
115     throw MSQLException.unsupported();
116   }
117
118   public int setString(long pos, String JavaDoc str, int offset, int len)
119                                                           throws SQLException JavaDoc {
120     throw MSQLException.unsupported();
121   }
122
123   public java.io.OutputStream JavaDoc setAsciiStream(long pos) throws SQLException JavaDoc {
124     throw MSQLException.unsupported();
125   }
126
127   public java.io.Writer JavaDoc setCharacterStream(long pos) throws SQLException JavaDoc {
128     throw MSQLException.unsupported();
129   }
130
131   public void truncate(long len) throws SQLException JavaDoc {
132     throw MSQLException.unsupported();
133   }
134
135   //#ENDIF
136

137 }
138
139
Popular Tags