use of java.nio.charset.MalformedInputException in project zaproxy by zaproxy.
the class ExtensionScript method postInit.
@Override
public void postInit() {
final List<String[]> scriptsNotAdded = new ArrayList<>(1);
for (ScriptWrapper script : this.getScriptParam().getScripts()) {
try {
this.loadScript(script);
if (script.getType() != null) {
this.addScript(script, false);
} else {
logger.warn("Failed to add script \"" + script.getName() + "\", script type not found: " + script.getTypeName());
scriptsNotAdded.add(new String[] { script.getName(), script.getEngineName(), Constant.messages.getString("script.info.scriptsNotAdded.error.missingType", script.getTypeName()) });
}
} catch (MalformedInputException e) {
logger.warn("Failed to add script \"" + script.getName() + "\", contains invalid character sequence (UTF-8).");
scriptsNotAdded.add(new String[] { script.getName(), script.getEngineName(), Constant.messages.getString("script.info.scriptsNotAdded.error.invalidChars") });
} catch (InvalidParameterException | IOException e) {
logger.error(e.getMessage(), e);
scriptsNotAdded.add(new String[] { script.getName(), script.getEngineName(), Constant.messages.getString("script.info.scriptsNotAdded.error.other") });
}
}
informScriptsNotAdded(scriptsNotAdded);
this.loadTemplates();
for (File dir : this.getScriptParam().getScriptDirs()) {
// Load the scripts from subdirectories of each directory configured
int numAdded = addScriptsFromDir(dir);
logger.debug("Added " + numAdded + " scripts from dir: " + dir.getAbsolutePath());
}
shouldLoadTemplatesOnScriptTypeRegistration = true;
}
use of java.nio.charset.MalformedInputException in project jgnash by ccavanaugh.
the class FileMagic method isOfxV2.
public static boolean isOfxV2(final Path path) {
boolean result = false;
if (Files.exists(path)) {
try (final BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
String line = reader.readLine();
/* Assume that the OFX file is one continuous string of information when searching for OFXHEADER */
while (line != null) {
line = line.trim();
// consume any processing instructions and check for ofx 2.0 hints
if (!line.isEmpty() && line.startsWith("<?")) {
if (line.contains("OFXHEADER=\"200\"")) {
result = true;
break;
}
} else if (!line.isEmpty()) {
// allow empty lines at the beginning of the file
if (line.startsWith("<OFX>")) {
//must be ofx
result = true;
}
break;
}
line = reader.readLine();
}
} catch (final MalformedInputException mie) {
// caused by binary file
result = false;
Logger.getLogger(FileMagic.class.getName()).info("Tried to read a binary file");
} catch (IOException e) {
Logger.getLogger(FileMagic.class.getName()).log(Level.SEVERE, e.toString(), e);
}
}
return result;
}
use of java.nio.charset.MalformedInputException in project jgnash by ccavanaugh.
the class FileMagic method getOfxV1Encoding.
/**
* Determine the correct character encoding of an OFX Version 1 file.
*
* @param path File to look at
* @return encoding of the file
*/
private static String getOfxV1Encoding(final Path path, final Charset charset) throws MalformedInputException {
String encoding = null;
String characterSet = null;
if (Files.exists(path)) {
try (final BufferedReader reader = Files.newBufferedReader(path, charset)) {
String line = reader.readLine();
while (line != null) {
line = line.trim();
if (!line.isEmpty()) {
// allow empty lines at the beginning of the file
if (line.startsWith("ENCODING:")) {
String[] splits = COLON_DELIMITER_PATTERN.split(line);
if (splits.length == 2) {
encoding = splits[1];
}
} else if (line.startsWith("CHARSET:")) {
String[] splits = COLON_DELIMITER_PATTERN.split(line);
if (splits.length == 2) {
characterSet = splits[1];
}
}
if (encoding != null && characterSet != null) {
break;
}
}
line = reader.readLine();
}
} catch (final MalformedInputException e) {
// rethrow
throw e;
} catch (final IOException e) {
Logger.getLogger(FileMagic.class.getName()).log(Level.SEVERE, e.toString(), e);
}
}
if (encoding != null && characterSet != null) {
if (encoding.equals(StandardCharsets.UTF_8.name()) && characterSet.equals("CSUNICODE")) {
return StandardCharsets.ISO_8859_1.name();
} else if (encoding.equals(StandardCharsets.UTF_8.name())) {
return StandardCharsets.UTF_8.name();
} else if (encoding.equals(USASCII) && characterSet.equals("1252")) {
return WINDOWS_1252;
} else if (encoding.equals(USASCII) && characterSet.contains("8859-1")) {
return "ISO-8859-1";
} else if (encoding.equals(USASCII) && characterSet.equals("NONE")) {
return WINDOWS_1252;
}
}
return WINDOWS_1252;
}
use of java.nio.charset.MalformedInputException in project tomcat by apache.
the class TestB2CConverter method testBug54602c.
@Test
public void testBug54602c() throws Exception {
// Check partial input is rejected once it is known to be all available
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_8);
ByteChunk bc = new ByteChunk();
CharChunk cc = new CharChunk();
bc.append(UTF8_PARTIAL, 0, UTF8_PARTIAL.length);
cc.allocate(bc.getLength(), -1);
conv.convert(bc, cc, false);
Exception e = null;
try {
conv.convert(bc, cc, true);
} catch (MalformedInputException mie) {
e = mie;
}
Assert.assertNotNull(e);
}
use of java.nio.charset.MalformedInputException in project hadoop by apache.
the class Text method validateUTF8.
/**
* Check to see if a byte array is valid utf-8
* @param utf8 the array of bytes
* @param start the offset of the first byte in the array
* @param len the length of the byte sequence
* @throws MalformedInputException if the byte array contains invalid bytes
*/
public static void validateUTF8(byte[] utf8, int start, int len) throws MalformedInputException {
int count = start;
int leadByte = 0;
int length = 0;
int state = LEAD_BYTE;
while (count < start + len) {
int aByte = utf8[count] & 0xFF;
switch(state) {
case LEAD_BYTE:
leadByte = aByte;
length = bytesFromUTF8[aByte];
switch(length) {
case // check for ASCII
0:
if (leadByte > 0x7F)
throw new MalformedInputException(count);
break;
case 1:
if (leadByte < 0xC2 || leadByte > 0xDF)
throw new MalformedInputException(count);
state = TRAIL_BYTE_1;
break;
case 2:
if (leadByte < 0xE0 || leadByte > 0xEF)
throw new MalformedInputException(count);
state = TRAIL_BYTE_1;
break;
case 3:
if (leadByte < 0xF0 || leadByte > 0xF4)
throw new MalformedInputException(count);
state = TRAIL_BYTE_1;
break;
default:
// or if < 0 we got a trail byte in the lead byte position
throw new MalformedInputException(count);
}
// switch (length)
break;
case TRAIL_BYTE_1:
if (leadByte == 0xF0 && aByte < 0x90)
throw new MalformedInputException(count);
if (leadByte == 0xF4 && aByte > 0x8F)
throw new MalformedInputException(count);
if (leadByte == 0xE0 && aByte < 0xA0)
throw new MalformedInputException(count);
if (leadByte == 0xED && aByte > 0x9F)
throw new MalformedInputException(count);
// falls through to regular trail-byte test!!
case TRAIL_BYTE:
if (aByte < 0x80 || aByte > 0xBF)
throw new MalformedInputException(count);
if (--length == 0) {
state = LEAD_BYTE;
} else {
state = TRAIL_BYTE;
}
break;
default:
break;
}
// switch (state)
count++;
}
}
Aggregations