KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > driver > BlobOutputStream


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2005 Emic Networks
4  * Contact: c-jdbc@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or any later
9  * version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  *
20  * Initial developer(s): Marc Herbert
21  * Contributor(s): ______________________.
22  */

23
24 package org.objectweb.cjdbc.driver;
25
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 /**
31  * This class defines a BlobOutputStream.
32  * <p>
33  * TODO: we should implement close() then error once we are closed.
34  *
35  * @author <a HREF="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
36  * @version 1.0
37  */

38 public class BlobOutputStream
39     extends OutputStream JavaDoc
40 {
41   /** The actual Blob we are pointing to */
42   Blob blob;
43   /** The current offset in the stream, counting from zero (NOT SQL style) */
44   int currentPos;
45
46   /**
47    * Creates a new <code>BlobOutputStream</code> object pointing to the given
48    * Blob (currently implemented as an array).
49    *
50    * @param b the reference to the underlying blob
51    * @param startPos the starting position in the array (counting from zero).
52    */

53   public BlobOutputStream(Blob b, int startPos)
54   {
55     super();
56     this.blob = b;
57     currentPos = startPos;
58   }
59
60   /**
61    * @see java.io.OutputStream#write(int)
62    */

63   public void write(int b) throws IOException JavaDoc
64   {
65     blob.getInternalByteArray()[currentPos] = (byte) b;
66     currentPos++;
67   }
68
69   /**
70    * @see java.io.OutputStream#write(byte[], int, int)
71    */

72   public void write(byte[] b, int off, int len) throws IOException JavaDoc
73   {
74     try
75     {
76       // SQL indexes count from 1
77
blob.setBytes(currentPos + 1, b, off, len);
78       currentPos += len;
79     }
80     catch (SQLException JavaDoc sqle)
81     {
82       throw (IOException JavaDoc) new IOException JavaDoc(sqle.getLocalizedMessage())
83           .initCause(sqle);
84     }
85
86   }
87
88 }
89
Popular Tags