All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.twelvemonkeys.io.CompoundReader Maven / Gradle / Ivy

There is a newer version: 2.3
Show newest version
/*
 * Copyright (c) 2008, Harald Kuhr
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name "TwelveMonkeys" nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.twelvemonkeys.io;

import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;

/**
 * A Reader implementation that can read from multiple sources.
 * 

* * @author Harald Kuhr * @author last modified by $Author: haku $ * @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/CompoundReader.java#1 $ */ public class CompoundReader extends Reader { private Reader mCurrent; private List mReaders; protected final Object mLock; protected final boolean mMarkSupported; private int mCurrentReader; private int mMarkedReader; private int mMark; private int mNext; /** * Create a new compound reader. * * @param pReaders Iterator containting Readers, * providing the character stream. * * @throws NullPointerException if pReaders is null, or * any of the elements in the iterator is null. * @throws ClassCastException if any element of the iterator is not a * java.io.Reader */ public CompoundReader(Iterator pReaders) { super(""); if (pReaders == null) { throw new NullPointerException("readers == null"); } mLock = lock = pReaders; // NOTE: It's ok to sync on pReaders, as the // reference can't change, only it's elements mReaders = new ArrayList(); boolean markSupported = true; while (pReaders.hasNext()) { Reader reader = (Reader) pReaders.next(); if (reader == null) { throw new NullPointerException("readers cannot contain null-elements"); } mReaders.add(reader); markSupported = markSupported && reader.markSupported(); } mMarkSupported = markSupported; mCurrent = nextReader(); } protected final Reader nextReader() { if (mCurrentReader >= mReaders.size()) { mCurrent = new EmptyReader(); } else { mCurrent = (Reader) mReaders.get(mCurrentReader++); } // NOTE: Reset mNext for every reader, and record marked reader in mark/reset methods! mNext = 0; return mCurrent; } /** * Check to make sure that the stream has not been closed * * @throws IOException if the stream is closed */ protected final void ensureOpen() throws IOException { if (mReaders == null) { throw new IOException("Stream closed"); } } public void close() throws IOException { // Close all readers for (Iterator iterator = mReaders.iterator(); iterator.hasNext();) { Reader reader = (Reader) iterator.next(); reader.close(); } mReaders = null; } public void mark(int pReadLimit) throws IOException { if (pReadLimit < 0){ throw new IllegalArgumentException("Read limit < 0"); } // TODO: It would be nice if we could actually close some readers now synchronized (mLock) { ensureOpen(); mMark = mNext; mMarkedReader = mCurrentReader; mCurrent.mark(pReadLimit); } } public void reset() throws IOException { synchronized (mLock) { ensureOpen(); if (mCurrentReader != mMarkedReader) { // Reset any reader before this for (int i = mCurrentReader; i >= mMarkedReader; i--) { ((Reader) mReaders.get(i)).reset(); } mCurrentReader = mMarkedReader - 1; nextReader(); } mCurrent.reset(); mNext = mMark; } } public boolean markSupported() { return mMarkSupported; } public int read() throws IOException { synchronized (mLock) { int read = mCurrent.read(); if (read < 0 && mCurrentReader < mReaders.size()) { nextReader(); return read(); // In case of 0-length readers } mNext++; return read; } } public int read(char pBuffer[], int pOffset, int pLength) throws IOException { synchronized (mLock) { int read = mCurrent.read(pBuffer, pOffset, pLength); if (read < 0 && mCurrentReader < mReaders.size()) { nextReader(); return read(pBuffer, pOffset, pLength); // In case of 0-length readers } mNext += read; return read; } } public boolean ready() throws IOException { return mCurrent.ready(); } public long skip(long pChars) throws IOException { synchronized (mLock) { long skipped = mCurrent.skip(pChars); if (skipped == 0 && mCurrentReader < mReaders.size()) { nextReader(); return skip(pChars); // In case of 0-length readers } mNext += skipped; return skipped; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy