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