1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 /*
25  * ByteArrayXOutputStream.java
26  *
27  * Created on 11. April 2003, 14:20
28  */
29 
30 package com.sun.star.lib.uno.adapter;
31 
32 /**
33  *
34  * @author  lo119109
35  */
36 
37 import com.sun.star.io.*;
38 import  com.sun.star.lib.uno.helper.ComponentBase;
39 
40 public class XOutputStreamToByteArrayAdapter
41 	extends ComponentBase
42 	implements XOutputStream
43 {
44     private int initialSize = 100240; // 10 kb
45     private int size = 0;
46     private int position = 0;
47     private boolean externalBuffer = false;
48     private boolean closed = false;
49     private byte[] buffer;
50 
51     /** Creates a new instance of ByteArrayXOutputStream */
52     public XOutputStreamToByteArrayAdapter() {
53         this(null);
54     }
55 
56     public XOutputStreamToByteArrayAdapter(byte[] aBuffer) {
57         if (aBuffer != null) {
58             externalBuffer = true;
59             buffer = aBuffer;
60             size = buffer.length;
61             // System.err.println("new outputbuffer with external storage");
62         } else {
63             size = initialSize;
64             buffer = new byte[size];
65             // System.err.println("new outputbuffer with internal storage");
66         }
67     }
68 
69     public byte[] getBuffer() {
70         return buffer;
71     }
72 
73     public void closeOutput()
74         throws com.sun.star.io.NotConnectedException,
75             com.sun.star.io.BufferSizeExceededException,
76             com.sun.star.io.IOException
77     {
78         // trim buffer
79         if ( buffer.length > position && !externalBuffer )
80 		{
81             byte[] newBuffer = new byte[position];
82             System.arraycopy(buffer, 0, newBuffer, 0, position);
83             buffer = newBuffer;
84         }
85         closed = true;
86     }
87 
88     public void flush()
89         throws com.sun.star.io.NotConnectedException,
90             com.sun.star.io.BufferSizeExceededException,
91             com.sun.star.io.IOException
92     {
93     }
94 
95     public void writeBytes(byte[] values)
96         throws com.sun.star.io.NotConnectedException,
97             com.sun.star.io.BufferSizeExceededException,
98             com.sun.star.io.IOException
99     {
100         // System.err.println("writeBytes("+values.length+")");
101         if ( values.length > size-position )
102 		{
103             if ( externalBuffer )
104 				throw new BufferSizeExceededException("out of buffer space, cannot grow external buffer");
105             byte[] newBuffer = null;
106             while ( values.length > size-position )
107 				size *= 2;
108             // System.err.println("new buffer size is "+size+" bytes.");
109 			newBuffer = new byte[size];
110             System.arraycopy(buffer, 0, newBuffer, 0, position);
111             buffer = newBuffer;
112         }
113         System.arraycopy(values, 0, buffer, position, values.length);
114         position += values.length;
115     }
116 
117 }
118