summaryrefslogtreecommitdiffstats
path: root/package/qa/storages/BorderedStream.java
blob: ce7ebe55ff4b50c4d725d05905b7e7274be0c29b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package complex.storages;

import com.sun.star.uno.XInterface;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XSingleServiceFactory;

import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XInterface;

import com.sun.star.io.XStream;
import com.sun.star.io.XInputStream;
import com.sun.star.io.XOutputStream;
import com.sun.star.io.XTruncate;
import com.sun.star.io.XSeekable;


public class BorderedStream
    implements XStream, XInputStream, XOutputStream, XTruncate, XSeekable
{
    int m_nMaxSize;
    int m_nCurSize;
    int m_nCurPos;
    byte m_pBytes[];

    public BorderedStream( int nMaxSize )
    {
        m_nMaxSize = nMaxSize;
        m_nCurSize = 0;
        m_nCurPos = 0;
        m_pBytes = new byte[m_nMaxSize];
    }


    // XStream



    public synchronized XInputStream getInputStream()
        throws com.sun.star.uno.RuntimeException
    {
        return (XInputStream)UnoRuntime.queryInterface( XInputStream.class, this );
    }


    public synchronized XOutputStream getOutputStream()
        throws com.sun.star.uno.RuntimeException
    {
        return (XOutputStream)UnoRuntime.queryInterface( XOutputStream.class, this );
    }


    // XInputStream



    public synchronized int readBytes( byte[][] aData, int nBytesToRead )
        throws  com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        int nRead = 0;
        if ( m_pBytes != null && nBytesToRead > 0 )
        {
            int nAvailable = m_nCurSize - m_nCurPos;
            if ( nBytesToRead > nAvailable )
                nBytesToRead = nAvailable;

            aData[0] = new byte[nBytesToRead];
            for ( int nInd = 0; nInd < nBytesToRead; nInd++ )
                aData[0][nInd] = m_pBytes[m_nCurPos+nInd];

            nRead = nBytesToRead;
            m_nCurPos += nRead;
        }
        else
        {
            aData[0] = new byte[0];
        }

        return nRead;
    }


    public synchronized int readSomeBytes( byte[][] aData, int nMaxBytesToRead )
        throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        return readBytes( aData, nMaxBytesToRead );
    }


    public synchronized void skipBytes( int nBytesToSkip  )
        throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        if ( nBytesToSkip < 0 )
            throw new com.sun.star.io.IOException(); // illegal argument

        if ( m_nCurSize - m_nCurPos > nBytesToSkip )
            m_nCurPos += nBytesToSkip;
        else
            m_nCurPos = m_nCurSize;
    }


    public synchronized int available()
        throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        return 0;
    }


    public synchronized void closeInput()
        throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        // no need to do anything
    }



    // XOutputStream



    public synchronized void writeBytes( byte[] aData  )
        throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        if ( m_pBytes != null && aData.length > 0 )
        {
            if ( aData.length > m_nMaxSize - m_nCurPos )
                throw new com.sun.star.io.IOException();

            for ( int nInd = 0; nInd < aData.length; nInd++ )
                m_pBytes[m_nCurPos+nInd] = aData[nInd];

            m_nCurPos += aData.length;
            if ( m_nCurPos > m_nCurSize )
                m_nCurSize = m_nCurPos;
        }
    }


    public synchronized void flush()
        throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        // nothing to do
    }


    public synchronized void closeOutput()
        throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        // nothing to do
    }



    // XTruncate



    public synchronized void truncate()
        throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        m_nCurSize = 0;
        m_nCurPos = 0;
    }



    // XSeekable



    public synchronized void seek( long location )
        throws com.sun.star.lang.IllegalArgumentException, com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        if ( location > (long)m_nCurSize )
            throw new com.sun.star.lang.IllegalArgumentException();

        m_nCurPos = (int)location;
    }


    public synchronized long getPosition()
        throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        return (long)m_nCurPos;
    }


    public synchronized long getLength()
        throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
    {
        return (long)m_nCurSize;
    }
};