update credits
[LibreOffice.git] / javaunohelper / com / sun / star / lib / uno / adapter / InputStreamToXInputStreamAdapter.java
blob0053c4dfee702cc74cfa67bda61cace304304e6f
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 java.io.IOException;
22 import com.sun.star.io.XInputStream;
24 import java.io.InputStream;
26 /** The <code>InputStreamToInputXStreamAdapter</code> wraps the
27 Java <code>InputStream</code> object into a
28 UNO <code>XInputStream</code> object.
29 This allows users to access an <code>InputStream</code>
30 as if it were an <code>XInputStream</code>.
32 public final class InputStreamToXInputStreamAdapter implements XInputStream {
34 /**
35 * Internal store to the InputStream
37 private final InputStream iIn;
39 /**
40 * Constructor.
42 * @param in The <code>XInputStream</code> to be
43 * accessed as an <code>InputStream</code>.
45 public InputStreamToXInputStreamAdapter (InputStream in)
47 iIn = in;
50 public int available() throws
51 com.sun.star.io.IOException
54 int bytesAvail;
56 try {
57 bytesAvail = iIn.available();
58 } catch (IOException e) {
59 throw new com.sun.star.io.IOException(e);
62 return bytesAvail;
65 public void closeInput() throws
66 com.sun.star.io.IOException
68 try {
69 iIn.close();
70 } catch (IOException e) {
71 throw new com.sun.star.io.IOException(e);
75 public int readBytes(byte[][] b, int len) throws
76 com.sun.star.io.IOException
78 try {
79 long bytesRead;
80 if (len >iIn.available()) {
81 bytesRead = iIn.read(b[0], 0, iIn.available());
83 else{
84 bytesRead = iIn.read(b[0], 0, len);
86 // Casting bytesRead to an int is okay, since the user can
87 // only pass in an integer length to read, so the bytesRead
88 // must <= len.
90 if (bytesRead <= 0) {
91 return 0;
93 return ((int)bytesRead);
96 } catch (IOException e) {
97 throw new com.sun.star.io.IOException("reader error", e);
101 public int readSomeBytes(byte[][] b, int len) throws
102 com.sun.star.io.IOException
104 try {
105 long bytesRead;
106 if (len >iIn.available()) {
107 bytesRead = iIn.read(b[0], 0, iIn.available());
109 else{
110 bytesRead = iIn.read(b[0], 0, len);
112 // Casting bytesRead to an int is okay, since the user can
113 // only pass in an integer length to read, so the bytesRead
114 // must <= len.
116 if (bytesRead <= 0) {
117 return 0;
119 return ((int)bytesRead);
122 } catch (IOException e) {
123 throw new com.sun.star.io.IOException("reader error", e);
127 public void skipBytes(int n) throws
128 com.sun.star.io.IOException
130 int tmpLongVal = n;
131 int tmpIntVal;
133 try {
134 iIn.available();
135 } catch (IOException e) {
136 throw new com.sun.star.io.IOException(e);
139 do {
140 if (tmpLongVal >= Integer.MAX_VALUE) {
141 tmpIntVal = Integer.MAX_VALUE;
142 } else {
143 // Casting is safe here.
144 tmpIntVal = tmpLongVal;
146 tmpLongVal -= tmpIntVal;
148 try {
149 iIn.skip(tmpIntVal);
150 } catch (IOException e) {
151 throw new com.sun.star.io.IOException(e);
153 } while (tmpLongVal > 0);