1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XOutputStreamToByteArrayAdapter.java,v $
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
;
44 import com
.sun
.star
.io
.*;
45 import com
.sun
.star
.lib
.uno
.helper
.ComponentBase
;
47 public class XOutputStreamToByteArrayAdapter
49 implements XOutputStream
51 private int initialSize
= 100240; // 10 kb
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() {
63 public XOutputStreamToByteArrayAdapter(byte[] aBuffer
) {
64 if (aBuffer
!= null) {
65 externalBuffer
= true;
68 // System.err.println("new outputbuffer with external storage");
71 buffer
= new byte[size
];
72 // System.err.println("new outputbuffer with internal storage");
76 public byte[] getBuffer() {
80 public void closeOutput()
81 throws com
.sun
.star
.io
.NotConnectedException
,
82 com
.sun
.star
.io
.BufferSizeExceededException
,
83 com
.sun
.star
.io
.IOException
86 if ( buffer
.length
> position
&& !externalBuffer
)
88 byte[] newBuffer
= new byte[position
];
89 System
.arraycopy(buffer
, 0, newBuffer
, 0, position
);
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
)
115 // System.err.println("new buffer size is "+size+" bytes.");
116 newBuffer
= new byte[size
];
117 System
.arraycopy(buffer
, 0, newBuffer
, 0, position
);
120 System
.arraycopy(values
, 0, buffer
, position
, values
.length
);
121 position
+= values
.length
;