protected Reader makeReader(String pInput) { // Split String[] input = StringUtil.toStringArray(pInput, " "); List<Reader> readers = new ArrayList<Reader>(input.length); // Reappend spaces... // TODO: Add other readers for (int i = 0; i < input.length; i++) { if (i != 0) { input[i] = " " + input[i]; } readers.add(new StringReader(input[i])); } return new CompoundReader(readers.iterator()); }
@Override public void reset() throws IOException { synchronized (finalLock) { ensureOpen(); if (currentReader != markedReader) { // Reset any reader before this for (int i = currentReader; i >= markedReader; i--) { readers.get(i).reset(); } currentReader = markedReader - 1; nextReader(); } current.reset(); mNext = mark; } }
public int read(char pBuffer[], int pOffset, int pLength) throws IOException { synchronized (finalLock) { int read = current.read(pBuffer, pOffset, pLength); if (read < 0 && currentReader < readers.size()) { nextReader(); return read(pBuffer, pOffset, pLength); // In case of 0-length readers } mNext += read; return read; } }
@Override public long skip(long pChars) throws IOException { synchronized (finalLock) { long skipped = current.skip(pChars); if (skipped == 0 && currentReader < readers.size()) { nextReader(); return skip(pChars); // In case of 0-length readers } mNext += skipped; return skipped; } } }
@Override 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 (finalLock) { ensureOpen(); mark = mNext; markedReader = currentReader; current.mark(pReadLimit); } }
/** * Create a new compound reader. * * @param pReaders {@code Iterator} containting {@code Reader}s, * providing the character stream. * * @throws NullPointerException if {@code pReaders} is {@code null}, or * any of the elements in the iterator is {@code null}. * @throws ClassCastException if any element of the iterator is not a * {@code java.io.Reader} */ public CompoundReader(final Iterator<Reader> pReaders) { super(Validate.notNull(pReaders, "readers")); finalLock = pReaders; // NOTE: It's ok to sync on pReaders, as the // reference can't change, only it's elements readers = new ArrayList<Reader>(); boolean markSupported = true; while (pReaders.hasNext()) { Reader reader = pReaders.next(); if (reader == null) { throw new NullPointerException("readers cannot contain null-elements"); } readers.add(reader); markSupported = markSupported && reader.markSupported(); } this.markSupported = markSupported; current = nextReader(); }
@Override public long skip(long pChars) throws IOException { synchronized (finalLock) { long skipped = current.skip(pChars); if (skipped == 0 && currentReader < readers.size()) { nextReader(); return skip(pChars); // In case of 0-length readers } mNext += skipped; return skipped; } } }
@Override 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); } }
/** * Create a new compound reader. * * @param pReaders {@code Iterator} containting {@code Reader}s, * providing the character stream. * * @throws NullPointerException if {@code pReaders} is {@code null}, or * any of the elements in the iterator is {@code null}. * @throws ClassCastException if any element of the iterator is not a * {@code java.io.Reader} */ public CompoundReader(final Iterator<Reader> pReaders) { super(Validate.notNull(pReaders, "readers")); finalLock = pReaders; // NOTE: It's ok to sync on pReaders, as the // reference can't change, only it's elements readers = new ArrayList<Reader>(); boolean markSupported = true; while (pReaders.hasNext()) { Reader reader = pReaders.next(); if (reader == null) { throw new NullPointerException("readers cannot contain null-elements"); } readers.add(reader); markSupported = markSupported && reader.markSupported(); } this.markSupported = markSupported; current = nextReader(); }
@Override public int read() throws IOException { synchronized (finalLock) { int read = current.read(); if (read < 0 && currentReader < readers.size()) { nextReader(); return read(); // In case of 0-length readers } mNext++; return read; } }
@Test public void testNullConstructor() { try { new CompoundReader(null); fail("Should not allow null argument"); } catch (RuntimeException e) { assertNotNull(e.getMessage()); } }
@Override 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; } } }
@Override public void reset() throws IOException { synchronized (mLock) { ensureOpen(); if (mCurrentReader != mMarkedReader) { // Reset any reader before this for (int i = mCurrentReader; i >= mMarkedReader; i--) { mReaders.get(i).reset(); } mCurrentReader = mMarkedReader - 1; nextReader(); } mCurrent.reset(); mNext = mMark; } }
@Override 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 (finalLock) { ensureOpen(); mark = mNext; markedReader = currentReader; current.mark(pReadLimit); } }
/** * Create a new compound reader. * * @param pReaders {@code Iterator} containting {@code Reader}s, * providing the character stream. * * @throws NullPointerException if {@code pReaders} is {@code null}, or * any of the elements in the iterator is {@code null}. * @throws ClassCastException if any element of the iterator is not a * {@code java.io.Reader} */ public CompoundReader(final Iterator<Reader> pReaders) { super(Validate.notNull(pReaders, "readers")); mLock = pReaders; // NOTE: It's ok to sync on pReaders, as the // reference can't change, only it's elements mReaders = new ArrayList<Reader>(); boolean markSupported = true; while (pReaders.hasNext()) { 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(); }
public int read(char pBuffer[], int pOffset, int pLength) throws IOException { synchronized (finalLock) { int read = current.read(pBuffer, pOffset, pLength); if (read < 0 && currentReader < readers.size()) { nextReader(); return read(pBuffer, pOffset, pLength); // In case of 0-length readers } mNext += read; return read; } }
@Test public void testEmptyIteratorConstructor() throws IOException { Reader reader = new CompoundReader(CollectionUtil.iterator(new Reader[0])); assertEquals(-1, reader.read()); }
@Override public void reset() throws IOException { synchronized (finalLock) { ensureOpen(); if (currentReader != markedReader) { // Reset any reader before this for (int i = currentReader; i >= markedReader; i--) { readers.get(i).reset(); } currentReader = markedReader - 1; nextReader(); } current.reset(); mNext = mark; } }
@Override public int read() throws IOException { synchronized (finalLock) { int read = current.read(); if (read < 0 && currentReader < readers.size()) { nextReader(); return read(); // In case of 0-length readers } mNext++; return read; } }
@Test public void testIteratorWithNullConstructor() throws IOException { try { new CompoundReader(CollectionUtil.iterator(new Reader[] {null})); fail("Should not allow null in iterator argument"); } catch (RuntimeException e) { assertNotNull(e.getMessage()); } } }