java.nio.charset.MalformedInputException Java Examples

The following examples show how to use java.nio.charset.MalformedInputException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestInputBuffer.java    From Tomcat8-Source-Read with MIT License 7 votes vote down vote up
@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");
    }
}
 
Example #2
Source File: TestB2CConverter.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: TaskScanner.java    From warnings-ng-plugin with MIT License 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example #4
Source File: TestTextNonUTF8.java    From hadoop with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example #5
Source File: TestTextNonUTF8.java    From big-c with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example #6
Source File: TestInputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@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");
    }
}
 
Example #7
Source File: TestInputBuffer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@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");
    }
}
 
Example #8
Source File: TestB2CConverter.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@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);
}
 
Example #9
Source File: TestB2CConverter.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #10
Source File: BytesAndLines.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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) { }
}
 
Example #11
Source File: BytesAndLines.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
    Files.write(tmpfile, bytes);
    try {
        Files.readAllLines(tmpfile);
        fail("MalformedInputException expected");
    } catch (MalformedInputException ignore) { }
}
 
Example #12
Source File: BytesAndLines.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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) { }
}
 
Example #13
Source File: StreamTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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");
    }
}
 
Example #14
Source File: BytesAndLines.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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) { }
}
 
Example #15
Source File: BytesAndLines.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
    Files.write(tmpfile, bytes);
    try {
        Files.readAllLines(tmpfile);
        fail("MalformedInputException expected");
    } catch (MalformedInputException ignore) { }
}
 
Example #16
Source File: StreamTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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");
    }
}
 
Example #17
Source File: BytesAndLines.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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) { }
}
 
Example #18
Source File: BytesAndLines.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
    Files.write(tmpfile, bytes);
    try {
        Files.readAllLines(tmpfile);
        fail("MalformedInputException expected");
    } catch (MalformedInputException ignore) { }
}
 
Example #19
Source File: TestB2CConverter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #20
Source File: CodeSetConversion.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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 ) ;
    }
}
 
Example #21
Source File: BytesAndLines.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
    Files.write(tmpfile, bytes);
    try {
        Files.readAllLines(tmpfile);
        fail("MalformedInputException expected");
    } catch (MalformedInputException ignore) { }
}
 
Example #22
Source File: StreamTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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");
    }
}
 
Example #23
Source File: StreamTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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");
    }
}
 
Example #24
Source File: StringCoding.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static String newStringNoRepl(byte[] src, Charset cs) throws CharacterCodingException {
    try {
        return newStringNoRepl1(src, cs);
    } catch (IllegalArgumentException e) {
        //newStringNoRepl1 throws IAE with MalformedInputException or CCE as the cause
        Throwable cause = e.getCause();
        if (cause instanceof MalformedInputException) {
            throw (MalformedInputException)cause;
        }
        throw (CharacterCodingException)cause;
    }
}
 
Example #25
Source File: BytesAndLines.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
    Files.write(tmpfile, bytes);
    try {
        Files.readAllLines(tmpfile);
        fail("MalformedInputException expected");
    } catch (MalformedInputException ignore) { }
}
 
Example #26
Source File: BytesAndLines.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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) { }
}
 
Example #27
Source File: StreamTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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");
    }
}
 
Example #28
Source File: CodeSetConversion.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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 ) ;
    }
}
 
Example #29
Source File: BytesAndLines.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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) { }
}
 
Example #30
Source File: BytesAndLines.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
    Files.write(tmpfile, bytes);
    try {
        Files.readAllLines(tmpfile);
        fail("MalformedInputException expected");
    } catch (MalformedInputException ignore) { }
}