KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > protocol > ByteArrayBlobOutputStream


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2005 Emic Networks
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Marc Herbert
19  * Contributor(s): ______________________.
20  */

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

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

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

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

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