use of java.nio.charset.MalformedInputException in project j2objc by google.
the class Files2Test method test_newBufferedReader$Path$Charset.
@Test
public void test_newBufferedReader$Path$Charset() throws IOException {
BufferedReader bufferedReader = Files.newBufferedReader(filesSetup.getDataFilePath(), StandardCharsets.US_ASCII);
assertEquals(TEST_FILE_DATA, bufferedReader.readLine());
// When the file has unicode characters.
writeToFile(filesSetup.getDataFilePath(), UTF_16_DATA);
bufferedReader = Files.newBufferedReader(filesSetup.getDataFilePath(), StandardCharsets.US_ASCII);
try {
bufferedReader.readLine();
fail();
} catch (MalformedInputException expected) {
}
}
use of java.nio.charset.MalformedInputException in project j2objc by google.
the class InputStreamReaderTest method test_read_1.
/*
* Class under test for int read() Regression for Harmony-411
*/
public void test_read_1() throws IOException {
// if the decoder is constructed by InputStreamReader itself, the
// decoder's default error action is REPLACE
InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(new byte[] { -32, -96 }), "UTF-8");
assertEquals("read() return incorrect value", 65533, isr.read());
InputStreamReader isr2 = new InputStreamReader(new ByteArrayInputStream(new byte[] { -32, -96 }), Charset.forName("UTF-8"));
assertEquals("read() return incorrect value", 65533, isr2.read());
// if the decoder is passed in, keep its status intact
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
InputStreamReader isr3 = new InputStreamReader(new ByteArrayInputStream(new byte[] { -32, -96 }), decoder);
try {
isr3.read();
fail("Should throw MalformedInputException");
} catch (MalformedInputException e) {
// expected
}
CharsetDecoder decoder2 = Charset.forName("UTF-8").newDecoder();
decoder2.onMalformedInput(CodingErrorAction.IGNORE);
InputStreamReader isr4 = new InputStreamReader(new ByteArrayInputStream(new byte[] { -32, -96 }), decoder2);
assertEquals("read() return incorrect value", -1, isr4.read());
CharsetDecoder decoder3 = Charset.forName("UTF-8").newDecoder();
decoder3.onMalformedInput(CodingErrorAction.REPLACE);
InputStreamReader isr5 = new InputStreamReader(new ByteArrayInputStream(new byte[] { -32, -96 }), decoder3);
assertEquals("read() return incorrect value", 65533, isr5.read());
}
use of java.nio.charset.MalformedInputException in project j2objc by google.
the class CharsetDecoder2Test method test_decode.
/**
* @tests java.nio.charset.CharsetDecoder#decode(java.nio.ByteBuffer)
*/
public void test_decode() throws CharacterCodingException {
// Regression for HARMONY-33
// ByteBuffer bb = ByteBuffer.allocate(1);
// bb.put(0, (byte) 77);
// CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
// decoder.onMalformedInput(CodingErrorAction.REPLACE);
// decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
// decoder.decode(bb);
// Regression for HARMONY-67
// byte[] b = new byte[] { (byte) 1 };
// ByteBuffer buf = ByteBuffer.wrap(b);
// CharBuffer charbuf = Charset.forName("UTF-16").decode(buf);
// assertEquals("Assert 0: charset UTF-16", 1, charbuf.length());
//
// charbuf = Charset.forName("UTF-16BE").decode(buf);
// assertEquals("Assert 1: charset UTF-16BE", 0, charbuf.length());
//
// charbuf = Charset.forName("UTF-16LE").decode(buf);
// assertEquals("Assert 2: charset UTF16LE", 0, charbuf.length());
// Regression for HARMONY-99
CharsetDecoder decoder2 = Charset.forName("UTF-16").newDecoder();
decoder2.onMalformedInput(CodingErrorAction.REPORT);
decoder2.onUnmappableCharacter(CodingErrorAction.REPORT);
ByteBuffer in = ByteBuffer.wrap(new byte[] { 109, 97, 109 });
try {
decoder2.decode(in);
fail("Assert 3: MalformedInputException should have thrown");
} catch (MalformedInputException e) {
// expected
}
}
use of java.nio.charset.MalformedInputException in project uavstack by uavorg.
the class IOUtils method encodeUTF8.
public static int encodeUTF8(char[] chars, int offset, int len, byte[] bytes) {
int sl = offset + len;
int dp = 0;
int dlASCII = dp + Math.min(len, bytes.length);
// ASCII only optimized loop
while (dp < dlASCII && chars[offset] < '\u0080') {
bytes[dp++] = (byte) chars[offset++];
}
while (offset < sl) {
char c = chars[offset++];
if (c < 0x80) {
// Have at most seven bits
bytes[dp++] = (byte) c;
} else if (c < 0x800) {
// 2 bytes, 11 bits
bytes[dp++] = (byte) (0xc0 | (c >> 6));
bytes[dp++] = (byte) (0x80 | (c & 0x3f));
} else if (c >= '\uD800' && c < ('\uDFFF' + 1)) {
// Character.isSurrogate(c) but 1.7
final int uc;
int ip = offset - 1;
if (c >= '\uD800' && c < ('\uDBFF' + 1)) {
// Character.isHighSurrogate(c)
if (sl - ip < 2) {
uc = -1;
} else {
char d = chars[ip + 1];
// d >= '\uDC00' && d < ('\uDFFF' + 1)
if (d >= '\uDC00' && d < ('\uDFFF' + 1)) {
// Character.isLowSurrogate(d)
// Character.toCodePoint(c, d)
uc = ((c << 10) + d) + (0x010000 - ('\uD800' << 10) - '\uDC00');
} else {
throw new JSONException("encodeUTF8 error", new MalformedInputException(1));
}
}
} else {
//
if (c >= '\uDC00' && c < ('\uDFFF' + 1)) {
// Character.isLowSurrogate(c)
bytes[dp++] = (byte) '?';
continue;
// throw new JSONException("encodeUTF8 error", new MalformedInputException(1));
} else {
uc = c;
}
}
if (uc < 0) {
bytes[dp++] = (byte) '?';
} else {
bytes[dp++] = (byte) (0xf0 | ((uc >> 18)));
bytes[dp++] = (byte) (0x80 | ((uc >> 12) & 0x3f));
bytes[dp++] = (byte) (0x80 | ((uc >> 6) & 0x3f));
bytes[dp++] = (byte) (0x80 | (uc & 0x3f));
// 2 chars
offset++;
}
} else {
// 3 bytes, 16 bits
bytes[dp++] = (byte) (0xe0 | ((c >> 12)));
bytes[dp++] = (byte) (0x80 | ((c >> 6) & 0x3f));
bytes[dp++] = (byte) (0x80 | (c & 0x3f));
}
}
return dp;
}
use of java.nio.charset.MalformedInputException in project checkstyle by checkstyle.
the class InputIndentationTryWithResourcesStrict method fooMethod.
// indent:4 exp:4
static void fooMethod(String zipFileName) throws IOException {
// indent:4 exp:4
// indent:8 exp:8
Charset charset = StandardCharsets.US_ASCII;
// indent:8 exp:8
Path filePath = Paths.get(zipFileName);
try (// indent:0 exp:12 warn
final BufferedWriter writer = Files.newBufferedWriter(null, charset);
// indent:12 exp:12
ZipFile zf = new ZipFile(zipFileName)) {
// indent:13 exp:8,12 warn
// indent:12 exp:12
zf.getName();
}
// indent:8 exp:8
try (// indent:8 exp:8
BufferedWriter writer = // indent:12 exp:12
Files.newBufferedWriter(filePath, // indent:0 exp:16 warn
charset);
// indent:12 exp:12
ZipFile zf = new ZipFile(zipFileName)) {
// indent:8 exp:8,12
// indent:12 exp:12
zf.getName();
}
// indent:8 exp:8
try (BufferedWriter writer = // indent:8 exp:8
Files.newBufferedWriter(filePath, // indent:0 exp:12 warn
charset);
// indent:12 exp:12
ZipFile zf = new ZipFile(zipFileName)) {
// indent:12 exp:8,12
// indent:12 exp:12
zf.getName();
}
// indent:8 exp:8
try (// indent:8 exp:8
BufferedWriter writer = // indent:12 exp:12
Files.newBufferedWriter(filePath, // indent:16 exp:16
charset);
// indent:12 exp:12
ZipFile zf = new ZipFile(zipFileName)) {
// indent:12 exp:8,12
// indent:12 exp:12
zf.getName();
}
// indent:8 exp:8
try (// indent:12 exp:12
BufferedWriter writer = Files.newBufferedWriter(null, charset);
// indent:12 exp:12
ZipFile zf = new ZipFile(zipFileName)) {
// indent:12 exp:8,12
// indent:12 exp:12
zf.getName();
}
// indent:8 exp:8
try {
// indent:8 exp:8
try (// indent:12 exp:12
BufferedWriter wrr = Files.newBufferedWriter(null, null)) {
// indent:20 exp:16 warn
// indent:16 exp:16
wrr.flush();
} catch (MalformedInputException e) {
// indent:12 exp:8,12
// Empty //indent:16 exp:16
}
// indent:12 exp:12
} catch (IOException e) {
// indent:8 exp:8
// indent:12 exp:12
Integer.parseInt("1");
}
// indent:8 exp:8
try {
// indent:8 exp:8
} catch (Exception e) {
// indent:8 exp:8
}
// indent:8 exp:8
try (BufferedWriter writer = Files.newBufferedWriter(null, charset)) {
// indent:8 exp:8
// indent:12 exp:12
Integer.parseInt("2");
} catch (Exception e) {
// indent:8 exp:8
}
// indent:8 exp:8
// indent:8 exp:8
BufferedWriter writ = Files.newBufferedWriter(filePath, charset);
try (BufferedWriter writer = writ) {
// indent:8 exp:8
// indent:12 exp:12
Integer.parseInt("2");
} catch (IOException e) {
// indent:12 exp:12
throw e;
}
// indent:8 exp:8
try (// indent:8 exp:8
BufferedWriter writer = writ) {
// indent:19 exp:12 warn
// indent:12 exp:12
Integer.parseInt("2");
} catch (IOException e) {
// indent:12 exp:12
throw e;
}
// indent:8 exp:8
try (// indent:8 exp:8
// indent:12 exp:12
BufferedWriter writer = writ) {
// indent:19 exp:16 warn
// indent:12 exp:12
Integer.parseInt("2");
} catch (IOException e) {
// indent:12 exp:12
throw e;
}
// indent:8 exp:8
try (// indent:8 exp:8
// indent:12 exp:12
BufferedWriter writer = writ) {
// indent:21 exp:16 warn
// indent:12 exp:12
Integer.parseInt("2");
} catch (IOException e) {
// indent:12 exp:12
throw e;
}
// indent:8 exp:8
try (// indent:12 exp:12
BufferedWriter writer = Files.newBufferedWriter(filePath, charset);
ZipFile zf = new // indent:12 exp:12
ZipFile(// indent:16 exp:16
zipFileName)) {
// indent:12 exp:8,12
// indent:12 exp:12
zf.getName();
}
// indent:8 exp:8
try (// indent:12 exp:12
BufferedWriter writer = Files.newBufferedWriter(filePath, charset);
ZipFile zf = new // indent:12 exp:12
ZipFile(// indent:17 exp:16 warn
zipFileName)) {
// indent:12 exp:8,12
// indent:12 exp:12
zf.getName();
}
// indent:8 exp:8
try (// indent:12 exp:12
BufferedWriter writer = Files.newBufferedWriter(filePath, charset);
ZipFile zf = new // indent:12 exp:12
ZipFile(// indent:15 exp:16 warn
zipFileName)) {
// indent:12 exp:8,12
// indent:12 exp:12
zf.getName();
}
// indent:8 exp:8
try (BufferedWriter writer = Files.newBufferedWriter(filePath, charset)) {
// indent:8 exp:8
// indent:12 exp:12
writer.close();
}
// indent:8 exp:8
try (BufferedWriter writer = Files.newBufferedWriter(filePath, charset)) {
// indent:8 exp:8
// indent:12 exp:12
writer.close();
}
// indent:8 exp:8
try (BufferedWriter writer = Files.newBufferedWriter(filePath, charset)) {
// indent:12 exp:12
// indent:15 exp:12 warn
writer.close();
}
// indent:8 exp:8
try (BufferedWriter writer = Files.newBufferedWriter(null, charset)) {
// indent:11 exp:12 warn
// indent:9 exp:12 warn
writer.close();
}
// indent:8 exp:8
try (BufferedWriter writer = // indent:11 exp:12 warn
Files.newBufferedWriter(filePath, charset)) {
// indent:11 exp:16 warn
// indent:13 exp:12 warn
writer.close();
}
// indent:8 exp:8
try (// indent:7 exp:8 warn
BufferedWriter writer = writ) {
// indent:7 exp:8,12 warn
} catch (MalformedInputException e) {
// indent:12 exp:12
throw e;
}
// indent:8 exp:8
try (// indent:9 exp:8 warn
BufferedWriter writer = writ) {
// indent:12 exp:8,12
} catch (MalformedInputException e) {
// indent:12 exp:12
throw e;
}
// indent:8 exp:8
try (BufferedWriter writer = // indent:8 exp:8
Files.newBufferedWriter(filePath, // indent:13 exp:12 warn
charset)) {
// indent:12 exp:8,12
} catch (MalformedInputException e) {
// indent:12 exp:12
throw e;
}
// indent:8 exp:8
try (BufferedWriter writer = // indent:8 exp:8
Files.newBufferedWriter(filePath, // indent:11 exp:12 warn
charset)) {
// indent:12 exp:8,12
} catch (MalformedInputException e) {
// indent:12 exp:12
throw e;
}
// indent:8 exp:8
try (DirectoryStream<Path> ds = // indent:8 exp:8
Files.newDirectoryStream(// indent:8 exp:8
filePath, new // indent:12 exp:12
DirectoryStream.Filter<Path>() {
// indent:16 exp:16
@Override
public boolean accept(Path path) {
// indent:20 exp:20
return path.toString().contains("");
}
})) // indent:12 exp:12
{
// indent:8 exp:8
for (// indent:12 exp:12
Path p : // indent:12 exp:12
ds) // indent:16 exp:16
;
}
// indent:8 exp:8
}
Aggregations