diff options
author | Julien Gouesse <[email protected]> | 2016-06-30 22:38:27 +0200 |
---|---|---|
committer | Julien Gouesse <[email protected]> | 2016-06-30 22:38:27 +0200 |
commit | c10e79d736e344956ce369f7198ceacb2123caf8 (patch) | |
tree | 1e3b77bc366227671fe3c9b08e515032a3c70609 /ardor3d-extras | |
parent | ced8e5e5a3439630b51a5aef53ef5dfbde69eb95 (diff) |
Adds a utility to check whether a file contains only US ASCII characters into ardor3d-extras
Diffstat (limited to 'ardor3d-extras')
-rw-r--r-- | ardor3d-extras/src/main/java/com/ardor3d/extension/model/util/FileHelper.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/ardor3d-extras/src/main/java/com/ardor3d/extension/model/util/FileHelper.java b/ardor3d-extras/src/main/java/com/ardor3d/extension/model/util/FileHelper.java new file mode 100644 index 0000000..5f8ae2b --- /dev/null +++ b/ardor3d-extras/src/main/java/com/ardor3d/extension/model/util/FileHelper.java @@ -0,0 +1,74 @@ +/** + * Copyright (c) 2008-2016 Ardor Labs, Inc. + * + * This file is part of Ardor3D. + * + * Ardor3D is free software: you can redistribute it and/or modify it + * under the terms of its license which may be found in the accompanying + * LICENSE file or at <http://www.ardor3d.com/LICENSE>. + */ + +package com.ardor3d.extension.model.util; + +import java.io.BufferedReader; +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.StandardCharsets; + +public class FileHelper { + + /** + * Tests whether or not the specified string is pure ASCII. Uses the method discussed at: + * + * <pre> + * http://www.rgagnon.com/javadetails/java-0536.html + * http://stackoverflow.com/questions/3585053/in-java-is-it-possible-to-check-if-a-string-is-only-ascii + * </pre> + * + * @param string + * String to test. + * @return Logical-true if pure ASCII, else logical-false. + */ + public boolean isStringPureAscii(final String string) { + final byte bytearray[] = string.getBytes(); + final CharsetDecoder d = StandardCharsets.US_ASCII.newDecoder(); + try { + final CharBuffer r = d.decode(ByteBuffer.wrap(bytearray)); + r.toString(); + } catch (final CharacterCodingException e) { + return false; + } + return true; + } + + /** + * Tests whether or not the file with the specified filename is pure ASCII. The method used is to read the file a + * line at a time and test if each line is ASCII. + * + * @param filename + * File name. + * @return Logical-true if pure ASCII, else logical-false. + */ + public boolean isFilePureAscii(final String filename) { + try (final FileInputStream fstream = new FileInputStream(filename); + final DataInputStream in = new DataInputStream(fstream); + final BufferedReader br = new BufferedReader(new InputStreamReader(in))) { + String strLine; + // read file a line at a time + while ((strLine = br.readLine()) != null) { + final boolean isAscii = isStringPureAscii(strLine); + if (!isAscii) { + return false; + } + } + } catch (final Exception e) { + return false; + } + return true; + } +} |