下面列出了怎么用java.nio.charset.UnmappableCharacterException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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;
}
}
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 ) ;
}
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException 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 ) ;
}
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException 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 ) ;
}
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException 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 ) ;
}
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException ignore) { }
}
public void testIncompatibleCharacter() throws Exception {
DataObject d = DataObject.find(testFileObject);
encodingName = "ISO-8859-1"; // NOI18N
EditorCookie o = d.getLookup().lookup(EditorCookie.class);
StyledDocument doc = o.openDocument();
doc.insertString(0, CZECH_STRING_UTF, null);
try {
o.saveDocument();
// try to open the file
InputStream istm = testFileObject.getInputStream();
try {
BufferedReader r = new BufferedReader(new InputStreamReader(istm, "ISO-8859-2")); // NOI18N
String line = r.readLine();
int questionMarkPos = line.indexOf('?'); // NOI18N
assertTrue("Should save question marks", questionMarkPos != -1); // NOI18N
} finally {
istm.close();
}
//fail("Exception expected");
} catch (UnmappableCharacterException ex) {
// expected exceptiom
}
}
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 ) ;
}
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException ignore) { }
}
static byte[] getBytesNoRepl(String s, Charset cs) throws CharacterCodingException {
try {
return getBytesNoRepl1(s, cs);
} catch (IllegalArgumentException e) {
//getBytesNoRepl1 throws IAE with UnmappableCharacterException or CCE as the cause
Throwable cause = e.getCause();
if (cause instanceof UnmappableCharacterException) {
throw (UnmappableCharacterException)cause;
}
throw (CharacterCodingException)cause;
}
}
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 ) ;
}
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException ignore) { }
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException 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 ) ;
}
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException 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 ) ;
}
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException 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 ) ;
}
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException ignore) { }
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException ignore) { }
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException ignore) { }
}
/**
* Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
*/
public void testWriteLines() throws IOException {
// zero lines
Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
assert(Files.size(tmpfile) == 0);
assert(result == tmpfile);
// two lines
List<String> lines = Arrays.asList("hi", "there");
Files.write(tmpfile, lines, US_ASCII);
List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(lines), "Unexpected lines");
// append two lines
Files.write(tmpfile, lines, US_ASCII, APPEND);
List<String> expected = new ArrayList<>();
expected.addAll(lines);
expected.addAll(lines);
assertTrue(expected.size() == 4, "List should have 4 elements");
actual = Files.readAllLines(tmpfile, US_ASCII);
assertTrue(actual.equals(expected), "Unexpected lines");
// UnmappableCharacterException
try {
String s = "\u00A0\u00A1";
Files.write(tmpfile, Arrays.asList(s), US_ASCII);
fail("UnmappableCharacterException expected");
} catch (UnmappableCharacterException ignore) { }
}
public void assertDeserialized(Serializable initial,
Serializable deserialized) {
// do common checks for all throwable objects
SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
deserialized);
UnmappableCharacterException initEx = (UnmappableCharacterException) initial;
UnmappableCharacterException desrEx = (UnmappableCharacterException) deserialized;
assertEquals("InputLength", initEx.getInputLength(), desrEx
.getInputLength());
}
public void testMultiStepEncode() throws CharacterCodingException {
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
try {
encoder.encode(CharBuffer.wrap("\ud800\udc00"));
fail("should unmappable");
} catch (UnmappableCharacterException e) {
}
encoder.reset();
}
public char[] getChars(byte[] bytes, int offset, int numBytes) {
// Possible optimization of reading directly from the CDR
// byte buffer. The sun.io converter supposedly can handle
// incremental conversions in which a char is broken across
// two convert calls.
//
// Basic tests didn't show more than a 1 ms increase
// worst case. It's less than a factor of 2 increase.
// Also makes the interface more difficult.
try {
ByteBuffer byteBuf = ByteBuffer.wrap(bytes, offset, numBytes);
CharBuffer charBuf = btc.decode(byteBuf);
// CharBuffer returned by the decoder will set its limit
// to byte immediately after the last written byte.
resultingNumChars = charBuf.limit();
// IMPORTANT - It's possible the underlying char[] in the
// CharBuffer returned by btc.decode(byteBuf)
// is longer in length than the number of characters
// decoded. Hence, the check below to ensure the
// char[] returned contains all the chars that have
// been decoded and no more.
if (charBuf.limit() == charBuf.capacity()) {
buffer = charBuf.array();
} else {
buffer = new char[charBuf.limit()];
charBuf.get(buffer, 0, charBuf.limit()).position(0);
}
return buffer;
} catch (IllegalStateException ile) {
// There were a decoding operation already in progress
throw wrapper.btcConverterFailure( ile ) ;
} 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 a character decoding error occured.
throw wrapper.btcConverterFailure( cce ) ;
}
}
public static void main(String[] args) throws IOException {
if (!isMacOSX) {
System.out.println("This test is for Mac OS X only. Passing.");
return;
}
String lang = System.getenv("LANG");
if (lang != null && !lang.contains("UTF-8")) {
System.out.println("LANG variable set to the language that " +
"does not support unicode, test passes vacuously");
return;
}
File sourceFile = new File(NAME_NFC + ".java");
String source = "public class " + NAME_NFC + " { " +
" public static void main(String args[]) {\n" +
" System.out.println(\"Success!\");\n" +
" }\n" +
"}\n";
ArrayList<String> content = new ArrayList<>();
content.add(source);
try {
createFile(sourceFile, content);
} catch (UnmappableCharacterException | InvalidPathException ipe) {
System.out.println("The locale or file system is configured in a way " +
"that prevents file creation. Real testing impossible.");
return;
}
HashMap<String, String> env = new HashMap<>();
env.put("LC_CTYPE", "UTF-8");
TestResult tr;
tr = doExec(env, javacCmd, NAME_NFD + ".java");
System.out.println(tr.testOutput);
if (!tr.isOK()) {
System.out.println(tr);
throw new RuntimeException("Compilation failed");
}
tr = doExec(env, javaCmd, "-cp", ".", NAME_NFD);
System.out.println(tr.testOutput);
if (!tr.isOK()) {
System.out.println(tr);
throw new RuntimeException("Test execution failed");
}
}