update credits
[LibreOffice.git] / javaunohelper / com / sun / star / lib / uno / adapter / ByteArrayToXInputStreamAdapter.java
blob2d3e9a8480b77cccbd3b9139e4aad428566d32a9
1 /*
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
25 extends ComponentBase
26 implements XInputStream, XSeekable
29 byte[] m_bytes;
30 int m_length;
31 int m_pos;
33 boolean m_open;
35 /** Creates a new instance of ByteArrayXInputStram */
36 public ByteArrayToXInputStreamAdapter(byte[] bytes) {
37 init(bytes);
40 public void init(byte[] bytes) {
41 m_bytes = bytes;
42 m_length = bytes.length;
43 m_pos = 0;
44 m_open = true;
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");
51 if(!m_open) {
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 {
57 _check();
58 long a = m_length - m_pos;
59 if (a != (int)a)
60 throw new com.sun.star.io.IOException("integer overflow");
61 else {
62 return (int)a;
66 public void closeInput() throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException {
67 _check();
68 m_open = false;
71 public int readBytes(byte[][] values, int param) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException {
72 _check();
73 try {
74 int remain = (m_length - m_pos);
75 if (param > remain) param = remain;
76 /* ARGH!!! */
77 if (values[0] == null){
78 values[0] = new byte[param];
80 System.arraycopy(m_bytes, m_pos, values[0], 0, param);
81 m_pos += param;
82 return 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 {
95 _check();
96 if (param > (m_length - m_pos))
97 throw new com.sun.star.io.BufferSizeExceededException("buffer overflow");
98 m_pos += param;
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");