1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 package com
.sun
.star
.lib
.uno
.adapter
;
22 import java
.io
.IOException
;
24 import com
.sun
.star
.io
.XInputStream
;
26 import java
.io
.InputStream
;
28 /** The <code>InputStreamToInputXStreamAdapter</code> wraps the
29 Java <code>InputStream</code> object into a
30 UNO <code>XInputStream</code> object.
31 This allows users to access an <code>InputStream</code>
32 as if it were an <code>XInputStream</code>.
34 public final class InputStreamToXInputStreamAdapter
implements XInputStream
{
37 * Internal store to the InputStream
39 private final InputStream iIn
;
44 * @param in The <code>XInputStream</code> to be
45 * accessed as an <code>InputStream</code>.
47 public InputStreamToXInputStreamAdapter (InputStream in
)
52 public int available() throws
53 com
.sun
.star
.io
.IOException
59 bytesAvail
= iIn
.available();
60 } catch (IOException e
) {
61 throw new com
.sun
.star
.io
.IOException(e
);
67 public void closeInput() throws
68 com
.sun
.star
.io
.IOException
72 } catch (IOException e
) {
73 throw new com
.sun
.star
.io
.IOException(e
);
77 public int readBytes(byte[][] b
, int len
) throws
78 com
.sun
.star
.io
.IOException
82 int totalBytesRead
= 0;
83 if (b
[0] == null || b
[0].length
< len
) {
87 // Casting bytesRead to an int is okay, since the user can
88 // only pass in an integer length to read, so the bytesRead
90 while ((len
> 0) && ((bytesRead
= iIn
.read(b
[0], totalBytesRead
, len
)) > 0)) {
91 totalBytesRead
+= (int)bytesRead
;
92 len
-= (int)bytesRead
;
94 if (totalBytesRead
< b
[0].length
) {
95 byte[] out
= new byte[totalBytesRead
];
96 System
.arraycopy(b
[0], 0, out
, 0, totalBytesRead
);
99 return totalBytesRead
;
100 } catch (IOException e
) {
101 throw new com
.sun
.star
.io
.IOException("reader error", e
);
105 public int readSomeBytes(byte[][] b
, int len
) throws
106 com
.sun
.star
.io
.IOException
110 if (b
[0] == null || b
[0].length
< len
) {
111 b
[0] = new byte[len
];
113 if (len
>iIn
.available()) {
114 bytesRead
= iIn
.read(b
[0], 0, iIn
.available());
117 bytesRead
= iIn
.read(b
[0], 0, len
);
120 // Casting bytesRead to an int is okay, since the user can
121 // only pass in an integer length to read, so the bytesRead
123 if (bytesRead
< b
[0].length
) {
124 int outSize
= bytesRead
> 0 ?
(int)bytesRead
: 0;
125 byte[] out
= new byte[outSize
];
126 System
.arraycopy(b
[0], 0, out
, 0, outSize
);
129 if (bytesRead
<= 0) {
132 return ((int)bytesRead
);
133 } catch (IOException e
) {
134 throw new com
.sun
.star
.io
.IOException("reader error", e
);
138 public void skipBytes(int n
) throws
139 com
.sun
.star
.io
.IOException
143 } catch (IOException e
) {
144 throw new com
.sun
.star
.io
.IOException(e
);
150 } catch (IOException e
) {
151 throw new com
.sun
.star
.io
.IOException(e
);
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */