1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 /*
29  * ByteArrayXOutputStream.java
30  *
31  * Created on 11. April 2003, 14:20
32  */
33 
34 package com.sun.star.lib.uno.adapter;
35 
36 /**
37  *
38  * @author  lo119109
39  */
40 
41 import com.sun.star.io.*;
42 import  com.sun.star.lib.uno.helper.ComponentBase;
43 
44 public class XOutputStreamToByteArrayAdapter
45 	extends ComponentBase
46 	implements XOutputStream
47 {
48     private int initialSize = 100240; // 10 kb
49     private int size = 0;
50     private int position = 0;
51     private boolean externalBuffer = false;
52     private boolean closed = false;
53     private byte[] buffer;
54 
55     /** Creates a new instance of ByteArrayXOutputStream */
56     public XOutputStreamToByteArrayAdapter() {
57         this(null);
58     }
59 
60     public XOutputStreamToByteArrayAdapter(byte[] aBuffer) {
61         if (aBuffer != null) {
62             externalBuffer = true;
63             buffer = aBuffer;
64             size = buffer.length;
65             // System.err.println("new outputbuffer with external storage");
66         } else {
67             size = initialSize;
68             buffer = new byte[size];
69             // System.err.println("new outputbuffer with internal storage");
70         }
71     }
72 
73     public byte[] getBuffer() {
74         return buffer;
75     }
76 
77     public void closeOutput()
78         throws com.sun.star.io.NotConnectedException,
79             com.sun.star.io.BufferSizeExceededException,
80             com.sun.star.io.IOException
81     {
82         // trim buffer
83         if ( buffer.length > position && !externalBuffer )
84 		{
85             byte[] newBuffer = new byte[position];
86             System.arraycopy(buffer, 0, newBuffer, 0, position);
87             buffer = newBuffer;
88         }
89         closed = true;
90     }
91 
92     public void flush()
93         throws com.sun.star.io.NotConnectedException,
94             com.sun.star.io.BufferSizeExceededException,
95             com.sun.star.io.IOException
96     {
97     }
98 
99     public void writeBytes(byte[] values)
100         throws com.sun.star.io.NotConnectedException,
101             com.sun.star.io.BufferSizeExceededException,
102             com.sun.star.io.IOException
103     {
104         // System.err.println("writeBytes("+values.length+")");
105         if ( values.length > size-position )
106 		{
107             if ( externalBuffer )
108 				throw new BufferSizeExceededException("out of buffer space, cannot grow external buffer");
109             byte[] newBuffer = null;
110             while ( values.length > size-position )
111 				size *= 2;
112             // System.err.println("new buffer size is "+size+" bytes.");
113 			newBuffer = new byte[size];
114             System.arraycopy(buffer, 0, newBuffer, 0, position);
115             buffer = newBuffer;
116         }
117         System.arraycopy(values, 0, buffer, position, values.length);
118         position += values.length;
119     }
120 
121 }
122