KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.io.Serializable JavaDoc;
28
29 /**
30  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
31  * @version $Revision: 37459 $
32  */

33 public class SerializableReader extends Reader JavaDoc implements Serializable JavaDoc
34 {
35    /** @since 1.1 */
36    static final long serialVersionUID = 1244952470388397765L;
37    private char[] data = null;
38
39    protected char buf[];
40    protected int pos;
41    protected int mark = 0;
42    protected int count;
43
44    public SerializableReader(Reader JavaDoc reader) throws IOException JavaDoc
45    {
46       BufferedReader JavaDoc in = new BufferedReader JavaDoc(reader);
47       String JavaDoc line = in.readLine();
48       while (line != null)
49       {
50          String JavaDoc current = (data == null) ? "" : new String JavaDoc(data);
51          String JavaDoc newData = current + line;
52          data = newData.toCharArray();
53          line = in.readLine();
54       }
55
56       reader.close();
57
58       this.buf = this.data;
59       this.pos = 0;
60       this.count = this.buf.length;
61
62    }
63
64    /**
65     * Close the stream. Once a stream has been closed, further read(),
66     * ready(), mark(), or reset() invocations will throw an IOException.
67     * Closing a previously-closed stream, however, has no effect.
68     *
69     * @throws java.io.IOException If an I/O error occurs
70     */

71    public void close() throws IOException JavaDoc
72    {
73       // Do nothing
74
}
75
76    /**
77     * Read characters into a portion of an array. This method will block
78     * until some input is available, an I/O error occurs, or the end of the
79     * stream is reached.
80     *
81     * @param cbuf Destination buffer
82     * @param off Offset at which to start storing characters
83     * @param len Maximum number of characters to read
84     * @return The number of characters read, or -1 if the end of the
85     * stream has been reached
86     * @throws java.io.IOException If an I/O error occurs
87     */

88    public int read(char cbuf[], int off, int len) throws IOException JavaDoc
89    {
90       if (cbuf == null)
91       {
92          throw new NullPointerException JavaDoc();
93       }
94       else if ((off < 0) || (off > cbuf.length) || (len < 0) ||
95             ((off + len) > cbuf.length) || ((off + len) < 0))
96       {
97          throw new IndexOutOfBoundsException JavaDoc();
98       }
99
100       if (pos >= count)
101       {
102          return -1;
103       }
104       if (pos + len > count)
105       {
106          len = count - pos;
107       }
108       if (len <= 0)
109       {
110          return 0;
111       }
112       System.arraycopy(buf, pos, cbuf, off, len);
113       pos += len;
114       return len;
115    }
116
117 }
Popular Tags