Search in sources :

Example 46 with CharsetDecoder

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();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) NamedNodeMap(org.w3c.dom.NamedNodeMap) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ResultType(com.facebook.buck.test.result.type.ResultType) Document(org.w3c.dom.Document) TestResultSummary(com.facebook.buck.test.TestResultSummary) SAXException(org.xml.sax.SAXException) BufferedReader(java.io.BufferedReader) ChunkAccumulator(com.facebook.buck.util.ChunkAccumulator)

Example 47 with CharsetDecoder

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());
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 48 with CharsetDecoder

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;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 49 with CharsetDecoder

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);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 50 with CharsetDecoder

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();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharsetDecoder (java.nio.charset.CharsetDecoder)90 CharBuffer (java.nio.CharBuffer)45 ByteBuffer (java.nio.ByteBuffer)33 CoderResult (java.nio.charset.CoderResult)25 Charset (java.nio.charset.Charset)24 InputStreamReader (java.io.InputStreamReader)11 CharacterCodingException (java.nio.charset.CharacterCodingException)9 IOException (java.io.IOException)8 BufferedReader (java.io.BufferedReader)5 Properties (java.util.Properties)5 RegisterRequestProcessor (com.linkedin.databus.container.request.RegisterRequestProcessor)4 LogicalSource (com.linkedin.databus.core.data_model.LogicalSource)4 ChunkedWritableByteChannel (com.linkedin.databus2.core.container.ChunkedWritableByteChannel)4 DatabusRequest (com.linkedin.databus2.core.container.request.DatabusRequest)4 SchemaRegistryService (com.linkedin.databus2.schemas.SchemaRegistryService)4 SourceIdNameRegistry (com.linkedin.databus2.schemas.SourceIdNameRegistry)4 InputStream (java.io.InputStream)4 Reader (java.io.Reader)4 ArrayList (java.util.ArrayList)4 RegisterResponseEntry (com.linkedin.databus2.core.container.request.RegisterResponseEntry)3