Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / javaunohelper / com / sun / star / lib / uno / adapter / InputStreamToXInputStreamAdapter.java
blob59ed553b327255c23ab83f4fe9b90579b9fc9d21
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 {
41 /**
42 * Internal store to the InputStream
44 private InputStream iIn;
46 /**
47 * Constructor.
49 * @param in The <code>XInputStream</code> to be
50 * accessed as an <code>InputStream</code>.
52 public InputStreamToXInputStreamAdapter (InputStream in)
54 iIn = in;
57 public int available() throws
58 com.sun.star.io.IOException
61 int bytesAvail;
63 try {
64 bytesAvail = iIn.available();
65 } catch (IOException e) {
66 throw new com.sun.star.io.IOException(e.toString());
69 return(bytesAvail);
72 public void closeInput() throws
73 com.sun.star.io.IOException
75 try {
76 iIn.close();
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
85 int count = 0;
86 try {
87 long bytesRead=0;
88 if (len >iIn.available()) {
89 bytesRead = iIn.read(b[0], 0, iIn.available());
91 else{
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
96 // must <= len.
98 if (bytesRead <= 0) {
99 return(0);
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
112 int count = 0;
113 try {
114 long bytesRead=0;
115 if (len >iIn.available()) {
116 bytesRead = iIn.read(b[0], 0, iIn.available());
118 else{
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
123 // must <= len.
125 if (bytesRead <= 0) {
126 return(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
139 int avail;
140 int tmpLongVal = n;
141 int tmpIntVal;
143 try {
144 avail = iIn.available();
145 } catch (IOException e) {
146 throw new com.sun.star.io.IOException(e.toString());
149 do {
150 if (tmpLongVal >= Integer.MAX_VALUE) {
151 tmpIntVal = Integer.MAX_VALUE;
152 } else {
153 // Casting is safe here.
154 tmpIntVal = (int)tmpLongVal;
156 tmpLongVal -= tmpIntVal;
158 try {
159 iIn.skip(tmpIntVal);
160 } catch (IOException e) {
161 throw new com.sun.star.io.IOException(e.toString());
163 } while (tmpLongVal > 0);