AbstractCharacterFilterReader.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io.input;

  18. import static org.apache.commons.io.IOUtils.EOF;

  19. import java.io.FilterReader;
  20. import java.io.IOException;
  21. import java.io.Reader;
  22. import java.util.function.IntPredicate;

  23. /**
  24.  * A filter reader that filters out characters where subclasses decide which characters to filter out.
  25.  */
  26. public abstract class AbstractCharacterFilterReader extends FilterReader {

  27.     /**
  28.      * Skips nothing.
  29.      *
  30.      * @since 2.9.0
  31.      */
  32.     protected static final IntPredicate SKIP_NONE = ch -> false;

  33.     private final IntPredicate skip;

  34.     /**
  35.      * Constructs a new reader.
  36.      *
  37.      * @param reader the reader to filter
  38.      */
  39.     protected AbstractCharacterFilterReader(final Reader reader) {
  40.         this(reader, SKIP_NONE);
  41.     }

  42.     /**
  43.      * Constructs a new reader.
  44.      *
  45.      * @param reader the reader to filter.
  46.      * @param skip Skip test.
  47.      * @since 2.9.0
  48.      */
  49.     protected AbstractCharacterFilterReader(final Reader reader, final IntPredicate skip) {
  50.         super(reader);
  51.         this.skip = skip == null ? SKIP_NONE : skip;
  52.     }

  53.     /**
  54.      * Returns true if the given character should be filtered out, false to keep the character.
  55.      *
  56.      * @param ch the character to test.
  57.      * @return true if the given character should be filtered out, false to keep the character.
  58.      */
  59.     protected boolean filter(final int ch) {
  60.         return skip.test(ch);
  61.     }

  62.     @Override
  63.     public int read() throws IOException {
  64.         int ch;
  65.         do {
  66.             ch = in.read();
  67.         } while (ch != EOF && filter(ch));
  68.         return ch;
  69.     }

  70.     @Override
  71.     public int read(final char[] cbuf, final int off, final int len) throws IOException {
  72.         final int read = super.read(cbuf, off, len);
  73.         if (read == EOF) {
  74.             return EOF;
  75.         }
  76.         int pos = off - 1;
  77.         for (int readPos = off; readPos < off + read; readPos++) {
  78.             if (filter(cbuf[readPos])) {
  79.                 continue;
  80.             }
  81.             pos++;
  82.             if (pos < readPos) {
  83.                 cbuf[pos] = cbuf[readPos];
  84.             }
  85.         }
  86.         return pos - off + 1;
  87.     }
  88. }