use of java.nio.charset.CharsetDecoder in project buck by facebook.
the class CxxGtestTest method parseResults.
@Override
protected ImmutableList<TestResultSummary> parseResults(Path exitCode, Path output, Path results) throws IOException, SAXException {
// Try to parse the results file first, which should be written if the test suite exited
// normally (even in the event of a failing test). If this fails, just construct a test
// summary with the output we have.
Document doc;
try {
doc = XmlDomParser.parse(getProjectFilesystem().resolve(results));
} catch (SAXException e) {
return ImmutableList.of(getProgramFailureSummary("test program aborted before finishing", getProjectFilesystem().readFileIfItExists(output).orElse("")));
}
ImmutableList.Builder<TestResultSummary> summariesBuilder = ImmutableList.builder();
// It's possible the test output had invalid characters in it's output, so make sure to
// ignore these as we parse the output lines.
Optional<String> currentTest = Optional.empty();
Map<String, ChunkAccumulator> stdout = Maps.newHashMap();
CharsetDecoder decoder = Charsets.UTF_8.newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
try (InputStream input = getProjectFilesystem().newFileInputStream(output);
BufferedReader reader = new BufferedReader(new InputStreamReader(input, decoder))) {
String line;
while ((line = reader.readLine()) != null) {
Matcher matcher;
if ((matcher = START.matcher(line.trim())).matches()) {
String test = matcher.group(1);
currentTest = Optional.of(test);
stdout.put(test, new ChunkAccumulator(Charsets.UTF_8, maxTestOutputSize));
} else if (END.matcher(line.trim()).matches()) {
currentTest = Optional.empty();
} else if (currentTest.isPresent()) {
Preconditions.checkNotNull(stdout.get(currentTest.get())).append(line);
}
}
}
NodeList testcases = doc.getElementsByTagName("testcase");
for (int index = 0; index < testcases.getLength(); index++) {
Node testcase = testcases.item(index);
NamedNodeMap attributes = testcase.getAttributes();
String testCase = attributes.getNamedItem("classname").getNodeValue();
String testName = attributes.getNamedItem("name").getNodeValue();
String testFull = String.format("%s.%s", testCase, testName);
Double time = Double.parseDouble(attributes.getNamedItem("time").getNodeValue()) * 1000;
// Prepare the result message and type
ResultType type = ResultType.SUCCESS;
String message = "";
if (testcase.getChildNodes().getLength() > 0) {
Node failure = testcase.getChildNodes().item(1);
type = ResultType.FAILURE;
message = failure.getAttributes().getNamedItem("message").getNodeValue();
} else if (attributes.getNamedItem("status").getNodeValue().equals(NOTRUN)) {
type = ResultType.ASSUMPTION_VIOLATION;
message = "DISABLED";
}
// Prepare the tests stdout.
String testStdout = "";
if (stdout.containsKey(testFull)) {
testStdout = Joiner.on(System.lineSeparator()).join(stdout.get(testFull).getChunks());
}
summariesBuilder.add(new TestResultSummary(testCase, testName, type, time.longValue(), message, "", testStdout, ""));
}
return summariesBuilder.build();
}
use of java.nio.charset.CharsetDecoder in project j2objc by google.
the class CharsetDecoderTest method test_ByteArray_decode_no_offset.
// http://code.google.com/p/android/issues/detail?id=4237
public void test_ByteArray_decode_no_offset() throws Exception {
CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
byte[] arr = encode("UTF-16", "Android");
ByteBuffer inBuffer = ByteBuffer.wrap(arr, 0, arr.length).slice();
CharBuffer outBuffer = CharBuffer.allocate(arr.length);
decoder.reset();
CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
assertFalse(coderResult.toString(), coderResult.isError());
decoder.flush(outBuffer);
outBuffer.flip();
assertEquals("Android", outBuffer.toString().trim());
}
use of java.nio.charset.CharsetDecoder in project gradle by gradle.
the class StreamByteBuffer method readAsCharBuffer.
private CharBuffer readAsCharBuffer(Charset charset) throws CharacterCodingException {
CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
CharBuffer charbuffer = CharBuffer.allocate(totalBytesUnread());
ByteBuffer buf = null;
boolean wasUnderflow = false;
ByteBuffer nextBuf = null;
boolean needsFlush = false;
while (hasRemaining(nextBuf) || hasRemaining(buf) || prepareRead() != -1) {
if (hasRemaining(buf)) {
// handle decoding underflow, multi-byte unicode character at buffer chunk boundary
if (!wasUnderflow) {
throw new IllegalStateException("Unexpected state. Buffer has remaining bytes without underflow in decoding.");
}
if (!hasRemaining(nextBuf) && prepareRead() != -1) {
nextBuf = currentReadChunk.readToNioBuffer();
}
// copy one by one until the underflow has been resolved
buf = ByteBuffer.allocate(buf.remaining() + 1).put(buf);
buf.put(nextBuf.get());
buf.flip();
} else {
if (hasRemaining(nextBuf)) {
buf = nextBuf;
} else if (prepareRead() != -1) {
buf = currentReadChunk.readToNioBuffer();
if (!hasRemaining(buf)) {
throw new IllegalStateException("Unexpected state. Buffer is empty.");
}
}
nextBuf = null;
}
boolean endOfInput = !hasRemaining(nextBuf) && prepareRead() == -1;
int bufRemainingBefore = buf.remaining();
CoderResult result = decoder.decode(buf, charbuffer, false);
if (bufRemainingBefore > buf.remaining()) {
needsFlush = true;
}
if (endOfInput) {
result = decoder.decode(ByteBuffer.allocate(0), charbuffer, true);
if (!result.isUnderflow()) {
result.throwException();
}
break;
}
wasUnderflow = result.isUnderflow();
}
if (needsFlush) {
CoderResult result = decoder.flush(charbuffer);
if (!result.isUnderflow()) {
result.throwException();
}
}
clear();
// push back remaining bytes of multi-byte unicode character
while (hasRemaining(buf)) {
byte b = buf.get();
try {
getOutputStream().write(b);
} catch (IOException e) {
throw UncheckedException.throwAsUncheckedException(e);
}
}
charbuffer.flip();
return charbuffer;
}
use of java.nio.charset.CharsetDecoder in project MusicDNA by harjot-oberai.
the class Mp4HdlrBox method processData.
public void processData() throws CannotReadException {
//Skip other flags
dataBuffer.position(dataBuffer.position() + VERSION_FLAG_LENGTH + OTHER_FLAG_LENGTH + RESERVED_FLAG_LENGTH);
CharsetDecoder decoder = Charset.forName("ISO-8859-1").newDecoder();
try {
handlerType = decoder.decode((ByteBuffer) dataBuffer.slice().limit(HANDLER_LENGTH)).toString();
} catch (CharacterCodingException cee) {
//Ignore
}
//To getFields human readable name
mediaDataType = mediaDataTypeMap.get(handlerType);
}
use of java.nio.charset.CharsetDecoder in project k-9 by k9mail.
the class FolderNameCodec method decode.
public String decode(String encodedFolderName) throws CharacterCodingException {
CharsetDecoder decoder = modifiedUtf7Charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT);
ByteBuffer byteBuffer = ByteBuffer.wrap(encodedFolderName.getBytes(asciiCharset));
CharBuffer charBuffer = decoder.decode(byteBuffer);
return charBuffer.toString();
}
Aggregations