Refactored the validFile method

Method is now Java 1.8 compatible
This commit is contained in:
Keith Armstrong 2025-01-07 15:45:55 -05:00
parent 712ddc9421
commit 7ea1f88238

View File

@ -2,10 +2,13 @@ package com.belkast.soap;
import java.io.*; import java.io.*;
import java.nio.charset.CharacterCodingException; import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.*;
import java.nio.charset.CodingErrorAction;
public class fileProcessor { public class fileProcessor {
@ -72,26 +75,38 @@ public class fileProcessor {
return returnString; 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); final CharsetDecoder decoder;
try (Reader reader = Files.newBufferedReader(path)) { String charsetName = "UTF-8";
int c = reader.read(); Charset charset = Charset.forName(charsetName);
if (c == 0xfeff) decoder = charset.newDecoder();
{ decoder.onMalformedInput(CodingErrorAction.REPORT);
/* System.out.println("File starts with a byte order mark."); */
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());
} }
else if (c >= 0)
{
reader.transferTo(Writer.nullWriter());
} }
} catch (CharacterCodingException e) { return result;
return false;
} catch (IOException e) {
return false;
// throw new RuntimeException(e);
}
return true;
} }
public static long numberOfLines(String fileName) public static long numberOfLines(String fileName)