1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.script.framework.io;
25 
26 import com.sun.star.io.XInputStream;
27 import java.io.InputStream;
28 import java.io.IOException;
29 
30 public class XInputStreamImpl implements XInputStream
31 {
32     InputStream is;
XInputStreamImpl( InputStream is )33     public XInputStreamImpl( InputStream is )
34     {
35         this.is = is;
36     }
37 
readBytes( byte[][] aData, int nBytesToRead )38     public int readBytes( /*OUT*/byte[][] aData, /*IN*/int nBytesToRead ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException
39     {
40         aData[ 0 ] = new byte[ nBytesToRead ];
41 
42         int totalBytesRead = 0;
43 
44         try
45         {
46             int bytesRead = 0;
47             while ( ( bytesRead = is.read( aData[ 0 ], totalBytesRead, nBytesToRead ) ) > 0 && ( totalBytesRead < nBytesToRead ) )
48             {
49                 totalBytesRead += bytesRead;
50                 nBytesToRead -= bytesRead;
51             }
52         }
53         catch ( IOException e )
54         {
55             throw new com.sun.star.io.IOException( e.toString() );
56         }
57         catch ( IndexOutOfBoundsException aie )
58         {
59             throw new com.sun.star.io.BufferSizeExceededException( aie.toString() );
60         }
61         return totalBytesRead;
62     }
63 
readSomeBytes( byte[][] aData, int nMaxBytesToRead )64     public int readSomeBytes( /*OUT*/byte[][] aData, /*IN*/int nMaxBytesToRead ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException
65     {
66         int bytesToRead = nMaxBytesToRead;
67         int availableBytes = available();
68         if ( availableBytes < nMaxBytesToRead )
69         {
70             bytesToRead = availableBytes;
71         }
72         int read =  readBytes( aData, bytesToRead );
73         return read;
74     }
75 
skipBytes( int nBytesToSkip )76     public void skipBytes( /*IN*/int nBytesToSkip ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException
77     {
78         long bytesSkipped = 0;
79         try
80         {
81             bytesSkipped = is.skip( (long)nBytesToSkip );
82         }
83         catch ( IOException e )
84         {
85             throw new com.sun.star.io.IOException( e.toString() );
86         }
87     }
88 
available( )89     public int available(  ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException
90     {
91         int bytesAvail = 0;
92         try
93         {
94             bytesAvail = is.available();
95         }
96         catch ( IOException e )
97         {
98             throw new com.sun.star.io.IOException( e.toString() );
99         }
100         return bytesAvail;
101     }
102 
closeInput( )103     public void closeInput(  ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException
104     {
105         try
106         {
107             is.close();
108         }
109         catch( IOException e )
110         {
111             throw new com.sun.star.io.IOException( e.toString() );
112         }
113     }
114 
115 }
116