~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/classpath/ikvm/io/InputStreamWrapper.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2006, 2007 Jeroen Frijters
 
3
 
 
4
  This software is provided 'as-is', without any express or implied
 
5
  warranty.  In no event will the authors be held liable for any damages
 
6
  arising from the use of this software.
 
7
 
 
8
  Permission is granted to anyone to use this software for any purpose,
 
9
  including commercial applications, and to alter it and redistribute it
 
10
  freely, subject to the following restrictions:
 
11
 
 
12
  1. The origin of this software must not be misrepresented; you must not
 
13
     claim that you wrote the original software. If you use this software
 
14
     in a product, an acknowledgment in the product documentation would be
 
15
     appreciated but is not required.
 
16
  2. Altered source versions must be plainly marked as such, and must not be
 
17
     misrepresented as being the original software.
 
18
  3. This notice may not be removed or altered from any source distribution.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
 
 
25
package ikvm.io;
 
26
 
 
27
public final class InputStreamWrapper extends java.io.InputStream
 
28
{
 
29
    private cli.System.IO.Stream stream;
 
30
    private long markPosition = -1;
 
31
    private boolean atEOF;
 
32
 
 
33
    public InputStreamWrapper(cli.System.IO.Stream stream)
 
34
    {
 
35
        this.stream = stream;
 
36
    }
 
37
 
 
38
    public int read() throws java.io.IOException
 
39
    {
 
40
        try
 
41
        {
 
42
            if (false) throw new cli.System.IO.IOException();
 
43
            if (false) throw new cli.System.ObjectDisposedException(null);
 
44
            int i = stream.ReadByte();
 
45
            if (i == -1)
 
46
            {
 
47
                atEOF = true;
 
48
            }
 
49
            return i;
 
50
        }
 
51
        catch (cli.System.IO.IOException x)
 
52
        {
 
53
            java.io.IOException ex = new java.io.IOException();
 
54
            ex.initCause(x);
 
55
            throw ex;
 
56
        }
 
57
        catch (cli.System.ObjectDisposedException x)
 
58
        {
 
59
            java.io.IOException ex = new java.io.IOException();
 
60
            ex.initCause(x);
 
61
            throw ex;
 
62
        }
 
63
    }
 
64
 
 
65
    public int read(byte[] b) throws java.io.IOException
 
66
    {
 
67
        return read(b, 0, b.length);
 
68
    }
 
69
 
 
70
    public int read(byte[] b, int off, int len) throws java.io.IOException
 
71
    {
 
72
        if (b == null)
 
73
        {
 
74
            throw new NullPointerException();
 
75
        }
 
76
        if (off < 0 || len < 0 || b.length - off < len)
 
77
        {
 
78
            throw new IndexOutOfBoundsException();
 
79
        }
 
80
        if (len == 0)
 
81
        {
 
82
            return 0;
 
83
        }
 
84
        try
 
85
        {
 
86
            if (false) throw new cli.System.IO.IOException();
 
87
            if (false) throw new cli.System.ObjectDisposedException(null);
 
88
            int count = stream.Read(b, off, len);
 
89
            if (count == 0)
 
90
            {
 
91
                atEOF = true;
 
92
                return -1;
 
93
            }
 
94
            return count;
 
95
        }
 
96
        catch (cli.System.IO.IOException x)
 
97
        {
 
98
            java.io.IOException ex = new java.io.IOException();
 
99
            ex.initCause(x);
 
100
            throw ex;
 
101
        }
 
102
        catch (cli.System.ObjectDisposedException x)
 
103
        {
 
104
            java.io.IOException ex = new java.io.IOException();
 
105
            ex.initCause(x);
 
106
            throw ex;
 
107
        }
 
108
    }
 
109
 
 
110
    public long skip(long n) throws java.io.IOException
 
111
    {
 
112
        if (n <= 0)
 
113
        {
 
114
            return 0;
 
115
        }
 
116
        else if (stream.get_CanSeek())
 
117
        {
 
118
            try
 
119
            {
 
120
                if (false) throw new cli.System.IO.IOException();
 
121
                if (false) throw new cli.System.ObjectDisposedException(null);
 
122
                long pos = stream.get_Position();
 
123
                n = Math.min(n, Math.max(0, stream.get_Length() - pos));
 
124
                stream.set_Position(pos + n);
 
125
                return n;
 
126
            }
 
127
            catch (cli.System.IO.IOException x)
 
128
            {
 
129
                java.io.IOException ex = new java.io.IOException();
 
130
                ex.initCause(x);
 
131
                throw ex;
 
132
            }
 
133
            catch (cli.System.ObjectDisposedException x)
 
134
            {
 
135
                java.io.IOException ex = new java.io.IOException();
 
136
                ex.initCause(x);
 
137
                throw ex;
 
138
            }
 
139
        }
 
140
        else
 
141
        {
 
142
            return super.skip(n);
 
143
        }
 
144
    }
 
145
 
 
146
    public int available() throws java.io.IOException
 
147
    {
 
148
        if (stream.get_CanSeek())
 
149
        {
 
150
            try
 
151
            {
 
152
                if (false) throw new cli.System.IO.IOException();
 
153
                if (false) throw new cli.System.ObjectDisposedException(null);
 
154
                long val = stream.get_Length() - stream.get_Position();
 
155
                return (int)Math.min(Math.max(val, 0), Integer.MAX_VALUE);
 
156
            }
 
157
            catch (cli.System.IO.IOException x)
 
158
            {
 
159
                java.io.IOException ex = new java.io.IOException();
 
160
                ex.initCause(x);
 
161
                throw ex;
 
162
            }
 
163
            catch (cli.System.ObjectDisposedException x)
 
164
            {
 
165
                java.io.IOException ex = new java.io.IOException();
 
166
                ex.initCause(x);
 
167
                throw ex;
 
168
            }
 
169
        }
 
170
        else
 
171
        {
 
172
            // It turns out that it's important that available() return non-zero when
 
173
            // data is still available, because BufferedInputStream uses the non-zero
 
174
            // return value as a cue to continue reading.
 
175
            // As suggested by Mark Reinhold, we emulate InflaterInputStream's behavior
 
176
            // and return 0 after we've reached EOF and otherwise 1.
 
177
            return atEOF ? 0 : 1;
 
178
        }
 
179
    }
 
180
 
 
181
    public void close() throws java.io.IOException
 
182
    {
 
183
        stream.Close();
 
184
    }
 
185
 
 
186
    public void mark(int readlimit)
 
187
    {
 
188
        if (stream.get_CanSeek())
 
189
        {
 
190
            try
 
191
            {
 
192
                if (false) throw new cli.System.IO.IOException();
 
193
                if (false) throw new cli.System.ObjectDisposedException(null);
 
194
                markPosition = stream.get_Position();
 
195
            }
 
196
            catch (cli.System.IO.IOException x)
 
197
            {
 
198
            }
 
199
            catch (cli.System.ObjectDisposedException x)
 
200
            {
 
201
            }
 
202
        }
 
203
    }
 
204
 
 
205
    public void reset() throws java.io.IOException
 
206
    {
 
207
        if (!stream.get_CanSeek())
 
208
        {
 
209
            throw new java.io.IOException("mark/reset not supported");
 
210
        }
 
211
        if (markPosition == -1)
 
212
        {
 
213
            throw new java.io.IOException("no mark available");
 
214
        }
 
215
        try
 
216
        {
 
217
            if (false) throw new cli.System.IO.IOException();
 
218
            if (false) throw new cli.System.ObjectDisposedException(null);
 
219
            stream.set_Position(markPosition);
 
220
        }
 
221
        catch (cli.System.IO.IOException x)
 
222
        {
 
223
            java.io.IOException ex = new java.io.IOException();
 
224
            ex.initCause(x);
 
225
            throw ex;
 
226
        }
 
227
        catch (cli.System.ObjectDisposedException x)
 
228
        {
 
229
            java.io.IOException ex = new java.io.IOException();
 
230
            ex.initCause(x);
 
231
            throw ex;
 
232
        }
 
233
    }
 
234
 
 
235
    public boolean markSupported()
 
236
    {
 
237
        return stream.get_CanSeek();
 
238
    }
 
239
}