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