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