01: /*
02:
03: Derby - Class org.apache.derby.client.am.BlobOutputStream
04:
05: Licensed to the Apache Software Foundation (ASF) under one or more
06: contributor license agreements. See the NOTICE file distributed with
07: this work for additional information regarding copyright ownership.
08: The ASF licenses this file to You under the Apache License, Version 2.0
09: (the "License"); you may not use this file except in compliance with
10: the License. You may obtain a copy of the License at
11:
12: http://www.apache.org/licenses/LICENSE-2.0
13:
14: Unless required by applicable law or agreed to in writing, software
15: distributed under the License is distributed on an "AS IS" BASIS,
16: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: See the License for the specific language governing permissions and
18: limitations under the License.
19:
20: */
21:
22: package org.apache.derby.client.am;
23:
24: public class BlobOutputStream extends java.io.OutputStream {
25: private Blob blob_;
26: private long offset_;
27:
28: public BlobOutputStream(Blob blob, long offset) {
29: blob_ = blob;
30: offset_ = offset;
31:
32: /*
33: offset_=1 while blob_.binaryString_.length - blob_.dataOffset_ = 0
34: for a empty Blob hence check for offset_-1
35: */
36: if ((offset_ - 1) > (blob_.binaryString_.length - blob_.dataOffset_)) {
37: throw new IndexOutOfBoundsException();
38: }
39: }
40:
41: public void write(int b) throws java.io.IOException {
42:
43: byte[] newbuf = new byte[(int) offset_ + blob_.dataOffset_];
44: System.arraycopy(blob_.binaryString_, 0, newbuf, 0,
45: (int) offset_ - 1 + blob_.dataOffset_);
46: blob_.binaryString_ = newbuf;
47: blob_.binaryString_[(int) offset_ + blob_.dataOffset_ - 1] = (byte) b;
48: blob_.binaryStream_ = new java.io.ByteArrayInputStream(
49: blob_.binaryString_);
50: blob_.sqlLength_ = blob_.binaryString_.length
51: - blob_.dataOffset_;
52: offset_++;
53: }
54:
55: public void write(byte b[], int off, int len)
56: throws java.io.IOException {
57: if (b == null) {
58: throw new NullPointerException();
59: } else if ((off < 0) || (off > b.length) || (len < 0)
60: || ((off + len) > b.length) || ((off + len) < 0)) {
61: throw new IndexOutOfBoundsException();
62: } else if (len == 0) {
63: return;
64: }
65: byte[] newbuf = new byte[(int) offset_ - 1 + len
66: + blob_.dataOffset_];
67: System.arraycopy(blob_.binaryString_, 0, newbuf, 0,
68: (int) offset_ - 1 + blob_.dataOffset_);
69: blob_.binaryString_ = newbuf;
70: for (int i = 0; i < len; i++, offset_++) {
71: blob_.binaryString_[(int) offset_ + blob_.dataOffset_ - 1] = b[off
72: + i];
73: }
74: blob_.binaryStream_ = new java.io.ByteArrayInputStream(
75: blob_.binaryString_);
76: blob_.sqlLength_ = blob_.binaryString_.length
77: - blob_.dataOffset_;
78: }
79: }
|