1*a5b190bfSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a5b190bfSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a5b190bfSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a5b190bfSAndrew Rist  * distributed with this work for additional information
6*a5b190bfSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a5b190bfSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a5b190bfSAndrew Rist  * "License"); you may not use this file except in compliance
9*a5b190bfSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a5b190bfSAndrew Rist  *
11*a5b190bfSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a5b190bfSAndrew Rist  *
13*a5b190bfSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a5b190bfSAndrew Rist  * software distributed under the License is distributed on an
15*a5b190bfSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a5b190bfSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a5b190bfSAndrew Rist  * specific language governing permissions and limitations
18*a5b190bfSAndrew Rist  * under the License.
19*a5b190bfSAndrew Rist  *
20*a5b190bfSAndrew Rist  *************************************************************/
21*a5b190bfSAndrew Rist 
22*a5b190bfSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir /*
25cdf0e10cSrcweir  * ByteArrayXInputStram.java
26cdf0e10cSrcweir  *
27cdf0e10cSrcweir  * Created on 10. April 2003, 15:45
28cdf0e10cSrcweir  */
29cdf0e10cSrcweir 
30cdf0e10cSrcweir package com.sun.star.lib.uno.adapter;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir /**
33cdf0e10cSrcweir  *
34cdf0e10cSrcweir  * @author  lo119109
35cdf0e10cSrcweir  */
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import com.sun.star.io.XInputStream;
38cdf0e10cSrcweir import com.sun.star.io.XSeekable;
39cdf0e10cSrcweir import  com.sun.star.lib.uno.helper.ComponentBase;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir public class ByteArrayToXInputStreamAdapter
42cdf0e10cSrcweir 	extends ComponentBase
43cdf0e10cSrcweir     implements XInputStream, XSeekable
44cdf0e10cSrcweir {
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     byte[] m_bytes;
47cdf0e10cSrcweir     int m_length;
48cdf0e10cSrcweir     int m_pos;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     boolean m_open;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     /** Creates a new instance of ByteArrayXInputStram */
ByteArrayToXInputStreamAdapter(byte[] bytes)53cdf0e10cSrcweir     public ByteArrayToXInputStreamAdapter(byte[] bytes) {
54cdf0e10cSrcweir         init(bytes);
55cdf0e10cSrcweir     }
56cdf0e10cSrcweir 
init(byte[] bytes)57cdf0e10cSrcweir     public void init(byte[] bytes) {
58cdf0e10cSrcweir         // System.err.println("ByteArrayXInputStream");
59cdf0e10cSrcweir         m_bytes = bytes;
60cdf0e10cSrcweir         m_length = bytes.length;
61cdf0e10cSrcweir         m_pos = 0;
62cdf0e10cSrcweir         m_open = true;
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
_check()65cdf0e10cSrcweir     private void _check() throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException {
66cdf0e10cSrcweir         if (m_bytes == null) {
67cdf0e10cSrcweir             // System.err.println("check failed no bytes!");
68cdf0e10cSrcweir             throw new com.sun.star.io.NotConnectedException("no bytes");
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir         if(!m_open) {
71cdf0e10cSrcweir             // System.err.println("check failed: closed");
72cdf0e10cSrcweir             throw new com.sun.star.io.IOException("input closed");
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir 
available()76cdf0e10cSrcweir     public int available() throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException {
77cdf0e10cSrcweir         _check();
78cdf0e10cSrcweir         long a = m_length - m_pos;
79cdf0e10cSrcweir         if (a != (int)a)
80cdf0e10cSrcweir             throw new com.sun.star.io.IOException("integer overflow");
81cdf0e10cSrcweir         else {
82cdf0e10cSrcweir             // System.err.println("available() -> "+a);
83cdf0e10cSrcweir             return (int)a;
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir     }
86cdf0e10cSrcweir 
closeInput()87cdf0e10cSrcweir     public void closeInput() throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException {
88cdf0e10cSrcweir         // System.err.println("closeInput()");
89cdf0e10cSrcweir         _check();
90cdf0e10cSrcweir         m_open = false;
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
readBytes(byte[][] values, int param)93cdf0e10cSrcweir     public int readBytes(byte[][] values, int param) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException {
94cdf0e10cSrcweir 		// System.err.println("readbytes(..., "+param+")");
95cdf0e10cSrcweir         _check();
96cdf0e10cSrcweir 	try {
97cdf0e10cSrcweir             int remain = (int)(m_length - m_pos);
98cdf0e10cSrcweir             if (param > remain) param = remain;
99cdf0e10cSrcweir             /* ARGH!!! */
100cdf0e10cSrcweir             if (values[0] == null){
101cdf0e10cSrcweir                 values[0] = new byte[param];
102cdf0e10cSrcweir                 // System.err.println("allocated new buffer of "+param+" bytes");
103cdf0e10cSrcweir             }
104cdf0e10cSrcweir             System.arraycopy(m_bytes, m_pos, values[0], 0, param);
105cdf0e10cSrcweir             // System.err.println("readbytes() -> "+param);
106cdf0e10cSrcweir 			m_pos += param;
107cdf0e10cSrcweir             return param;
108cdf0e10cSrcweir         } catch (ArrayIndexOutOfBoundsException ae) {
109cdf0e10cSrcweir             // System.err.println("readbytes() -> ArrayIndexOutOfBounds");
110cdf0e10cSrcweir             ae.printStackTrace();
111cdf0e10cSrcweir             throw new com.sun.star.io.BufferSizeExceededException("buffer overflow");
112cdf0e10cSrcweir         } catch (Exception e) {
113cdf0e10cSrcweir             // System.err.println("readbytes() -> Exception: "+e.getMessage());
114cdf0e10cSrcweir             e.printStackTrace();
115cdf0e10cSrcweir             throw new com.sun.star.io.IOException("error accessing buffer");
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
readSomeBytes(byte[][] values, int param)119cdf0e10cSrcweir     public int readSomeBytes(byte[][] values, int param) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException {
120cdf0e10cSrcweir 		// System.err.println("readSomebytes()");
121cdf0e10cSrcweir         return readBytes(values, param);
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir 
skipBytes(int param)124cdf0e10cSrcweir     public void skipBytes(int param) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException {
125cdf0e10cSrcweir 		// System.err.println("skipBytes("+param+")");
126cdf0e10cSrcweir         _check();
127cdf0e10cSrcweir         if (param > (m_length - m_pos))
128cdf0e10cSrcweir             throw new com.sun.star.io.BufferSizeExceededException("buffer overflow");
129cdf0e10cSrcweir         m_pos += param;
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
getLength()132cdf0e10cSrcweir     public long getLength() throws com.sun.star.io.IOException {
133cdf0e10cSrcweir         // System.err.println("getLength() -> "+m_length);
134cdf0e10cSrcweir         if (m_bytes != null) return m_length;
135cdf0e10cSrcweir         else throw new com.sun.star.io.IOException("no bytes");
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
getPosition()138cdf0e10cSrcweir     public long getPosition() throws com.sun.star.io.IOException {
139cdf0e10cSrcweir         // System.err.println("getPosition() -> "+m_pos);
140cdf0e10cSrcweir         if (m_bytes != null) return m_pos;
141cdf0e10cSrcweir         else throw new com.sun.star.io.IOException("no bytes");
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
seek(long param)144cdf0e10cSrcweir     public void seek(long param) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.io.IOException {
145cdf0e10cSrcweir 		// System.err.println("seek("+param+")");
146cdf0e10cSrcweir         if (m_bytes != null){
147cdf0e10cSrcweir             if (param < 0 || param > m_length) throw new com.sun.star.lang.IllegalArgumentException("invalid seek position");
148cdf0e10cSrcweir             else m_pos = (int)param;
149cdf0e10cSrcweir         }else throw new com.sun.star.io.IOException("no bytes");
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
finalize()152cdf0e10cSrcweir     public void finalize() throws Throwable{
153cdf0e10cSrcweir         // System.err.println("finalizer called for ByteArrayXInputStream!");
154cdf0e10cSrcweir         super.finalize();
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir }
158