下面列出了怎么用java.nio.charset.MalformedInputException的API类实例代码及写法,或者点击链接到github查看源代码。
@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");
}
}
@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);
}
/**
* Scans the specified file for open tasks.
*
* @param file
* the file to scan
* @param charset
* encoding of the file
*
* @return the open tasks
*/
public Report scan(final Path file, final Charset charset) {
try (Stream<String> lines = Files.lines(file, charset)) {
return scanTasks(lines.iterator(), new IssueBuilder().setFileName(file.toString()));
}
catch (IOException | UncheckedIOException exception) {
Report report = new Report();
Throwable cause = exception.getCause();
if (cause instanceof MalformedInputException || cause instanceof UnmappableCharacterException) {
report.logError("Can't read source file '%s', defined encoding '%s' seems to be wrong",
file, charset);
}
else {
report.logException(exception, "Exception while reading the source code file '%s':", file);
}
return report;
}
}
@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");
}
}
@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);
}
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));
}
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));
}
@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");
}
}
@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);
}
@Test(expected=MalformedInputException.class)
public void testBug54602a() throws Exception {
// Check invalid input is rejected straight away
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_8);
ByteChunk bc = new ByteChunk();
CharChunk cc = new CharChunk();
bc.append(UTF8_INVALID, 0, UTF8_INVALID.length);
cc.allocate(bc.getLength(), -1);
conv.convert(bc, cc, false);
}
@Test(expected=MalformedInputException.class)
public void testBug54602b() throws Exception {
// Check partial input is rejected
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, true);
}
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 ) ;
}
}
public AddedFileView(ReadOnlyRepository repo, Hash base, Hash head, List<CommitMetadata> commits, MetadataFormatter formatter, Patch patch, Path out) throws IOException {
this.patch = patch;
this.out = out;
this.commits = commits;
this.formatter = formatter;
var path = patch.target().path().get();
var pathInRepo = repo.root().resolve(path);
if (patch.isTextual()) {
binaryContent = null;
if (head == null) {
List<String> lines = null;
for (var charset : List.of(StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1)) {
try {
lines = Files.readAllLines(pathInRepo, charset);
break;
} catch (MalformedInputException e) {
continue;
}
}
if (lines == null) {
throw new IllegalStateException("Could not read " + pathInRepo + " as UTF-8 nor as ISO-8859-1");
}
newContent = lines;
} else {
newContent = repo.lines(path, head).orElseThrow(IllegalArgumentException::new);
}
stats = new WebrevStats(patch.asTextualPatch().stats(), newContent.size());
} else {
newContent = null;
if (head == null) {
binaryContent = Files.readAllBytes(pathInRepo);
} else {
binaryContent = repo.show(path, head).orElseThrow(IllegalArgumentException::new);
}
stats = WebrevStats.empty();
}
}
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");
}
}
/**
* 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) { }
}
private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
Files.write(tmpfile, bytes);
try {
Files.readAllLines(tmpfile);
fail("MalformedInputException expected");
} catch (MalformedInputException ignore) { }
}
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 ) ;
}
}
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");
}
}
/**
* 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) { }
}
private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
Files.write(tmpfile, bytes);
try {
Files.readAllLines(tmpfile);
fail("MalformedInputException expected");
} catch (MalformedInputException ignore) { }
}
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 ) ;
}
}
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");
}
}
/**
* 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) { }
}
private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
Files.write(tmpfile, bytes);
try {
Files.readAllLines(tmpfile);
fail("MalformedInputException expected");
} catch (MalformedInputException ignore) { }
}
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 ) ;
}
}
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 ) ;
}
}
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");
}
}
/**
* 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) { }
}
private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
Files.write(tmpfile, bytes);
try {
Files.readAllLines(tmpfile);
fail("MalformedInputException expected");
} catch (MalformedInputException ignore) { }
}
@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);
}