001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.io.util;
020
021import java.io.FilterReader;
022import java.io.IOException;
023import java.io.Reader;
024
025/**
026 * A filter that copies all data read into a buffer. The buffer has to be
027 * cleared regularly (see {@link #copied} else heap space will eventually
028 * become exhausted. 
029 */
030public class CopyReader extends FilterReader {
031
032    @SuppressWarnings("PMD.AvoidStringBufferField")
033    private final StringBuilder copied = new StringBuilder();
034    private int copyBufferSize = 1024 * 10;
035    private boolean overflow;
036
037    /**
038     * Instantiates a new copy reader.
039     *
040     * @param source the source
041     */
042    public CopyReader(Reader source) {
043        super(source);
044    }
045
046    /**
047     * Sets the copy buffer size, defaults to 10240.
048     *
049     * @param size the size
050     * @return the copy reader
051     */
052    public CopyReader setCopyBufferSize(int size) {
053        copyBufferSize = size;
054        return this;
055    }
056
057    @Override
058    public int read() throws IOException {
059        char[] buf = new char[1];
060        if (read(buf, 0, 1) == -1) {
061            return -1;
062        } else {
063            return buf[0];
064        }
065    }
066
067    @Override
068    public int read(char[] cbuf, int off, int len) throws IOException {
069        int count = in.read(cbuf, off, len);
070        if (count > 0 && !overflow) {
071            if (copied.length() > copyBufferSize) {
072                copied.append("...");
073                overflow = true;
074            } else {
075                copied.append(cbuf, off, count);
076            }
077        }
078        return count;
079    }
080
081    /**
082     * Returns all chars and resets the copy buffer.
083     *
084     * @return the string
085     */
086    public String copied() {
087        int count = copied.length();
088        String res = copied.substring(0, count);
089        copied.delete(0, count);
090        overflow = false;
091        return res;
092
093    }
094}