1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 package com
.sun
.star
.lib
.uno
.adapter
;
29 import java
.io
.IOException
;
30 import com
.sun
.star
.io
.XInputStream
;
31 import java
.io
.InputStream
;
33 /** The <code>InputStreamToInputXStreamAdapter</code> wraps the
34 Java <code>InputStream</code> object into a
35 UNO <code>XInputStream</code> object.
36 This allows users to access an <code>InputStream</code>
37 as if it were an <code>XInputStream</code>.
39 public class InputStreamToXInputStreamAdapter
implements XInputStream
{
42 * Internal store to the InputStream
44 private InputStream iIn
;
49 * @param in The <code>XInputStream</code> to be
50 * accessed as an <code>InputStream</code>.
52 public InputStreamToXInputStreamAdapter (InputStream in
)
57 public int available() throws
58 com
.sun
.star
.io
.IOException
64 bytesAvail
= iIn
.available();
65 } catch (IOException e
) {
66 throw new com
.sun
.star
.io
.IOException(e
.toString());
72 public void closeInput() throws
73 com
.sun
.star
.io
.IOException
77 } catch (IOException e
) {
78 throw new com
.sun
.star
.io
.IOException(e
.toString());
82 public int readBytes(byte[][] b
, int len
) throws
83 com
.sun
.star
.io
.IOException
88 if (len
>iIn
.available()) {
89 bytesRead
= iIn
.read(b
[0], 0, iIn
.available());
92 bytesRead
= iIn
.read(b
[0], 0, len
);
94 // Casting bytesRead to an int is okay, since the user can
95 // only pass in an integer length to read, so the bytesRead
101 return ((int)bytesRead
);
104 } catch (IOException e
) {
105 throw new com
.sun
.star
.io
.IOException("reader error: "+e
.toString());
109 public int readSomeBytes(byte[][] b
, int len
) throws
110 com
.sun
.star
.io
.IOException
115 if (len
>iIn
.available()) {
116 bytesRead
= iIn
.read(b
[0], 0, iIn
.available());
119 bytesRead
= iIn
.read(b
[0], 0, len
);
121 // Casting bytesRead to an int is okay, since the user can
122 // only pass in an integer length to read, so the bytesRead
125 if (bytesRead
<= 0) {
128 return ((int)bytesRead
);
131 } catch (IOException e
) {
132 throw new com
.sun
.star
.io
.IOException("reader error: "+e
.toString());
136 public void skipBytes(int n
) throws
137 com
.sun
.star
.io
.IOException
144 avail
= iIn
.available();
145 } catch (IOException e
) {
146 throw new com
.sun
.star
.io
.IOException(e
.toString());
150 if (tmpLongVal
>= Integer
.MAX_VALUE
) {
151 tmpIntVal
= Integer
.MAX_VALUE
;
153 // Casting is safe here.
154 tmpIntVal
= (int)tmpLongVal
;
156 tmpLongVal
-= tmpIntVal
;
160 } catch (IOException e
) {
161 throw new com
.sun
.star
.io
.IOException(e
.toString());
163 } while (tmpLongVal
> 0);