LineOrientedFilteringReader.java

  1. /* Copyright 2022-2025 Luc Maisonobe
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS 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.orekit.data;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.Reader;

  21. import org.hipparchus.util.FastMath;

  22. /** Base class for implementing line-oriented data filtering readers.
  23.  * <p>
  24.  * This reader is intended to be used in {@link DataFilter}.
  25.  * </p>
  26.  * @author Luc Maisonobe
  27.  * @since 12.1
  28.  */
  29. public abstract class LineOrientedFilteringReader extends Reader {

  30.     /** Line-oriented input. */
  31.     private final BufferedReader reader;

  32.     /** Line number. */
  33.     private int lastLineNumber;

  34.     /** Pending filtered output lines. */
  35.     private CharSequence pending;

  36.     /** Number of characters already output in pending lines. */
  37.     private int countOut;

  38.     /** Simple constructor.
  39.      * @param name file name
  40.      * @param input underlying raw stream
  41.      * @exception IOException if first lines cannot be read
  42.      */
  43.     public LineOrientedFilteringReader(final String name, final Reader input) throws IOException {
  44.         reader         = new BufferedReader(input);
  45.         lastLineNumber = 0;
  46.     }

  47.     /** Get the underlying line-oriented reader.
  48.      * @return underlying line-oriented reader
  49.      */
  50.     protected BufferedReader getBufferedReader() {
  51.         return reader;
  52.     }

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     public int read(final char[] b, final int offset, final int len) throws IOException {

  56.         if (pending == null) {
  57.             // we need to read another line from the underlying characters stream and filter it
  58.             countOut = 0;
  59.             final String originalLine = reader.readLine();
  60.             ++lastLineNumber;
  61.             if (originalLine == null) {
  62.                 // there are no lines left
  63.                 return -1;
  64.             } else {
  65.                 pending = filterLine(lastLineNumber, originalLine);
  66.             }
  67.         }

  68.         // copy as many characters as possible from current line
  69.         int n = FastMath.min(len, pending.length() - countOut);
  70.         for (int i = 0; i < n; ++i) {
  71.             b[offset + i] = pending.charAt(countOut + i);
  72.         }

  73.         if (n < len) {
  74.             // line has been completed and we can still output end of line
  75.             b[offset + n] = '\n';
  76.             pending       = null;
  77.             ++n;
  78.         } else {
  79.             // there are still some pending characters
  80.             countOut += n;
  81.         }

  82.         return n;

  83.     }

  84.     /** Filter one line.
  85.      * @param lineNumber line number
  86.      * @param originalLine original line
  87.      * @return filtered line
  88.      * @exception IOException if line cannot be parsed
  89.      */
  90.     protected abstract CharSequence filterLine(int lineNumber, String originalLine) throws IOException;

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public void close() throws IOException {
  94.         reader.close();
  95.     }

  96. }