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
{
35 * Internal store to the InputStream
37 private final InputStream iIn
;
42 * @param in The <code>XInputStream</code> to be
43 * accessed as an <code>InputStream</code>.
45 public InputStreamToXInputStreamAdapter (InputStream in
)
50 public int available() throws
51 com
.sun
.star
.io
.IOException
57 bytesAvail
= iIn
.available();
58 } catch (IOException e
) {
59 throw new com
.sun
.star
.io
.IOException(e
);
65 public void closeInput() throws
66 com
.sun
.star
.io
.IOException
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
80 if (len
>iIn
.available()) {
81 bytesRead
= iIn
.read(b
[0], 0, iIn
.available());
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
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
106 if (len
>iIn
.available()) {
107 bytesRead
= iIn
.read(b
[0], 0, iIn
.available());
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
116 if (bytesRead
<= 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
135 } catch (IOException e
) {
136 throw new com
.sun
.star
.io
.IOException(e
);
140 if (tmpLongVal
>= Integer
.MAX_VALUE
) {
141 tmpIntVal
= Integer
.MAX_VALUE
;
143 // Casting is safe here.
144 tmpIntVal
= tmpLongVal
;
146 tmpLongVal
-= tmpIntVal
;
150 } catch (IOException e
) {
151 throw new com
.sun
.star
.io
.IOException(e
);
153 } while (tmpLongVal
> 0);