1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.utils.text;
18
19 import java.io.BufferedReader;
20 import java.io.BufferedWriter;
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.FileWriter;
24 import java.io.IOException;
25
26 import org.apache.commons.io.IOUtils;
27
28
29
30
31
32
33
34 public final class EOLUtils {
35
36
37
38
39 private static final int BUF_SIZE = 65536;
40
41
42 public static final int UNIX = 0;
43
44 public static final int DOS = 1;
45
46 public static final int MAC = 2;
47
48 public static final int UNDEFINED = 3;
49
50
51
52
53
54 public static final String[] SCHEMENAMES = {"UNIX", "DOS", "MAC", "UNDEFINED"};
55
56
57
58
59 private static final String[] EOLS = {"\n", "\r\n", "\r", ""};
60
61
62
63
64 private EOLUtils() {
65 }
66
67
68
69
70
71
72
73 public static int detectEOL(String source) {
74
75 int scheme = UNDEFINED;
76 if (source != null) {
77 if (source.indexOf(EOLS[DOS]) != -1) {
78 scheme = DOS;
79 } else if (source.indexOf(EOLS[MAC]) != -1) {
80 scheme = MAC;
81 } else if (source.indexOf(EOLS[UNIX]) != -1) {
82 scheme = UNIX;
83 }
84 }
85 return scheme;
86 }
87
88
89
90
91
92
93
94
95
96 public static int detectEOL(File source) throws IOException {
97 if (source == null || !source.exists()) {
98 return UNDEFINED;
99 }
100
101 BufferedReader br = null;
102 try {
103 br = new BufferedReader(new FileReader(source));
104 char[] buffer = new char[BUF_SIZE];
105 int read = br.read(buffer);
106
107 if (read > 0) {
108 return detectEOL(String.valueOf(buffer, 0, read));
109 } else {
110 return UNDEFINED;
111 }
112 } finally {
113 IOUtils.closeQuietly(br);
114 }
115 }
116
117
118
119
120
121
122
123
124
125 public static String applyEOL(String source, int scheme) {
126 int baseScheme = detectEOL(source);
127 if (baseScheme != UNDEFINED && scheme != baseScheme) {
128
129 return source.replaceAll(EOLS[baseScheme], EOLS[scheme]);
130 } else {
131 return source;
132 }
133 }
134
135
136
137
138
139
140
141
142
143 public static void applyEOL(File source, int scheme) throws IOException {
144 if (source == null || !source.exists()) {
145 return;
146 }
147
148 String eol = EOLS[scheme];
149
150 boolean eolDetected = false;
151
152 StringBuffer sb = new StringBuffer();
153 BufferedReader br = null;
154 try {
155 br = new BufferedReader(new FileReader(source));
156 int c = br.read();
157
158 while (c != -1) {
159 if (eolDetected) {
160 eolDetected = (c == '\r');
161 sb.append(eol);
162 if (c != '\n' && c != '\r') {
163 sb.append((char)c);
164 }
165 } else {
166 if (c == '\r') {
167 eolDetected = true;
168 } else if (c == '\n') {
169 sb.append(eol);
170 } else {
171 sb.append((char)c);
172 }
173 }
174 c = br.read();
175 }
176 if (eolDetected) {
177 sb.append(eol);
178 }
179 } finally {
180 IOUtils.closeQuietly(br);
181 }
182
183 BufferedWriter bw = null;
184 try {
185 bw = new BufferedWriter(new FileWriter(source));
186 bw.write(sb.toString());
187 } finally {
188 IOUtils.closeQuietly(bw);
189 }
190 }
191 }