TruncatingFilter.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. /** Filter for truncating line-oriented files.
  23.  * <p>
  24.  * This filter is mainly intended for test purposes, but may also
  25.  * be used to filter out unwanted trailing data in time series for example
  26.  * </p>
  27.  * @author Luc Maisonobe
  28.  * @since 12.0
  29.  */
  30. public class TruncatingFilter implements DataFilter {

  31.     /** Number of lines to keep. */
  32.     private final int nbLines;

  33.     /** Simple constructor.
  34.      * @param nbLines number of lines to keep
  35.      */
  36.     public TruncatingFilter(final int nbLines) {
  37.         this.nbLines = nbLines;
  38.     }

  39.     /** {@inheritDoc} */
  40.     @Override
  41.     public DataSource filter(final DataSource original) throws IOException {
  42.         return new DataSource(original.getName() + "-truncated-after-line-" + nbLines,
  43.                               () -> new TruncatingReader(original.getOpener().openReaderOnce()));
  44.     }

  45.     private class TruncatingReader extends Reader {

  46.         /** Line-oriented reader for raw data. */
  47.         private final BufferedReader reader;

  48.         /** Number of lines already read. */
  49.         private int linesRead;

  50.         /** Pending line, read but not output. */
  51.         private String pending;

  52.         /** Number of characters already output in pending line. */
  53.         private int countOut;

  54.         TruncatingReader(final Reader reader) {
  55.             this.reader = new BufferedReader(reader);
  56.         }

  57.         /** {@inheritDoc} */
  58.         @Override
  59.         public int read(final char[] b, final int offset, final int len) throws IOException {

  60.             if (linesRead < nbLines) {

  61.                 if (pending == null) {
  62.                     // we need to read another part from the underlying characters stream
  63.                     countOut = 0;
  64.                     pending = reader.readLine();
  65.                     if (pending == null) {
  66.                         // there are no lines left
  67.                         return -1;
  68.                     }
  69.                 }

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

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

  86.             } else {
  87.                 return -1;
  88.             }

  89.         }

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

  95.     }

  96. }