diff --git a/src/com/belkast/soap/fileProcessor.java b/src/com/belkast/soap/fileProcessor.java index 392b3fb..803a374 100644 --- a/src/com/belkast/soap/fileProcessor.java +++ b/src/com/belkast/soap/fileProcessor.java @@ -2,10 +2,13 @@ package com.belkast.soap; import java.io.*; import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; +import java.nio.charset.CodingErrorAction; public class fileProcessor { @@ -72,26 +75,38 @@ public class fileProcessor { return returnString; } - public static Boolean validFile(String myFile) { + public static boolean validFile(String thisFile) + { + Path path = Paths.get(thisFile); + boolean result = true; + long total = 0; + InputStreamReader isr = null; + char[] buf = new char[65536]; + int chars; - Path path = Paths.get(myFile); - try (Reader reader = Files.newBufferedReader(path)) { - int c = reader.read(); - if (c == 0xfeff) - { -/* System.out.println("File starts with a byte order mark."); */ - } - else if (c >= 0) - { - reader.transferTo(Writer.nullWriter()); - } - } catch (CharacterCodingException e) { - return false; - } catch (IOException e) { - return false; -// throw new RuntimeException(e); + final CharsetDecoder decoder; + String charsetName = "UTF-8"; + Charset charset = Charset.forName(charsetName); + decoder = charset.newDecoder(); + decoder.onMalformedInput(CodingErrorAction.REPORT); + + try { + isr = new InputStreamReader(new FileInputStream(thisFile), decoder); + while ((chars = isr.read(buf)) >= 0) + total += chars; + } catch (CharacterCodingException ex) { + result = false; + } catch (IOException ex) { + result = false; + } finally { + if (isr != null) + try { + isr.close(); + } catch (IOException ex) { + System.err.println("Failed to close file: " + ex.toString()); + } } - return true; + return result; } public static long numberOfLines(String fileName)