|
3 | 3 | import com.codeforces.commons.collection.MapBuilder;
|
4 | 4 | import com.codeforces.commons.holder.Holders;
|
5 | 5 | import com.codeforces.commons.io.FileUtil;
|
6 |
| -import com.codeforces.commons.io.IoUtil; |
7 | 6 | import com.codeforces.commons.pair.*;
|
8 | 7 | import com.codeforces.commons.properties.internal.CommonsPropertiesUtil;
|
9 | 8 | import com.codeforces.commons.reflection.ReflectionUtil;
|
10 | 9 | import com.codeforces.commons.text.similarity.SimilarityChecker;
|
11 | 10 | import org.apache.commons.codec.binary.Base64;
|
12 | 11 | import org.apache.commons.codec.binary.Hex;
|
13 | 12 | import org.apache.commons.codec.digest.DigestUtils;
|
| 13 | +import org.apache.commons.io.FileUtils; |
14 | 14 | import org.apache.commons.lang3.ArrayUtils;
|
15 | 15 | import org.apache.commons.lang3.mutable.MutableBoolean;
|
16 | 16 | import org.apache.commons.text.translate.CharSequenceTranslator;
|
|
36 | 36 | import java.awt.event.KeyEvent;
|
37 | 37 | import java.awt.font.FontRenderContext;
|
38 | 38 | import java.awt.geom.Rectangle2D;
|
39 |
| -import java.io.*; |
| 39 | +import java.io.ByteArrayOutputStream; |
| 40 | +import java.io.File; |
| 41 | +import java.io.IOException; |
| 42 | +import java.io.StringReader; |
40 | 43 | import java.lang.reflect.Array;
|
41 | 44 | import java.nio.charset.StandardCharsets;
|
42 | 45 | import java.security.InvalidKeyException;
|
43 | 46 | import java.security.MessageDigest;
|
44 | 47 | import java.security.NoSuchAlgorithmException;
|
45 |
| -import java.util.List; |
46 | 48 | import java.util.*;
|
| 49 | +import java.util.List; |
47 | 50 | import java.util.concurrent.locks.Lock;
|
48 | 51 | import java.util.concurrent.locks.ReadWriteLock;
|
49 | 52 | import java.util.concurrent.locks.ReentrantReadWriteLock;
|
@@ -1206,25 +1209,46 @@ private static String wellformSingleLineForWindows(String line) {
|
1206 | 1209 | return trim(sb.toString());
|
1207 | 1210 | }
|
1208 | 1211 |
|
| 1212 | + /** |
| 1213 | + * Removes all \r if it really looks like windows encoded text file. |
| 1214 | + * |
| 1215 | + * @param file File to process |
| 1216 | + * @throws IOException On any IO error |
| 1217 | + */ |
1209 | 1218 | public static void convertFileToLinuxStyle(File file) throws IOException {
|
1210 |
| - byte[] bytes = FileUtil.getBytes(file); |
1211 |
| - BufferedReader reader = null; |
1212 |
| - BufferedWriter writer = null; |
| 1219 | + byte[] content = FileUtil.getBytes(file); |
1213 | 1220 |
|
1214 |
| - try { |
1215 |
| - reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes), UTF_8)); |
1216 |
| - writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), UTF_8)); |
| 1221 | + boolean validCrLf = true; |
| 1222 | + int crCount = 0; |
| 1223 | + |
| 1224 | + for (int i = 0; i < content.length; i++) { |
| 1225 | + if (content[i] == 0x0D) { // \r |
| 1226 | + crCount++; |
| 1227 | + if (i + 1 >= content.length || content[i + 1] != 0x0A) { |
| 1228 | + validCrLf = false; |
| 1229 | + break; |
| 1230 | + } |
| 1231 | + } else if (content[i] == 0x0A) { // \n |
| 1232 | + if (i == 0 || content[i - 1] != 0x0D) { |
| 1233 | + validCrLf = false; |
| 1234 | + break; |
| 1235 | + } |
| 1236 | + } |
| 1237 | + } |
1217 | 1238 |
|
1218 |
| - String line; |
1219 |
| - while ((line = reader.readLine()) != null) { |
1220 |
| - writer.write(line); |
1221 |
| - writer.write(10); |
| 1239 | + if (validCrLf && crCount > 0) { |
| 1240 | + // Remove all \r (0x0D) bytes |
| 1241 | + ByteArrayOutputStream out = new ByteArrayOutputStream(content.length - crCount); |
| 1242 | + for (byte b : content) { |
| 1243 | + if (b != 0x0D) { |
| 1244 | + out.write(b); |
| 1245 | + } |
1222 | 1246 | }
|
1223 |
| - } finally { |
1224 |
| - IoUtil.closeQuietly(reader, writer); |
| 1247 | + FileUtils.writeByteArrayToFile(file, out.toByteArray()); |
1225 | 1248 | }
|
1226 | 1249 | }
|
1227 | 1250 |
|
| 1251 | + |
1228 | 1252 | /**
|
1229 | 1253 | * @param s Given string.
|
1230 | 1254 | * @param maxLength Maximal length.
|
|
0 commit comments