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