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