| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 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/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 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 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package com.sun.star.script.framework.io; |
| 20 |
|
| 21 |
import com.sun.star.io.XInputStream; |
| 22 |
|
| 23 |
import java.io.IOException; |
| 24 |
import java.io.InputStream; |
| 25 |
|
| 26 |
public class XInputStreamImpl implements XInputStream { |
| 27 |
|
| 28 |
private final InputStream is; |
| 29 |
|
| 30 |
public XInputStreamImpl(InputStream is) { |
| 31 |
this.is = is; |
| 32 |
} |
| 33 |
|
| 34 |
public int readBytes(/*OUT*/byte[][] aData, /*IN*/int nBytesToRead) throws |
| 35 |
com.sun.star.io.NotConnectedException, |
| 36 |
com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException { |
| 37 |
|
| 38 |
aData[ 0 ] = new byte[ nBytesToRead ]; |
| 39 |
int totalBytesRead = 0; |
| 40 |
|
| 41 |
try { |
| 42 |
int bytesRead; |
| 43 |
|
| 44 |
while (( nBytesToRead > 0 ) && (bytesRead = is.read(aData[ 0 ], totalBytesRead, nBytesToRead)) > 0) { |
| 45 |
totalBytesRead += bytesRead; |
| 46 |
nBytesToRead -= bytesRead; |
| 47 |
} |
| 48 |
if ( totalBytesRead < aData[ 0 ].length ) |
| 49 |
{ |
| 50 |
byte[] out = new byte[ totalBytesRead ]; |
| 51 |
System.arraycopy( aData[ 0 ], 0, out, 0, totalBytesRead ); |
| 52 |
aData[ 0 ] = out; |
| 53 |
} |
| 54 |
} catch (IOException e) { |
| 55 |
throw new com.sun.star.io.IOException(e); |
| 56 |
} catch (IndexOutOfBoundsException aie) { |
| 57 |
throw new com.sun.star.io.BufferSizeExceededException(aie); |
| 58 |
} |
| 59 |
|
| 60 |
return totalBytesRead; |
| 61 |
} |
| 62 |
|
| 63 |
public int readSomeBytes(/*OUT*/byte[][] aData, /*IN*/int nMaxBytesToRead) |
| 64 |
throws com.sun.star.io.NotConnectedException, |
| 65 |
com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException { |
| 66 |
|
| 67 |
int bytesToRead = nMaxBytesToRead; |
| 68 |
int availableBytes = available(); |
| 69 |
|
| 70 |
if (0 < availableBytes && availableBytes < nMaxBytesToRead) |
| 71 |
{ |
| 72 |
bytesToRead = availableBytes; |
| 73 |
} |
| 74 |
|
| 75 |
int read = readBytes(aData, bytesToRead); |
| 76 |
return read; |
| 77 |
} |
| 78 |
|
| 79 |
public void skipBytes(/*IN*/int nBytesToSkip) throws |
| 80 |
com.sun.star.io.NotConnectedException, |
| 81 |
com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException { |
| 82 |
|
| 83 |
try { |
| 84 |
do { |
| 85 |
nBytesToSkip -= is.skip(nBytesToSkip); |
| 86 |
} while (nBytesToSkip > 0); |
| 87 |
} catch (IOException e) { |
| 88 |
throw new com.sun.star.io.IOException(e); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public int available() throws |
| 93 |
com.sun.star.io.NotConnectedException, com.sun.star.io.IOException { |
| 94 |
|
| 95 |
int bytesAvail = 0; |
| 96 |
|
| 97 |
try { |
| 98 |
bytesAvail = is.available(); |
| 99 |
} catch (IOException e) { |
| 100 |
throw new com.sun.star.io.IOException(e); |
| 101 |
} |
| 102 |
|
| 103 |
return bytesAvail; |
| 104 |
} |
| 105 |
|
| 106 |
public void closeInput() throws |
| 107 |
com.sun.star.io.NotConnectedException, com.sun.star.io.IOException { |
| 108 |
|
| 109 |
try { |
| 110 |
is.close(); |
| 111 |
} catch (IOException e) { |
| 112 |
throw new com.sun.star.io.IOException(e); |
| 113 |
} |
| 114 |
} |
| 115 |
} |