本文整理汇总了Java中java.nio.charset.MalformedInputException类的典型用法代码示例。如果您正苦于以下问题:Java MalformedInputException类的具体用法?Java MalformedInputException怎么用?Java MalformedInputException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MalformedInputException类属于java.nio.charset包,在下文中一共展示了MalformedInputException类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doPost
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
Reader r = req.getReader();
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/plain");
Writer w = resp.getWriter();
try {
// Copy one character at a time
int c = r.read();
while (c != -1) {
w.write(c);
c = r.read();
}
w.close();
} catch (MalformedInputException mie) {
resp.resetBuffer();
w.write("FAILED");
}
}
示例2: testParseFalseEncodedFile
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
public void testParseFalseEncodedFile() throws IOException {
Path tempDir = createTempDir();
Path dict = tempDir.resolve("foo.dict");
Settings nodeSettings = Settings.builder()
.put("foo.bar_path", dict)
.put(Environment.PATH_HOME_SETTING.getKey(), tempDir).build();
try (OutputStream writer = Files.newOutputStream(dict)) {
writer.write(new byte[]{(byte) 0xff, 0x00, 0x00}); // some invalid UTF-8
writer.write('\n');
}
Environment env = new Environment(nodeSettings);
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class,
() -> Analysis.getWordList(env, nodeSettings, "foo.bar"));
assertEquals("Unsupported character encoding detected while reading foo.bar_path: " + tempDir.resolve("foo.dict").toString()
+ " - files must be UTF-8 encoded" , ex.getMessage());
assertTrue(ex.getCause().toString(), ex.getCause() instanceof MalformedInputException
|| ex.getCause() instanceof CharacterCodingException);
}
示例3: testNonUTF8
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
@Test
public void testNonUTF8() throws Exception{
// this is a non UTF8 byte array
byte b[] = {-0x01, -0x01, -0x01, -0x01, -0x01, -0x01, -0x01};
boolean nonUTF8 = false;
Text t = new Text(b);
try{
Text.validateUTF8(b);
}catch(MalformedInputException me){
nonUTF8 = false;
}
// asserting that the byte array is non utf8
assertFalse(nonUTF8);
byte ret[] = t.getBytes();
// asseting that the byte array are the same when the Text
// object is created.
assertTrue(Arrays.equals(b, ret));
}
示例4: testBug54602c
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
@Test
public void testBug54602c() throws Exception {
// Check partial input is rejected once it is known to be all available
B2CConverter conv = new B2CConverter("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);
}
示例5: testNonUTF8
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
public void testNonUTF8() throws Exception{
// this is a non UTF8 byte array
byte b[] = {-0x01, -0x01, -0x01, -0x01, -0x01, -0x01, -0x01};
boolean nonUTF8 = false;
Text t = new Text(b);
try{
Text.validateUTF8(b);
}catch(MalformedInputException me){
nonUTF8 = false;
}
// asserting that the byte array is non utf8
assertFalse(nonUTF8);
byte ret[] = t.getBytes();
// asseting that the byte array are the same when the Text
// object is created.
assertTrue(Arrays.equals(b, ret));
}
示例6: fillBuffer
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
/**
* Decode bytes from {@link #byteBuf} and fill {@link #charBuf}. This method
* is a no-op if {@link #charBuf} is not empty or if there are no bytes to
* decode.
* @return true if the buffer contains bytes now, false if it's still empty
* @throws CharacterCodingException if the input data contains invalid
* characters
*/
private boolean fillBuffer() throws CharacterCodingException {
if (charBuf.hasRemaining()) {
return true;
}
if (byteBuf.position() == 0) {
return false;
}
charBuf.position(0);
charBuf.limit(charBuf.capacity());
byteBuf.flip();
CoderResult result = decoder.decode(byteBuf, charBuf, done);
if (result.isMalformed()) {
throw new MalformedInputException(result.length());
}
if (result.isUnmappable()) {
throw new UnmappableCharacterException(result.length());
}
charBuf.flip();
byteBuf.compact();
return charBuf.hasRemaining();
}
示例7: exceptionCaught
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
public void exceptionCaught(final FtpIoSession session,
final Throwable cause) throws Exception {
if(cause instanceof ProtocolDecoderException &&
cause.getCause() instanceof MalformedInputException) {
// client probably sent something which is not UTF-8 and we failed to
// decode it
LOG.warn(
"Client sent command that could not be decoded: {}",
((ProtocolDecoderException)cause).getHexdump());
session.write(new DefaultFtpReply(FtpReply.REPLY_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS, "Invalid character in command"));
} else if (cause instanceof WriteToClosedSessionException) {
WriteToClosedSessionException writeToClosedSessionException =
(WriteToClosedSessionException) cause;
LOG.warn(
"Client closed connection before all replies could be sent, last reply was {}",
writeToClosedSessionException.getRequest());
session.close(false).awaitUninterruptibly(10000);
} else {
LOG.error("Exception caught, closing session", cause);
session.close(false).awaitUninterruptibly(10000);
}
}
示例8: testBug54602b
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
@Test(expected=MalformedInputException.class)
public void testBug54602b() throws Exception {
// Check partial input is rejected
B2CConverter conv = new B2CConverter("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, true);
}
示例9: testUtf8DecodeErrorHandlingFailMalformed
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
@Test(expected = MalformedInputException.class)
public void testUtf8DecodeErrorHandlingFailMalformed() throws IOException {
ResettableInputStream in = initUtf8DecodeTest(DecodeErrorPolicy.FAIL);
while (in.readChar() != -1) {
// Do nothing... read the whole file and throw away the bytes.
}
fail("Expected MalformedInputException!");
}
示例10: testLatin1DecodeErrorHandlingFailMalformed
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
@Test(expected = MalformedInputException.class)
public void testLatin1DecodeErrorHandlingFailMalformed() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
generateLatin1InvalidSequence(out);
Files.write(out.toByteArray(), file);
ResettableInputStream in = initInputStream(DecodeErrorPolicy.FAIL);
while (in.readChar() != -1) {
// Do nothing... read the whole file and throw away the bytes.
}
fail("Expected MalformedInputException!");
}
示例11: convertCharArray
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
private void convertCharArray() {
try {
// Possible optimization of directly converting into the CDR buffer.
// However, that means the CDR code would have to reserve
// a 4 byte string length ahead of time, and we'd need a
// confusing partial conversion scheme for when we couldn't
// fit everything in the buffer but needed to know the
// converted length before proceeding due to fragmentation.
// Then there's the issue of the chunking code.
//
// For right now, this is less messy and basic tests don't
// show more than a 1 ms penalty worst case. Less than a
// factor of 2 increase.
// Convert the characters
buffer = ctb.encode(CharBuffer.wrap(chars,0,numChars));
// ByteBuffer returned by the encoder will set its limit
// to byte immediately after the last written byte.
numBytes = buffer.limit();
} catch (IllegalStateException ise) {
// an encoding operation is already in progress
throw wrapper.ctbConverterFailure( ise ) ;
} catch (MalformedInputException mie) {
// There were illegal Unicode char pairs
throw wrapper.badUnicodePair( mie ) ;
} catch (UnmappableCharacterException uce) {
// A character doesn't map to the desired code set
// CORBA formal 00-11-03.
throw omgWrapper.charNotInCodeset( uce ) ;
} catch (CharacterCodingException cce) {
// If this happens, then some other encoding error occured
throw wrapper.ctbConverterFailure( cce ) ;
}
}
示例12: checkMalformedInputException
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
private void checkMalformedInputException(Stream<String> s) {
try {
List<String> lines = s.collect(Collectors.toList());
fail("UncheckedIOException expected");
} catch (UncheckedIOException ex) {
IOException cause = ex.getCause();
assertTrue(cause instanceof MalformedInputException,
"MalformedInputException expected");
}
}
示例13: testReadAllLines
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
/**
* Exercise Files.readAllLines(Path, Charset)
*/
public void testReadAllLines() throws IOException {
// zero lines
Files.write(tmpfile, new byte[0]);
List<String> lines = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(lines.isEmpty(), "No line expected");
// one line
byte[] hi = { (byte)'h', (byte)'i' };
Files.write(tmpfile, hi);
lines = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(lines.size() == 1, "One line expected");
assertTrue(lines.get(0).equals("hi"), "'Hi' expected");
// two lines using platform's line separator
List<String> expected = Arrays.asList("hi", "there");
Files.write(tmpfile, expected, US_ASCII);
assertTrue(Files.size(tmpfile) > 0, "File is empty");
lines = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(lines.equals(expected), "Unexpected lines");
// MalformedInputException
byte[] bad = { (byte)0xff, (byte)0xff };
Files.write(tmpfile, bad);
try {
Files.readAllLines(tmpfile, US_ASCII);
fail("MalformedInputException expected");
} catch (MalformedInputException ignore) { }
}
示例14: testReadAllLinesMalformedUTF8
import java.nio.charset.MalformedInputException; //导入依赖的package包/类
private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
Files.write(tmpfile, bytes);
try {
Files.readAllLines(tmpfile);
fail("MalformedInputException expected");
} catch (MalformedInputException ignore) { }
}