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 Project: Tomcat8-Source-Read   Author: chenmudu   File: TestInputBuffer.java    License: MIT License 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 #2
Source Project: Tomcat8-Source-Read   Author: chenmudu   File: TestB2CConverter.java    License: 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 #3
Source Project: warnings-ng-plugin   Author: jenkinsci   File: TaskScanner.java    License: 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 Project: Tomcat7.0.67   Author: tryandcatch   File: TestInputBuffer.java    License: 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 #5
Source Project: Tomcat7.0.67   Author: tryandcatch   File: TestB2CConverter.java    License: 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 #6
Source Project: hadoop   Author: naver   File: TestTextNonUTF8.java    License: 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 #7
Source Project: big-c   Author: yncxcw   File: TestTextNonUTF8.java    License: 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 #8
Source Project: tomcatsrc   Author: wangyingjie   File: TestInputBuffer.java    License: 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 #9
Source Project: tomcatsrc   Author: wangyingjie   File: TestB2CConverter.java    License: 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 Project: Tomcat8-Source-Read   Author: chenmudu   File: TestB2CConverter.java    License: MIT License 5 votes vote down vote up
@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);
}
 
Example #11
Source Project: Tomcat8-Source-Read   Author: chenmudu   File: TestB2CConverter.java    License: MIT License 5 votes vote down vote up
@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);
}
 
Example #12
Source Project: jdk1.8-source-analysis   Author: raysonfang   File: CodeSetConversion.java    License: Apache License 2.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 #13
Source Project: skara   Author: openjdk   File: AddedFileView.java    License: GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example #14
Source Project: dragonwell8_jdk   Author: alibaba   File: StreamTest.java    License: 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 #15
Source Project: dragonwell8_jdk   Author: alibaba   File: BytesAndLines.java    License: 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 #16
Source Project: dragonwell8_jdk   Author: alibaba   File: BytesAndLines.java    License: 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 #17
Source Project: TencentKona-8   Author: Tencent   File: CodeSetConversion.java    License: 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 #18
Source Project: TencentKona-8   Author: Tencent   File: StreamTest.java    License: 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 #19
Source Project: TencentKona-8   Author: Tencent   File: BytesAndLines.java    License: 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 #20
Source Project: TencentKona-8   Author: Tencent   File: BytesAndLines.java    License: 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 #21
Source Project: jdk8u60   Author: chenghanpeng   File: CodeSetConversion.java    License: 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 #22
Source Project: jdk8u60   Author: chenghanpeng   File: StreamTest.java    License: 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 Project: jdk8u60   Author: chenghanpeng   File: BytesAndLines.java    License: 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 #24
Source Project: jdk8u60   Author: chenghanpeng   File: BytesAndLines.java    License: 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 #25
Source Project: JDKSourceCode1.8   Author: wupeixuan   File: CodeSetConversion.java    License: MIT License 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 #26
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: CodeSetConversion.java    License: 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 #27
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: StreamTest.java    License: 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 Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: BytesAndLines.java    License: 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 #29
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: BytesAndLines.java    License: 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 #30
Source Project: Tomcat7.0.67   Author: tryandcatch   File: TestB2CConverter.java    License: 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);
}