001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.jdbc.remote;
023:
024: import java.io.BufferedReader;
025: import java.io.IOException;
026: import java.io.Reader;
027: import java.io.Serializable;
028:
029: /**
030: * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
031: * @version $Revision: 57189 $
032: */
033: public class SerializableReader extends Reader implements Serializable {
034: /** @since 1.1 */
035: static final long serialVersionUID = 1244952470388397765L;
036: private char[] data = null;
037:
038: protected char buf[];
039: protected int pos;
040: protected int mark = 0;
041: protected int count;
042:
043: public SerializableReader(Reader reader) throws IOException {
044: BufferedReader in = new BufferedReader(reader);
045: String line = in.readLine();
046: while (line != null) {
047: String current = (data == null) ? "" : new String(data);
048: String newData = current + line;
049: data = newData.toCharArray();
050: line = in.readLine();
051: }
052:
053: reader.close();
054:
055: this .buf = this .data;
056: this .pos = 0;
057: this .count = this .buf.length;
058:
059: }
060:
061: /**
062: * Close the stream. Once a stream has been closed, further read(),
063: * ready(), mark(), or reset() invocations will throw an IOException.
064: * Closing a previously-closed stream, however, has no effect.
065: *
066: * @throws java.io.IOException If an I/O error occurs
067: */
068: public void close() throws IOException {
069: // Do nothing
070: }
071:
072: /**
073: * Read characters into a portion of an array. This method will block
074: * until some input is available, an I/O error occurs, or the end of the
075: * stream is reached.
076: *
077: * @param cbuf Destination buffer
078: * @param off Offset at which to start storing characters
079: * @param len Maximum number of characters to read
080: * @return The number of characters read, or -1 if the end of the
081: * stream has been reached
082: * @throws java.io.IOException If an I/O error occurs
083: */
084: public int read(char cbuf[], int off, int len) throws IOException {
085: if (cbuf == null) {
086: throw new NullPointerException();
087: } else if ((off < 0) || (off > cbuf.length) || (len < 0)
088: || ((off + len) > cbuf.length) || ((off + len) < 0)) {
089: throw new IndexOutOfBoundsException();
090: }
091:
092: if (pos >= count) {
093: return -1;
094: }
095: if (pos + len > count) {
096: len = count - pos;
097: }
098: if (len <= 0) {
099: return 0;
100: }
101: System.arraycopy(buf, pos, cbuf, off, len);
102: pos += len;
103: return len;
104: }
105:
106: }
|