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