View Javadoc
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    *      https://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  
19  import static org.apache.commons.io.IOUtils.CR;
20  import static org.apache.commons.io.IOUtils.EOF;
21  import static org.apache.commons.io.IOUtils.LF;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  /**
27   * A filtering input stream that ensures the content will have Windows line endings, CRLF.
28   *
29   * @since 2.5
30   */
31  public class WindowsLineEndingInputStream extends AbstractLineEndingInputStream {
32  
33      private boolean injectSlashLf;
34  
35      /**
36       * Constructs an input stream that filters another stream.
37       *
38       * @param in                        The input stream to wrap.
39       * @param lineFeedAtEos true to ensure that the stream ends with CRLF.
40       */
41      public WindowsLineEndingInputStream(final InputStream in, final boolean lineFeedAtEos) {
42          super(in, lineFeedAtEos);
43      }
44  
45      /**
46       * Handles the end of stream condition.
47       *
48       * @return The next char to output to the stream.
49       */
50      private int handleEos() {
51          if (!lineFeedAtEos) {
52              return EOF;
53          }
54          if (!atSlashLf && !atSlashCr) {
55              atSlashCr = true;
56              return CR;
57          }
58          if (!atSlashLf) {
59              atSlashCr = false;
60              atSlashLf = true;
61              return LF;
62          }
63          return EOF;
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public synchronized int read() throws IOException {
71          if (atEos) {
72              return handleEos();
73          }
74          if (injectSlashLf) {
75              injectSlashLf = false;
76              return LF;
77          }
78          final boolean prevWasSlashR = atSlashCr;
79          final int target = in.read();
80          atEos = target == EOF;
81          if (!atEos) {
82              atSlashCr = target == CR;
83              atSlashLf = target == LF;
84          }
85          if (atEos) {
86              return handleEos();
87          }
88          if (target == LF && !prevWasSlashR) {
89              injectSlashLf = true;
90              return CR;
91          }
92          return target;
93      }
94  }