1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ByteArrayToXInputStreamAdapter.java,v $
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
;
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
50 implements XInputStream
, XSeekable
59 /** Creates a new instance of ByteArrayXInputStram */
60 public ByteArrayToXInputStreamAdapter(byte[] bytes
) {
64 public void init(byte[] bytes
) {
65 // System.err.println("ByteArrayXInputStream");
67 m_length
= bytes
.length
;
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");
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
{
85 long a
= m_length
- m_pos
;
87 throw new com
.sun
.star
.io
.IOException("integer overflow");
89 // System.err.println("available() -> "+a);
94 public void closeInput() throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.IOException
{
95 // System.err.println("closeInput()");
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+")");
104 int remain
= (int)(m_length
- m_pos
);
105 if (param
> remain
) param
= remain
;
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);
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());
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+")");
134 if (param
> (m_length
- m_pos
))
135 throw new com
.sun
.star
.io
.BufferSizeExceededException("buffer overflow");
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!");