update credits
[LibreOffice.git] / javaunohelper / com / sun / star / lib / uno / adapter / XOutputStreamToByteArrayAdapter.java
blob48871277a75fad0c30f5133d68108b2500e814c3
1 /*
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
24 extends ComponentBase
25 implements XOutputStream
27 private static final int initialSize = 100240; // 10 kb
28 private int size = 0;
29 private int position = 0;
30 private boolean externalBuffer = false;
31 private byte[] buffer;
33 /** Creates a new instance of ByteArrayXOutputStream */
34 public XOutputStreamToByteArrayAdapter() {
35 this(null);
38 public XOutputStreamToByteArrayAdapter(byte[] aBuffer) {
39 if (aBuffer != null) {
40 externalBuffer = true;
41 buffer = aBuffer;
42 size = buffer.length;
43 } else {
44 size = initialSize;
45 buffer = new byte[size];
49 public byte[] getBuffer() {
50 return buffer;
53 public void closeOutput()
54 throws com.sun.star.io.NotConnectedException,
55 com.sun.star.io.BufferSizeExceededException,
56 com.sun.star.io.IOException
58 // trim buffer
59 if ( buffer.length > position && !externalBuffer )
61 byte[] newBuffer = new byte[position];
62 System.arraycopy(buffer, 0, newBuffer, 0, position);
63 buffer = newBuffer;
67 public void flush()
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 )
81 if ( externalBuffer )
82 throw new BufferSizeExceededException("out of buffer space, cannot grow external buffer");
83 while ( values.length > size-position ) {
84 size *= 2;
86 byte[] newBuffer = new byte[size];
87 System.arraycopy(buffer, 0, newBuffer, 0, position);
88 buffer = newBuffer;
90 System.arraycopy(values, 0, buffer, position, values.length);
91 position += values.length;