tdf#153192 support read protection in RTF documents
[LibreOffice.git] / ridljar / com / sun / star / lib / uno / adapter / InputStreamToXInputStreamAdapter.java
blobdd634e7713700bea8f0c9240475dad077fda38d7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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 {
36 /**
37 * Internal store to the InputStream
39 private final InputStream iIn;
41 /**
42 * Constructor.
44 * @param in The <code>XInputStream</code> to be
45 * accessed as an <code>InputStream</code>.
47 public InputStreamToXInputStreamAdapter (InputStream in)
49 iIn = in;
52 public int available() throws
53 com.sun.star.io.IOException
56 int bytesAvail;
58 try {
59 bytesAvail = iIn.available();
60 } catch (IOException e) {
61 throw new com.sun.star.io.IOException(e);
64 return bytesAvail;
67 public void closeInput() throws
68 com.sun.star.io.IOException
70 try {
71 iIn.close();
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
80 try {
81 long bytesRead;
82 int totalBytesRead = 0;
83 if (b[0] == null || b[0].length < len) {
84 b[0] = new byte[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
89 // must <= len.
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);
97 b[0] = out;
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
108 try {
109 long bytesRead;
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());
116 else{
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
122 // must <= len.
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);
127 b[0] = out;
129 if (bytesRead <= 0) {
130 return 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
141 try {
142 iIn.available();
143 } catch (IOException e) {
144 throw new com.sun.star.io.IOException(e);
147 do {
148 try {
149 n -= iIn.skip(n);
150 } catch (IOException e) {
151 throw new com.sun.star.io.IOException(e);
153 } while (n > 0);
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */