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    *      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  
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 InputStream {
32  
33      private boolean atEos;
34  
35      private boolean atSlashCr;
36  
37      private boolean atSlashLf;
38  
39      private final InputStream in;
40  
41      private boolean injectSlashLf;
42  
43      private final boolean lineFeedAtEndOfFile;
44  
45      /**
46       * Constructs an input stream that filters another stream
47       *
48       * @param in                        The input stream to wrap
49       * @param ensureLineFeedAtEndOfFile true to ensure that the file ends with CRLF
50       */
51      public WindowsLineEndingInputStream(final InputStream in, final boolean ensureLineFeedAtEndOfFile) {
52          this.in = in;
53          this.lineFeedAtEndOfFile = ensureLineFeedAtEndOfFile;
54      }
55  
56      /**
57       * Closes the stream. Also closes the underlying stream.
58       *
59       * @throws IOException upon error
60       */
61      @Override
62      public void close() throws IOException {
63          super.close();
64          in.close();
65      }
66  
67      /**
68       * Handles the end of stream condition.
69       *
70       * @return The next char to output to the stream
71       */
72      private int handleEos() {
73          if (!lineFeedAtEndOfFile) {
74              return EOF;
75          }
76          if (!atSlashLf && !atSlashCr) {
77              atSlashCr = true;
78              return CR;
79          }
80          if (!atSlashLf) {
81              atSlashCr = false;
82              atSlashLf = true;
83              return LF;
84          }
85          return EOF;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public synchronized void mark(final int readLimit) {
93          throw UnsupportedOperationExceptions.mark();
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public int read() throws IOException {
101         if (atEos) {
102             return handleEos();
103         }
104         if (injectSlashLf) {
105             injectSlashLf = false;
106             return LF;
107         }
108         final boolean prevWasSlashR = atSlashCr;
109         final int target = readWithUpdate();
110         if (atEos) {
111             return handleEos();
112         }
113         if (target == LF && !prevWasSlashR) {
114             injectSlashLf = true;
115             return CR;
116         }
117         return target;
118     }
119 
120     /**
121      * Reads the next item from the target, updating internal flags in the process
122      * @return the next int read from the target stream
123      * @throws IOException upon error
124      */
125     private int readWithUpdate() throws IOException {
126         final int target = this.in.read();
127         atEos = target == EOF;
128         if (atEos) {
129             return target;
130         }
131         atSlashCr = target == CR;
132         atSlashLf = target == LF;
133         return target;
134     }
135 }