2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package com
.sun
.star
.lib
.uno
.adapter
;
20 import com
.sun
.star
.io
.*;
21 import com
.sun
.star
.lib
.uno
.helper
.ComponentBase
;
23 public final class XOutputStreamToByteArrayAdapter
25 implements XOutputStream
27 private static final int initialSize
= 100240; // 10 kb
29 private int position
= 0;
30 private boolean externalBuffer
= false;
31 private byte[] buffer
;
33 /** Creates a new instance of ByteArrayXOutputStream */
34 public XOutputStreamToByteArrayAdapter() {
38 public XOutputStreamToByteArrayAdapter(byte[] aBuffer
) {
39 if (aBuffer
!= null) {
40 externalBuffer
= true;
45 buffer
= new byte[size
];
49 public byte[] getBuffer() {
53 public void closeOutput()
54 throws com
.sun
.star
.io
.NotConnectedException
,
55 com
.sun
.star
.io
.BufferSizeExceededException
,
56 com
.sun
.star
.io
.IOException
59 if ( buffer
.length
> position
&& !externalBuffer
)
61 byte[] newBuffer
= new byte[position
];
62 System
.arraycopy(buffer
, 0, newBuffer
, 0, position
);
68 throws com
.sun
.star
.io
.NotConnectedException
,
69 com
.sun
.star
.io
.BufferSizeExceededException
,
70 com
.sun
.star
.io
.IOException
74 public void writeBytes(byte[] values
)
75 throws com
.sun
.star
.io
.NotConnectedException
,
76 com
.sun
.star
.io
.BufferSizeExceededException
,
77 com
.sun
.star
.io
.IOException
79 if ( values
.length
> size
-position
)
82 throw new BufferSizeExceededException("out of buffer space, cannot grow external buffer");
83 while ( values
.length
> size
-position
) {
86 byte[] newBuffer
= new byte[size
];
87 System
.arraycopy(buffer
, 0, newBuffer
, 0, position
);
90 System
.arraycopy(values
, 0, buffer
, position
, values
.length
);
91 position
+= values
.length
;