KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jdbc > remote > SerializableInputStream


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.jdbc.remote;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 /**
32  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
33  * @version $Revision: 37459 $
34  */

35 public class SerializableInputStream extends InputStream JavaDoc implements Serializable JavaDoc
36 {
37    /** @since 1.2 */
38    static final long serialVersionUID = 3364193722688048342L;
39    
40    private byte[] data = null;
41
42    protected byte buf[];
43    protected int pos;
44    protected int mark = 0;
45    protected int count;
46
47
48    public SerializableInputStream(InputStream JavaDoc ins) throws IOException JavaDoc
49    {
50       List JavaDoc byteList = new ArrayList JavaDoc();
51       int dat = ins.read();
52       while (dat != -1)
53       {
54          byteList.add(new Byte JavaDoc((byte)dat));
55          dat = ins.read();
56       }
57
58       data = new byte[byteList.size()];
59       int counter = 0;
60       Iterator JavaDoc itr = byteList.iterator();
61       while(itr.hasNext())
62       {
63          data[counter++] = ((Byte JavaDoc)itr.next()).byteValue();
64       }
65       ins.close();
66       this.buf = this.data;
67       this.pos = 0;
68       this.count = this.buf.length;
69
70    }
71
72    /**
73     * Returns the number of bytes that can be read (or skipped over) from
74     * this input stream without blocking by the next caller of a method for
75     * this input stream. The next caller might be the same thread or or
76     * another thread.
77     * <p/>
78     * <p> The <code>available</code> method for class <code>InputStream</code>
79     * always returns <code>0</code>.
80     * <p/>
81     * <p> This method should be overridden by subclasses.
82     *
83     * @return the number of bytes that can be read from this input stream
84     * without blocking.
85     */

86    public synchronized int available()
87    {
88       return count - pos;
89    }
90
91    /**
92     * Closes this input stream and releases any system resources associated
93     * with the stream.
94     * <p/>
95     * <p> The <code>close</code> method of <code>InputStream</code> does
96     * nothing.
97     *
98     * @throws java.io.IOException if an I/O error occurs.
99     */

100    public void close() throws IOException JavaDoc
101    {
102       System.err.println("close()");
103    }
104
105    /**
106     * Repositions this stream to the position at the time the
107     * <code>mark</code> method was last called on this input stream.
108     * <p/>
109     * <p> The general contract of <code>reset</code> is:
110     * <p/>
111     * <p><ul>
112     * <p/>
113     * <li> If the method <code>markSupported</code> returns
114     * <code>true</code>, then:
115     * <p/>
116     * <ul><li> If the method <code>mark</code> has not been called since
117     * the stream was created, or the number of bytes read from the stream
118     * since <code>mark</code> was last called is larger than the argument
119     * to <code>mark</code> at that last call, then an
120     * <code>IOException</code> might be thrown.
121     * <p/>
122     * <li> If such an <code>IOException</code> is not thrown, then the
123     * stream is reset to a state such that all the bytes read since the
124     * most recent call to <code>mark</code> (or since the start of the
125     * file, if <code>mark</code> has not been called) will be resupplied
126     * to subsequent callers of the <code>read</code> method, followed by
127     * any bytes that otherwise would have been the next input data as of
128     * the time of the call to <code>reset</code>. </ul>
129     * <p/>
130     * <li> If the method <code>markSupported</code> returns
131     * <code>false</code>, then:
132     * <p/>
133     * <ul><li> The call to <code>reset</code> may throw an
134     * <code>IOException</code>.
135     * <p/>
136     * <li> If an <code>IOException</code> is not thrown, then the stream
137     * is reset to a fixed state that depends on the particular type of the
138     * input stream and how it was created. The bytes that will be supplied
139     * to subsequent callers of the <code>read</code> method depend on the
140     * particular type of the input stream. </ul></ul>
141     * <p/>
142     * <p> The method <code>reset</code> for class <code>InputStream</code>
143     * does nothing and always throws an <code>IOException</code>.
144     *
145     * @throws java.io.IOException if this stream has not been marked or if the
146     * mark has been invalidated.
147     * @see java.io.InputStream#mark(int)
148     * @see java.io.IOException
149     */

150    public synchronized void reset() throws IOException JavaDoc
151    {
152       System.err.println("reset()");
153    }
154
155    /**
156     * Tests if this input stream supports the <code>mark</code> and
157     * <code>reset</code> methods. Whether or not <code>mark</code> and
158     * <code>reset</code> are supported is an invariant property of a
159     * particular input stream instance. The <code>markSupported</code> method
160     * of <code>InputStream</code> returns <code>false</code>.
161     *
162     * @return <code>true</code> if this stream instance supports the mark
163     * and reset methods; <code>false</code> otherwise.
164     * @see java.io.InputStream#mark(int)
165     * @see java.io.InputStream#reset()
166     */

167    public boolean markSupported()
168    {
169       System.err.println("markSupported()");
170       return false;
171    }
172
173    /**
174     * Marks the current position in this input stream. A subsequent call to
175     * the <code>reset</code> method repositions this stream at the last marked
176     * position so that subsequent reads re-read the same bytes.
177     * <p/>
178     * <p> The <code>readlimit</code> arguments tells this input stream to
179     * allow that many bytes to be read before the mark position gets
180     * invalidated.
181     * <p/>
182     * <p> The general contract of <code>mark</code> is that, if the method
183     * <code>markSupported</code> returns <code>true</code>, the stream somehow
184     * remembers all the bytes read after the call to <code>mark</code> and
185     * stands ready to supply those same bytes again if and whenever the method
186     * <code>reset</code> is called. However, the stream is not required to
187     * remember any data at all if more than <code>readlimit</code> bytes are
188     * read from the stream before <code>reset</code> is called.
189     * <p/>
190     * <p> The <code>mark</code> method of <code>InputStream</code> does
191     * nothing.
192     *
193     * @param readlimit the maximum limit of bytes that can be read before
194     * the mark position becomes invalid.
195     * @see java.io.InputStream#reset()
196     */

197    public synchronized void mark(int readlimit)
198    {
199       System.err.println("mark(int readlimit)");
200    }
201
202    /**
203     * Skips over and discards <code>n</code> bytes of data from this input
204     * stream. The <code>skip</code> method may, for a variety of reasons, end
205     * up skipping over some smaller number of bytes, possibly <code>0</code>.
206     * This may result from any of a number of conditions; reaching end of file
207     * before <code>n</code> bytes have been skipped is only one possibility.
208     * The actual number of bytes skipped is returned. If <code>n</code> is
209     * negative, no bytes are skipped.
210     * <p/>
211     * <p> The <code>skip</code> method of <code>InputStream</code> creates a
212     * byte array and then repeatedly reads into it until <code>n</code> bytes
213     * have been read or the end of the stream has been reached. Subclasses are
214     * encouraged to provide a more efficient implementation of this method.
215     *
216     * @param n the number of bytes to be skipped.
217     * @return the actual number of bytes skipped.
218     * @throws java.io.IOException if an I/O error occurs.
219     */

220    public long skip(long n) throws IOException JavaDoc
221    {
222       System.err.println("skip(long n)");
223       return 0;
224    }
225
226    /**
227     * Reads some number of bytes from the input stream and stores them into
228     * the buffer array <code>b</code>. The number of bytes actually read is
229     * returned as an integer. This method blocks until input data is
230     * available, end of file is detected, or an exception is thrown.
231     * <p/>
232     * <p> If <code>b</code> is <code>null</code>, a
233     * <code>NullPointerException</code> is thrown. If the length of
234     * <code>b</code> is zero, then no bytes are read and <code>0</code> is
235     * returned; otherwise, there is an attempt to read at least one byte. If
236     * no byte is available because the stream is at end of file, the value
237     * <code>-1</code> is returned; otherwise, at least one byte is read and
238     * stored into <code>b</code>.
239     * <p/>
240     * <p> The first byte read is stored into element <code>b[0]</code>, the
241     * next one into <code>b[1]</code>, and so on. The number of bytes read is,
242     * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
243     * number of bytes actually read; these bytes will be stored in elements
244     * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
245     * leaving elements <code>b[</code><i>k</i><code>]</code> through
246     * <code>b[b.length-1]</code> unaffected.
247     * <p/>
248     * <p> If the first byte cannot be read for any reason other than end of
249     * file, then an <code>IOException</code> is thrown. In particular, an
250     * <code>IOException</code> is thrown if the input stream has been closed.
251     * <p/>
252     * <p> The <code>read(b)</code> method for class <code>InputStream</code>
253     * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
254     *
255     * @param b the buffer into which the data is read.
256     * @return the total number of bytes read into the buffer, or
257     * <code>-1</code> is there is no more data because the end of
258     * the stream has been reached.
259     * @throws java.io.IOException if an I/O error occurs.
260     * @throws NullPointerException if <code>b</code> is <code>null</code>.
261     * @see java.io.InputStream#read(byte[], int, int)
262     */

263    public int read(byte b[]) throws IOException JavaDoc
264    {
265       System.err.println("read(byte b[])");
266       return read(b, 0, data.length);
267    }
268
269    /**
270     * Reads up to <code>len</code> bytes of data from the input stream into
271     * an array of bytes. An attempt is made to read as many as
272     * <code>len</code> bytes, but a smaller number may be read, possibly
273     * zero. The number of bytes actually read is returned as an integer.
274     * <p/>
275     * <p> This method blocks until input data is available, end of file is
276     * detected, or an exception is thrown.
277     * <p/>
278     * <p> If <code>b</code> is <code>null</code>, a
279     * <code>NullPointerException</code> is thrown.
280     * <p/>
281     * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
282     * <code>off+len</code> is greater than the length of the array
283     * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is
284     * thrown.
285     * <p/>
286     * <p> If <code>len</code> is zero, then no bytes are read and
287     * <code>0</code> is returned; otherwise, there is an attempt to read at
288     * least one byte. If no byte is available because the stream is at end of
289     * file, the value <code>-1</code> is returned; otherwise, at least one
290     * byte is read and stored into <code>b</code>.
291     * <p/>
292     * <p> The first byte read is stored into element <code>b[off]</code>, the
293     * next one into <code>b[off+1]</code>, and so on. The number of bytes read
294     * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
295     * bytes actually read; these bytes will be stored in elements
296     * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
297     * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
298     * <code>b[off+len-1]</code> unaffected.
299     * <p/>
300     * <p> In every case, elements <code>b[0]</code> through
301     * <code>b[off]</code> and elements <code>b[off+len]</code> through
302     * <code>b[b.length-1]</code> are unaffected.
303     * <p/>
304     * <p> If the first byte cannot be read for any reason other than end of
305     * file, then an <code>IOException</code> is thrown. In particular, an
306     * <code>IOException</code> is thrown if the input stream has been closed.
307     * <p/>
308     * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method
309     * for class <code>InputStream</code> simply calls the method
310     * <code>read()</code> repeatedly. If the first such call results in an
311     * <code>IOException</code>, that exception is returned from the call to
312     * the <code>read(b,</code> <code>off,</code> <code>len)</code> method. If
313     * any subsequent call to <code>read()</code> results in a
314     * <code>IOException</code>, the exception is caught and treated as if it
315     * were end of file; the bytes read up to that point are stored into
316     * <code>b</code> and the number of bytes read before the exception
317     * occurred is returned. Subclasses are encouraged to provide a more
318     * efficient implementation of this method.
319     *
320     * @param b the buffer into which the data is read.
321     * @param off the start offset in array <code>b</code>
322     * at which the data is written.
323     * @param len the maximum number of bytes to read.
324     * @return the total number of bytes read into the buffer, or
325     * <code>-1</code> if there is no more data because the end of
326     * the stream has been reached.
327     * @throws NullPointerException if <code>b</code> is <code>null</code>.
328     * @see java.io.InputStream#read()
329     */

330    public synchronized int read(byte b[], int off, int len)
331    {
332       if (b == null)
333       {
334          throw new NullPointerException JavaDoc();
335       }
336       else if ((off < 0) || (off > b.length) || (len < 0) ||
337             ((off + len) > b.length) || ((off + len) < 0))
338       {
339          throw new IndexOutOfBoundsException JavaDoc();
340       }
341       if (pos >= count)
342       {
343          return -1;
344       }
345       if (pos + len > count)
346       {
347          len = count - pos;
348       }
349       if (len <= 0)
350       {
351          return 0;
352       }
353       System.arraycopy(buf, pos, b, off, len);
354       pos += len;
355       return len;
356    }
357
358    /**
359     * Reads the next byte of data from the input stream. The value byte is
360     * returned as an <code>int</code> in the range <code>0</code> to
361     * <code>255</code>. If no byte is available because the end of the stream
362     * has been reached, the value <code>-1</code> is returned. This method
363     * blocks until input data is available, the end of the stream is detected,
364     * or an exception is thrown.
365     * <p/>
366     * <p> A subclass must provide an implementation of this method.
367     *
368     * @return the next byte of data, or <code>-1</code> if the end of the
369     * stream is reached.
370     */

371    public synchronized int read()
372    {
373       return (pos < count) ? (buf[pos++] & 0xff) : -1;
374    }
375 }
376
Popular Tags