xref: /trunk/main/javaunohelper/com/sun/star/lib/uno/adapter/ByteArrayToXInputStreamAdapter.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 /*
29  * ByteArrayXInputStram.java
30  *
31  * Created on 10. April 2003, 15:45
32  */
33 
34 package com.sun.star.lib.uno.adapter;
35 
36 /**
37  *
38  * @author  lo119109
39  */
40 
41 import com.sun.star.io.XInputStream;
42 import com.sun.star.io.XSeekable;
43 import  com.sun.star.lib.uno.helper.ComponentBase;
44 
45 public class ByteArrayToXInputStreamAdapter
46     extends ComponentBase
47     implements XInputStream, XSeekable
48 {
49 
50     byte[] m_bytes;
51     int m_length;
52     int m_pos;
53 
54     boolean m_open;
55 
56     /** Creates a new instance of ByteArrayXInputStram */
57     public ByteArrayToXInputStreamAdapter(byte[] bytes) {
58         init(bytes);
59     }
60 
61     public void init(byte[] bytes) {
62         // System.err.println("ByteArrayXInputStream");
63         m_bytes = bytes;
64         m_length = bytes.length;
65         m_pos = 0;
66         m_open = true;
67     }
68 
69     private void _check() throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException {
70         if (m_bytes == null) {
71             // System.err.println("check failed no bytes!");
72             throw new com.sun.star.io.NotConnectedException("no bytes");
73         }
74         if(!m_open) {
75             // System.err.println("check failed: closed");
76             throw new com.sun.star.io.IOException("input closed");
77         }
78     }
79 
80     public int available() throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException {
81         _check();
82         long a = m_length - m_pos;
83         if (a != (int)a)
84             throw new com.sun.star.io.IOException("integer overflow");
85         else {
86             // System.err.println("available() -> "+a);
87             return (int)a;
88         }
89     }
90 
91     public void closeInput() throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException {
92         // System.err.println("closeInput()");
93         _check();
94         m_open = false;
95     }
96 
97     public int readBytes(byte[][] values, int param) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException {
98         // System.err.println("readbytes(..., "+param+")");
99         _check();
100     try {
101             int remain = (int)(m_length - m_pos);
102             if (param > remain) param = remain;
103             /* ARGH!!! */
104             if (values[0] == null){
105                 values[0] = new byte[param];
106                 // System.err.println("allocated new buffer of "+param+" bytes");
107             }
108             System.arraycopy(m_bytes, m_pos, values[0], 0, param);
109             // System.err.println("readbytes() -> "+param);
110             m_pos += param;
111             return param;
112         } catch (ArrayIndexOutOfBoundsException ae) {
113             // System.err.println("readbytes() -> ArrayIndexOutOfBounds");
114             ae.printStackTrace();
115             throw new com.sun.star.io.BufferSizeExceededException("buffer overflow");
116         } catch (Exception e) {
117             // System.err.println("readbytes() -> Exception: "+e.getMessage());
118             e.printStackTrace();
119             throw new com.sun.star.io.IOException("error accessing buffer");
120         }
121     }
122 
123     public int readSomeBytes(byte[][] values, int param) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException {
124         // System.err.println("readSomebytes()");
125         return readBytes(values, param);
126     }
127 
128     public void skipBytes(int param) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException {
129         // System.err.println("skipBytes("+param+")");
130         _check();
131         if (param > (m_length - m_pos))
132             throw new com.sun.star.io.BufferSizeExceededException("buffer overflow");
133         m_pos += param;
134     }
135 
136     public long getLength() throws com.sun.star.io.IOException {
137         // System.err.println("getLength() -> "+m_length);
138         if (m_bytes != null) return m_length;
139         else throw new com.sun.star.io.IOException("no bytes");
140     }
141 
142     public long getPosition() throws com.sun.star.io.IOException {
143         // System.err.println("getPosition() -> "+m_pos);
144         if (m_bytes != null) return m_pos;
145         else throw new com.sun.star.io.IOException("no bytes");
146     }
147 
148     public void seek(long param) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.io.IOException {
149         // System.err.println("seek("+param+")");
150         if (m_bytes != null){
151             if (param < 0 || param > m_length) throw new com.sun.star.lang.IllegalArgumentException("invalid seek position");
152             else m_pos = (int)param;
153         }else throw new com.sun.star.io.IOException("no bytes");
154     }
155 
156     public void finalize() throws Throwable{
157         // System.err.println("finalizer called for ByteArrayXInputStream!");
158         super.finalize();
159     }
160 
161 }
162