KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.jdbc.AbstractStreamableObject 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.SQLException JavaDoc;
29 import com.mckoi.util.PagedInputStream;
30
31 /**
32  * An abstract class that provides various convenience behaviour for
33  * creating streamable java.sql.Blob and java.sql.Clob classes. A streamable
34  * object is typically a large object that can be fetched in separate pieces
35  * from the server. A streamable object only survives for as long as the
36  * ResultSet that it is part of is open.
37  *
38  * @author Tobias Downer
39  */

40
41 abstract class AbstractStreamableObject {
42
43   /**
44    * The MConnection object that this object was returned as part of the result
45    * of.
46    */

47   protected final MConnection connection;
48   
49   /**
50    * The result_id of the ResultSet this clob is from.
51    */

52   protected final int result_set_id;
53   
54   /**
55    * The streamable object identifier.
56    */

57   private final long streamable_object_id;
58   
59   /**
60    * The type of encoding of the stream.
61    */

62   private final byte type;
63   
64   /**
65    * The size of the streamable object.
66    */

67   private final long size;
68
69   /**
70    * Constructor.
71    */

72   AbstractStreamableObject(MConnection connection, int result_set_id,
73                            byte type, long streamable_object_id, long size) {
74     this.connection = connection;
75     this.result_set_id = result_set_id;
76     this.type = type;
77     this.streamable_object_id = streamable_object_id;
78     this.size = size;
79   }
80
81   /**
82    * Returns the streamable object identifier for referencing this streamable
83    * object on the server.
84    */

85   protected long getStreamableId() {
86     return streamable_object_id;
87   }
88   
89   /**
90    * Returns the encoding type of this object.
91    */

92   protected byte getType() {
93     return type;
94   }
95   
96   /**
97    * Returns the number of bytes in this streamable object. Note that this
98    * may not represent the actual size of the object when it is decoded. For
99    * example, a Clob may be encoded as 2-byte per character (unicode) so the
100    * actual length of the clob with be size / 2.
101    */

102   protected long rawSize() {
103     return size;
104   }
105
106   
107   
108   
109   
110   // ---------- Inner classes ----------
111

112   /**
113    * An InputStream that is used to read the data from the streamable object as
114    * a basic byte encoding. This maintains an internal buffer.
115    */

116   class StreamableObjectInputStream extends PagedInputStream {
117
118     /**
119      * The default size of the buffer.
120      */

121     private final static int B_SIZE = 64 * 1024;
122
123     /**
124      * Construct the input stream.
125      */

126     public StreamableObjectInputStream(long in_size) {
127       super(B_SIZE, in_size);
128     }
129
130     protected void readPageContent(byte[] buf, long pos, int length)
131                                                           throws IOException {
132       try {
133         // Request a part of the blob from the server
134
StreamableObjectPart part = connection.requestStreamableObjectPart(
135                        result_set_id, streamable_object_id, pos, length);
136         System.arraycopy(part.getContents(), 0, buf, 0, length);
137       }
138       catch (SQLException JavaDoc e) {
139         throw new IOException("SQL Error: " + e.getMessage());
140       }
141     }
142     
143   }
144
145 }
146
147
Popular Tags