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:
022: import org.compass.core.engine.RepeatableReader;
023:
024: /**
025: * A character stream whose source is a string. reverses the string. Allows for
026: * repeatable reads from the same string.
027: * <p/>
028: * Note, this is an unsafe reader in terms of {@link IndexOutOfBoundsException}.
029: *
030: * @author kimchy
031: */
032: public class ReverseStringReader extends Reader implements
033: RepeatableReader {
034:
035: private String str;
036: private int next = 0;
037: private int mark = 0;
038:
039: public ReverseStringReader(String s) {
040: this .str = s;
041: this .next = s.length();
042: }
043:
044: public int read() throws IOException {
045: if (next <= 0)
046: return -1;
047: return str.charAt(--next);
048: }
049:
050: public int read(char cbuf[], int off, int len) throws IOException {
051: if (len == 0) {
052: return 0;
053: }
054: if (next <= 0) {
055: // reset the repeatable reader
056: close();
057: // return -1 indicating nothing left to read
058: return -1;
059: }
060: int n = Math.min(next, len);
061: for (int i = 0; i < n; i++) {
062: cbuf[off + i] = (char) read();
063: }
064: return n;
065: }
066:
067: public long skip(long ns) throws IOException {
068: if (next <= 0)
069: return 0;
070: long n = Math.min(next, ns);
071: next -= n;
072: return n;
073: }
074:
075: public boolean ready() throws IOException {
076: return true;
077: }
078:
079: public boolean markSupported() {
080: return true;
081: }
082:
083: public void mark(int readAheadLimit) throws IOException {
084: if (readAheadLimit < 0) {
085: throw new IllegalArgumentException("Read-ahead limit < 0");
086: }
087: mark = next;
088: }
089:
090: public void reset() throws IOException {
091: next = mark;
092: }
093:
094: // as part of the repeatable reader, the close will actually return the reader to it's
095: // initial state
096: public void close() {
097: this.next = str.length();
098: }
099:
100: }
|