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