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