Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / javaunohelper / com / sun / star / lib / uno / adapter / XOutputStreamToByteArrayAdapter.java
blob82c1aceecaeb74eddf0e179fa3b6736bed541676
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
29 * ByteArrayXOutputStream.java
31 * Created on 11. April 2003, 14:20
34 package com.sun.star.lib.uno.adapter;
36 /**
38 * @author lo119109
41 import com.sun.star.io.*;
42 import com.sun.star.lib.uno.helper.ComponentBase;
44 public class XOutputStreamToByteArrayAdapter
45 extends ComponentBase
46 implements XOutputStream
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;
55 /** Creates a new instance of ByteArrayXOutputStream */
56 public XOutputStreamToByteArrayAdapter() {
57 this(null);
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");
73 public byte[] getBuffer() {
74 return buffer;
77 public void closeOutput()
78 throws com.sun.star.io.NotConnectedException,
79 com.sun.star.io.BufferSizeExceededException,
80 com.sun.star.io.IOException
82 // trim buffer
83 if ( buffer.length > position && !externalBuffer )
85 byte[] newBuffer = new byte[position];
86 System.arraycopy(buffer, 0, newBuffer, 0, position);
87 buffer = newBuffer;
89 closed = true;
92 public void flush()
93 throws com.sun.star.io.NotConnectedException,
94 com.sun.star.io.BufferSizeExceededException,
95 com.sun.star.io.IOException
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
104 // System.err.println("writeBytes("+values.length+")");
105 if ( values.length > size-position )
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;
117 System.arraycopy(values, 0, buffer, position, values.length);
118 position += values.length;