001 /*
002 * LAPIS lightweight structured text processing system
003 *
004 * Copyright (C) 1998-2002 Carnegie Mellon University,
005 * Copyright (C) 2003 Massachusetts Institute of Technology.
006 * All rights reserved.
007 *
008 * This library is free software; you can redistribute it
009 * and/or modify it under the terms of the GNU General
010 * Public License as published by the Free Software
011 * Foundation, version 2.
012 *
013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014 */
015
016 package lapisx.progress;
017
018 import java.io.*;
019
020 public class ProgressReader extends FilterReader {
021 int chunkSize;
022 int n; // number of characters read for current chunk
023 BasicProgressGenerator gen;
024
025 public ProgressReader (Reader in, int chunkSize, BasicProgressGenerator gen) {
026 super (in);
027 this.chunkSize = chunkSize;
028 this.gen = gen;
029 }
030
031 public int read () throws IOException {
032 int c = super.read ();
033 if (c != -1 && ++n == chunkSize) {
034 gen.fireAddProgress (n);
035 n = 0;
036 }
037 return c;
038 }
039
040 public int read (char[] buf, int off, int len) throws IOException {
041 int r = super.read (buf, off, len);
042 if (r != -1) {
043 n += r;
044 if (n >= chunkSize) {
045 gen.fireAddProgress (n);
046 n = 0;
047 }
048 }
049 return r;
050 }
051 }