01: /**
02: * Sequoia: Database clustering technology.
03: * Copyright (C) 2005 Emic Networks
04: * Contact: sequoia@continuent.org
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
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: */package org.continuent.sequoia.common.protocol;
21:
22: import java.io.IOException;
23: import java.io.OutputStream;
24: import java.sql.SQLException;
25:
26: /**
27: * This class defines a BlobOutputStream.
28: * <p>
29: * TODO: we should implement close() then error once we are closed.
30: *
31: * @author <a href="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
32: * @version 1.0
33: */
34: public class ByteArrayBlobOutputStream extends OutputStream {
35: /** The actual Blob we are pointing to */
36: ByteArrayBlob blob;
37: /** The current offset in the stream, counting from zero (NOT SQL style) */
38: int currentPos;
39:
40: /**
41: * Creates a new <code>BlobOutputStream</code> object pointing to the given
42: * Blob (currently implemented as an array).
43: *
44: * @param b the reference to the underlying blob
45: * @param startPos the starting position in the array (counting from zero).
46: */
47: public ByteArrayBlobOutputStream(ByteArrayBlob b, int startPos) {
48: super ();
49: this .blob = b;
50: currentPos = startPos;
51: }
52:
53: /**
54: * @see java.io.OutputStream#write(int)
55: */
56: public void write(int b) throws IOException {
57: blob.getInternalByteArray()[currentPos] = (byte) b;
58: currentPos++;
59: }
60:
61: /**
62: * @see java.io.OutputStream#write(byte[], int, int)
63: */
64: public void write(byte[] b, int off, int len) throws IOException {
65: try {
66: // SQL indexes count from 1
67: blob.setBytes(currentPos + 1, b, off, len);
68: currentPos += len;
69: } catch (SQLException sqle) {
70: throw (IOException) new IOException(sqle
71: .getLocalizedMessage()).initCause(sqle);
72: }
73:
74: }
75:
76: }
|