1 package org.apache.maven.plugin.assembly.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25
26
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 {
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 }