001package ball.io;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: CharSequenceReader.java 5856 2020-04-27 20:21:05Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/io/CharSequenceReader.java $
007 * %%
008 * Copyright (C) 2008 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import java.io.IOException;
024import java.io.LineNumberReader;
025import java.io.Reader;
026import java.util.Objects;
027import lombok.ToString;
028
029/**
030 * {@link CharSequence} {@link Reader} implementation.
031 *
032 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
033 * @version $Revision: 5856 $
034 */
035@ToString
036public class CharSequenceReader extends LineNumberReader {
037
038    /**
039     * Sole constructor.
040     *
041     * @param   sequence        The {@link CharSequence}.
042     */
043    public CharSequenceReader(CharSequence sequence) {
044        super(new ReaderImpl(sequence), 1024);
045    }
046
047    @ToString
048    private static class ReaderImpl extends Reader {
049        private CharSequence sequence = null;
050        private volatile int length = 0;
051        private volatile int pos = 0;
052        private volatile int mark = 0;
053
054        public ReaderImpl(CharSequence sequence) {
055            super(new Object());
056
057            this.sequence = Objects.requireNonNull(sequence);
058            this.length = sequence.length();
059        }
060
061        @Override
062        public int read() throws IOException {
063            int character = -1;
064
065            synchronized (lock) {
066                if (pos < length) {
067                    character = sequence.charAt(pos++);
068                }
069            }
070
071            return character;
072        }
073
074        @Override
075        public int read(char chars[], int off, int len) throws IOException {
076            int count = -1;
077
078            synchronized (lock) {
079                if (pos < length) {
080                    count = Math.min(length - pos, len);
081
082                    for (int i = 0; i < count; i += 1) {
083                        chars[off + i] = sequence.charAt(pos + i);
084                    }
085
086                    pos += count;
087                }
088            }
089
090            return count;
091        }
092
093        @Override
094        public long skip(long count) throws IOException {
095            if (count < 0) {
096                throw new IllegalArgumentException("skip value is negative");
097            }
098
099            synchronized (lock) {
100                count = Math.min(count, length - pos);
101                pos += count;
102            }
103
104            return count;
105        }
106
107        @Override
108        public boolean ready() throws IOException { return true; }
109
110        @Override
111        public boolean markSupported() { return true; }
112
113        @Override
114        public void mark(int readAheadLimit) throws IOException {
115            if (readAheadLimit < 0){
116                throw new IllegalArgumentException("Read-ahead limit < 0");
117            }
118
119            synchronized (lock) {
120                mark = pos;
121            }
122        }
123
124        @Override
125        public void reset() throws IOException {
126            synchronized (lock) {
127                pos = mark;
128            }
129        }
130
131        @Override
132        public void close() throws IOException { sequence = null; }
133    }
134}