2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package com
.sun
.star
.lib
.uno
.adapter
;
20 import com
.sun
.star
.io
.XInputStream
;
21 import com
.sun
.star
.io
.XSeekable
;
22 import com
.sun
.star
.lib
.uno
.helper
.ComponentBase
;
24 public final class ByteArrayToXInputStreamAdapter
26 implements XInputStream
, XSeekable
35 /** Creates a new instance of ByteArrayXInputStram */
36 public ByteArrayToXInputStreamAdapter(byte[] bytes
) {
40 public void init(byte[] bytes
) {
42 m_length
= bytes
.length
;
47 private void _check() throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.IOException
{
48 if (m_bytes
== null) {
49 throw new com
.sun
.star
.io
.NotConnectedException("no bytes");
52 throw new com
.sun
.star
.io
.IOException("input closed");
56 public int available() throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.IOException
{
58 long a
= m_length
- m_pos
;
60 throw new com
.sun
.star
.io
.IOException("integer overflow");
66 public void closeInput() throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.IOException
{
71 public int readBytes(byte[][] values
, int param
) throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.BufferSizeExceededException
, com
.sun
.star
.io
.IOException
{
74 int remain
= (m_length
- m_pos
);
75 if (param
> remain
) param
= remain
;
77 if (values
[0] == null){
78 values
[0] = new byte[param
];
80 System
.arraycopy(m_bytes
, m_pos
, values
[0], 0, param
);
83 } catch (ArrayIndexOutOfBoundsException ex
) {
84 throw new com
.sun
.star
.io
.BufferSizeExceededException(ex
, "buffer overflow");
85 } catch (Exception ex
) {
86 throw new com
.sun
.star
.io
.IOException(ex
, "error accessing buffer");
90 public int readSomeBytes(byte[][] values
, int param
) throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.BufferSizeExceededException
, com
.sun
.star
.io
.IOException
{
91 return readBytes(values
, param
);
94 public void skipBytes(int param
) throws com
.sun
.star
.io
.NotConnectedException
, com
.sun
.star
.io
.BufferSizeExceededException
, com
.sun
.star
.io
.IOException
{
96 if (param
> (m_length
- m_pos
))
97 throw new com
.sun
.star
.io
.BufferSizeExceededException("buffer overflow");
101 public long getLength() throws com
.sun
.star
.io
.IOException
{
102 if (m_bytes
!= null) return m_length
;
103 else throw new com
.sun
.star
.io
.IOException("no bytes");
106 public long getPosition() throws com
.sun
.star
.io
.IOException
{
107 if (m_bytes
!= null) return m_pos
;
108 else throw new com
.sun
.star
.io
.IOException("no bytes");
111 public void seek(long param
) throws com
.sun
.star
.lang
.IllegalArgumentException
, com
.sun
.star
.io
.IOException
{
112 if (m_bytes
!= null){
113 if (param
< 0 || param
> m_length
) throw new com
.sun
.star
.lang
.IllegalArgumentException("invalid seek position");
114 else m_pos
= (int)param
;
115 }else throw new com
.sun
.star
.io
.IOException("no bytes");