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.BufferedReader;
22 import java.io.BufferedWriter;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.OutputStreamWriter;
28 import java.io.RandomAccessFile;
29 import java.nio.file.Files;
30
31 import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
32
33
34
35
36 public final class LineEndingsUtils {
37
38 private LineEndingsUtils() {
39
40 }
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public static void convertLineEndings(
57 final File source, File dest, LineEndings lineEndings, final Boolean atEndOfFile, String encoding)
58 throws IOException {
59
60
61
62
63 String eofChars = "";
64 if (atEndOfFile == null) {
65 if (source.length() >= 1) {
66 try (RandomAccessFile raf = new RandomAccessFile(source, "r")) {
67 raf.seek(source.length() - 1);
68 byte last = raf.readByte();
69 if (last == '\n') {
70 eofChars = lineEndings.getLineEndingCharacters();
71 }
72 }
73 }
74 } else if (atEndOfFile) {
75 eofChars = lineEndings.getLineEndingCharacters();
76 }
77
78 try (BufferedReader in = getBufferedReader(source, encoding);
79 BufferedWriter out = getBufferedWriter(dest, encoding)) {
80 String line = in.readLine();
81 while (line != null) {
82 out.write(line);
83 line = in.readLine();
84 if (line != null) {
85 out.write(lineEndings.getLineEndingCharacters());
86 } else {
87 out.write(eofChars);
88 }
89 }
90 }
91 }
92
93 private static BufferedReader getBufferedReader(File source, String encoding) throws IOException {
94 if (encoding == null) {
95
96 return new BufferedReader(new InputStreamReader(Files.newInputStream(source.toPath())));
97 } else {
98
99 return new BufferedReader(new InputStreamReader(Files.newInputStream(source.toPath()), encoding));
100 }
101 }
102
103 private static BufferedWriter getBufferedWriter(File dest, String encoding) throws IOException {
104 if (encoding == null) {
105
106 return new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(dest.toPath())));
107 } else {
108
109 return new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(dest.toPath()), encoding));
110 }
111 }
112
113
114
115
116
117
118
119
120
121 @SuppressWarnings("resource")
122 public static InputStream lineEndingConverter(InputStream in, LineEndings lineEndings) throws IOException {
123 return lineEndings.isNewLine()
124 ? new LinuxLineFeedInputStream(in, false)
125 : lineEndings.isCrLF() ? new WindowsLineFeedInputStream(in, false) : in;
126 }
127
128 public static LineEndings getLineEnding( String lineEnding) throws AssemblyFormattingException {
129 LineEndings result = LineEndings.keep;
130 if (lineEnding != null) {
131 try {
132 result = LineEndings.valueOf(lineEnding);
133 } catch (IllegalArgumentException e) {
134 throw new AssemblyFormattingException("Illegal lineEnding specified: '" + lineEnding + "'", e);
135 }
136 }
137 return result;
138 }
139
140
141
142
143
144
145
146
147
148 public static String getLineEndingCharacters( String lineEnding) throws AssemblyFormattingException {
149 String value = lineEnding;
150
151 if (lineEnding != null) {
152 try {
153 value = LineEndings.valueOf(lineEnding).getLineEndingCharacters();
154 } catch (IllegalArgumentException e) {
155 throw new AssemblyFormattingException("Illegal lineEnding specified: '" + lineEnding + "'", e);
156 }
157 }
158
159 return value;
160 }
161 }