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 WindowsLineFeedInputStream
29 extends InputStream
30 {
31
32 private boolean slashRSeen = false;
33
34 private boolean slashNSeen = false;
35
36 private boolean injectSlashN = false;
37
38 private boolean eofSeen = false;
39
40 private final InputStream target;
41
42 private final boolean ensureLineFeedAtEndOfFile;
43
44 public WindowsLineFeedInputStream( InputStream in, boolean ensureLineFeedAtEndOfFile )
45 {
46 this.target = in;
47 this.ensureLineFeedAtEndOfFile = ensureLineFeedAtEndOfFile;
48 }
49
50 private int readWithUpdate()
51 throws IOException
52 {
53 final int target = this.target.read();
54 eofSeen = target == -1;
55 if ( eofSeen )
56 {
57 return target;
58 }
59 slashRSeen = target == '\r';
60 slashNSeen = target == '\n';
61 return target;
62 }
63
64 @Override
65 public int read()
66 throws IOException
67 {
68 if ( eofSeen )
69 {
70 return eofGame();
71 }
72 else if ( injectSlashN )
73 {
74 injectSlashN = false;
75 return '\n';
76 }
77 else
78 {
79 boolean prevWasSlashR = slashRSeen;
80 int target = readWithUpdate();
81 if ( eofSeen )
82 {
83 return eofGame();
84 }
85 if ( target == '\n' )
86 {
87 if ( !prevWasSlashR )
88 {
89 injectSlashN = true;
90 return '\r';
91 }
92 }
93 return target;
94 }
95 }
96
97 private int eofGame()
98 {
99 if ( !ensureLineFeedAtEndOfFile )
100 {
101 return -1;
102 }
103 if ( !slashNSeen && !slashRSeen )
104 {
105 slashRSeen = true;
106 return '\r';
107 }
108 if ( !slashNSeen )
109 {
110 slashRSeen = false;
111 slashNSeen = true;
112 return '\n';
113 }
114 else
115 {
116 return -1;
117 }
118 }
119
120 @Override
121 public void close()
122 throws IOException
123 {
124 super.close();
125 target.close();
126 }
127
128 @Override
129 public synchronized void mark( int readlimit )
130 {
131 throw new UnsupportedOperationException( "Mark not implemented yet" );
132 }
133 }