Update ooo320-m1
[ooovba.git] / javaunohelper / com / sun / star / lib / uno / adapter / ByteArrayToXInputStreamAdapter.java
bloba3262b31a5a8f9b7dc07e2c6dba858aef93eab5c
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: ByteArrayToXInputStreamAdapter.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 * ByteArrayXInputStram.java
34 * Created on 10. April 2003, 15:45
37 package com.sun.star.lib.uno.adapter;
39 /**
41 * @author lo119109
44 import com.sun.star.io.XInputStream;
45 import com.sun.star.io.XSeekable;
46 import com.sun.star.lib.uno.helper.ComponentBase;
48 public class ByteArrayToXInputStreamAdapter
49 extends ComponentBase
50 implements XInputStream, XSeekable
53 byte[] m_bytes;
54 int m_length;
55 int m_pos;
57 boolean m_open;
59 /** Creates a new instance of ByteArrayXInputStram */
60 public ByteArrayToXInputStreamAdapter(byte[] bytes) {
61 init(bytes);
64 public void init(byte[] bytes) {
65 // System.err.println("ByteArrayXInputStream");
66 m_bytes = bytes;
67 m_length = bytes.length;
68 m_pos = 0;
69 m_open = true;
72 private void _check() throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException {
73 if (m_bytes == null) {
74 // System.err.println("check failed no bytes!");
75 throw new com.sun.star.io.NotConnectedException("no bytes");
77 if(!m_open) {
78 // System.err.println("check failed: closed");
79 throw new com.sun.star.io.IOException("input closed");
83 public int available() throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException {
84 _check();
85 long a = m_length - m_pos;
86 if (a != (int)a)
87 throw new com.sun.star.io.IOException("integer overflow");
88 else {
89 // System.err.println("available() -> "+a);
90 return (int)a;
94 public void closeInput() throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException {
95 // System.err.println("closeInput()");
96 _check();
97 m_open = false;
100 public int readBytes(byte[][] values, int param) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException {
101 // System.err.println("readbytes(..., "+param+")");
102 _check();
103 try {
104 int remain = (int)(m_length - m_pos);
105 if (param > remain) param = remain;
106 /* ARGH!!! */
107 if (values[0] == null){
108 values[0] = new byte[param];
109 // System.err.println("allocated new buffer of "+param+" bytes");
111 System.arraycopy(m_bytes, m_pos, values[0], 0, param);
112 // System.err.println("readbytes() -> "+param);
113 m_pos += param;
114 return param;
115 } catch (ArrayIndexOutOfBoundsException ae) {
116 // System.err.println("readbytes() -> ArrayIndexOutOfBounds");
117 ae.printStackTrace();
118 throw new com.sun.star.io.BufferSizeExceededException("buffer overflow");
119 } catch (Exception e) {
120 // System.err.println("readbytes() -> Exception: "+e.getMessage());
121 e.printStackTrace();
122 throw new com.sun.star.io.IOException("error accessing buffer");
126 public int readSomeBytes(byte[][] values, int param) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException {
127 // System.err.println("readSomebytes()");
128 return readBytes(values, param);
131 public void skipBytes(int param) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException {
132 // System.err.println("skipBytes("+param+")");
133 _check();
134 if (param > (m_length - m_pos))
135 throw new com.sun.star.io.BufferSizeExceededException("buffer overflow");
136 m_pos += param;
139 public long getLength() throws com.sun.star.io.IOException {
140 // System.err.println("getLength() -> "+m_length);
141 if (m_bytes != null) return m_length;
142 else throw new com.sun.star.io.IOException("no bytes");
145 public long getPosition() throws com.sun.star.io.IOException {
146 // System.err.println("getPosition() -> "+m_pos);
147 if (m_bytes != null) return m_pos;
148 else throw new com.sun.star.io.IOException("no bytes");
151 public void seek(long param) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.io.IOException {
152 // System.err.println("seek("+param+")");
153 if (m_bytes != null){
154 if (param < 0 || param > m_length) throw new com.sun.star.lang.IllegalArgumentException("invalid seek position");
155 else m_pos = (int)param;
156 }else throw new com.sun.star.io.IOException("no bytes");
159 public void finalize() throws Throwable{
160 // System.err.println("finalizer called for ByteArrayXInputStream!");
161 super.finalize();