View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.utils;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  /**
25   * @author Kristian Rosenvold
26   */
27  class LinuxLineFeedInputStream extends InputStream {
28  
29      private final InputStream target;
30  
31      private final boolean ensureLineFeedAtEndOfFile;
32  
33      private boolean slashNSeen = false;
34  
35      private boolean slashRSeen = false;
36  
37      private boolean eofSeen = false;
38  
39      LinuxLineFeedInputStream(InputStream in, boolean ensureLineFeedAtEndOfFile) {
40          this.target = in;
41          this.ensureLineFeedAtEndOfFile = ensureLineFeedAtEndOfFile;
42      }
43  
44      private int readWithUpdate() throws IOException {
45          final int target = this.target.read();
46          eofSeen = target == -1;
47          if (eofSeen) {
48              return target;
49          }
50          slashNSeen = target == '\n';
51          slashRSeen = target == '\r';
52          return target;
53      }
54  
55      @Override
56      public int read() throws IOException {
57          boolean prevWasSlashR = slashRSeen;
58          if (eofSeen) {
59              return eofGame(prevWasSlashR);
60          } else {
61              int target = readWithUpdate();
62              if (eofSeen) {
63                  return eofGame(prevWasSlashR);
64              }
65              if (slashRSeen) {
66                  return '\n';
67              }
68  
69              if (prevWasSlashR && slashNSeen) { // Lone /r
70                  return read();
71              }
72              return target;
73          }
74      }
75  
76      private int eofGame(boolean previousWasSlashR) {
77          if (previousWasSlashR || !ensureLineFeedAtEndOfFile) {
78              return -1;
79          }
80          if (!slashNSeen) {
81              slashNSeen = true;
82              return '\n';
83          } else {
84              return -1;
85          }
86      }
87  
88      @Override
89      public void close() throws IOException {
90          super.close();
91          target.close();
92      }
93  
94      @Override
95      public synchronized void mark(int readlimit) {
96          throw new UnsupportedOperationException("Mark not implemented yet");
97      }
98  }