001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.util.reader;
018:
019: import java.io.IOException;
020: import java.io.Reader;
021: import java.util.ArrayList;
022:
023: import org.compass.core.util.StringUtils;
024:
025: /**
026: * A <code>Reader</code> implementation which wraps several
027: * <code>Reader</code>s and reads them sequentially.
028: *
029: * @author kimchy
030: */
031: public class MultiIOReader extends Reader {
032:
033: private ArrayList readers = new ArrayList();
034:
035: private ArrayList names = new ArrayList();
036:
037: private Reader currentReader;
038:
039: private int index = 0;
040:
041: public MultiIOReader() {
042: }
043:
044: public MultiIOReader(Reader reader) {
045: add(reader);
046: }
047:
048: public MultiIOReader(String name, Reader reader) {
049: add(name, reader);
050: }
051:
052: public MultiIOReader(Reader[] readers) {
053: for (int i = 0; i < readers.length; i++) {
054: add(null, readers[i]);
055: }
056: }
057:
058: public MultiIOReader(String[] names, Reader[] readers) {
059: for (int i = 0; i < readers.length; i++) {
060: add(names[i], readers[i]);
061: }
062: }
063:
064: public void add(Reader reader) {
065: add(null, reader);
066: }
067:
068: public void add(String name, Reader reader) {
069: if (reader == null) {
070: return;
071: }
072: if (name != null) {
073: names.add(name);
074: }
075: readers.add(reader);
076: if (currentReader == null) {
077: currentReader = reader;
078: }
079: }
080:
081: /**
082: * Check to make sure that the stream has not been closed
083: */
084: private void ensureOpen() throws IOException {
085: if (readers == null)
086: throw new IOException("Stream closed");
087: }
088:
089: public int read() throws IOException {
090: ensureOpen();
091: int retVal = -1;
092:
093: while (retVal == -1) {
094: retVal = currentReader.read();
095: if (retVal == -1) {
096: ++index;
097: if (index == readers.size()) {
098: return -1;
099: }
100: currentReader = (Reader) readers.get(index);
101: }
102: }
103: return retVal;
104: }
105:
106: public int read(char cbuf[], int off, int len) throws IOException {
107: ensureOpen();
108: int sizeRead = -1;
109: while (sizeRead == -1) {
110: sizeRead = currentReader.read(cbuf, off, len);
111: if (sizeRead == -1) {
112: ++index;
113: if (index == readers.size()) {
114: return -1;
115: }
116: currentReader = (Reader) readers.get(index);
117: }
118: }
119: return sizeRead;
120: }
121:
122: public void close() throws IOException {
123: // the reader is closed, no need to close it again
124: if (readers == null) {
125: return;
126: }
127:
128: IOException ioe = null;
129: for (int i = 0; i < readers.size(); i++) {
130: try {
131: ((Reader) readers.get(i)).close();
132: } catch (IOException e) {
133: ioe = e;
134: }
135: }
136: readers = null;
137: if (ioe != null) {
138: throw ioe;
139: }
140: }
141:
142: public String toString() {
143: return StringUtils.collectionToCommaDelimitedString(names);
144: }
145: }
|