Search in sources :

Example 66 with CharBuffer

use of java.nio.CharBuffer in project hive by apache.

the class GenericUDFDecode method evaluate.

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    Object value = bytesOI.getPrimitiveJavaObject(arguments[0].get());
    if (value == null) {
        return null;
    }
    ByteBuffer wrappedBytes = ByteBuffer.wrap((byte[]) value);
    CharBuffer decoded;
    if (decoder != null) {
        try {
            decoded = decoder.decode(wrappedBytes);
        } catch (CharacterCodingException e) {
            throw new HiveException(e);
        }
    } else {
        String charSetName = PrimitiveObjectInspectorUtils.getString(arguments[1].get(), charsetOI);
        decoded = Charset.forName(charSetName).decode(wrappedBytes);
    }
    return decoded.toString();
}
Also used : HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 67 with CharBuffer

use of java.nio.CharBuffer in project tomcat by apache.

the class TestUtf8 method doTest.

private void doTest(CharsetDecoder decoder, Utf8TestCase testCase, int flags) {
    int len = testCase.input.length;
    ByteBuffer bb = ByteBuffer.allocate(len);
    CharBuffer cb = CharBuffer.allocate(len);
    // Configure decoder to fail on an error
    decoder.reset();
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    // an invalid sequence has been provided
    for (int i = 0; i < len; i++) {
        bb.put((byte) testCase.input[i]);
        bb.flip();
        CoderResult cr = decoder.decode(bb, cb, false);
        if (cr.isError()) {
            int expected = testCase.invalidIndex;
            if ((flags & ERROR_POS_PLUS1) != 0) {
                expected += 1;
            }
            if ((flags & ERROR_POS_PLUS2) != 0) {
                expected += 2;
            }
            if ((flags & ERROR_POS_PLUS4) != 0) {
                expected += 4;
            }
            Assert.assertEquals(testCase.description, expected, i);
            break;
        }
        bb.compact();
    }
    // Configure decoder to replace on an error
    decoder.reset();
    decoder.onMalformedInput(CodingErrorAction.REPLACE);
    decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    // Add each byte one at a time.
    bb.clear();
    cb.clear();
    for (int i = 0; i < len; i++) {
        bb.put((byte) testCase.input[i]);
        bb.flip();
        CoderResult cr = decoder.decode(bb, cb, false);
        if (cr.isError()) {
            Assert.fail(testCase.description);
        }
        bb.compact();
    }
    // For incomplete sequences at the end of the input need to tell
    // the decoder the input has ended
    bb.flip();
    CoderResult cr = decoder.decode(bb, cb, true);
    if (cr.isError()) {
        Assert.fail(testCase.description);
    }
    cb.flip();
    String expected = testCase.outputReplaced;
    if ((flags & REPLACE_SWALLOWS_TRAILER) != 0) {
        expected = expected.substring(0, expected.length() - 1);
    }
    if ((flags & REPLACE_MISSING1) != 0) {
        expected = expected.substring(0, 1) + expected.substring(2, expected.length());
    }
    if ((flags & REPLACE_MISSING2) != 0) {
        expected = expected.substring(0, 1) + expected.substring(3, expected.length());
    }
    if ((flags & REPLACE_MISSING4) != 0) {
        expected = expected.substring(0, 1) + expected.substring(5, expected.length());
    }
    Assert.assertEquals(testCase.description, expected, cb.toString());
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 68 with CharBuffer

use of java.nio.CharBuffer in project feign by OpenFeign.

the class Util method toString.

/**
   * Adapted from {@code com.google.common.io.CharStreams.toString()}.
   */
public static String toString(Reader reader) throws IOException {
    if (reader == null) {
        return null;
    }
    try {
        StringBuilder to = new StringBuilder();
        CharBuffer buf = CharBuffer.allocate(BUF_SIZE);
        while (reader.read(buf) != -1) {
            buf.flip();
            to.append(buf);
            buf.clear();
        }
        return to.toString();
    } finally {
        ensureClosed(reader);
    }
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 69 with CharBuffer

use of java.nio.CharBuffer in project rest.li by linkedin.

the class BufferChain method bufferToUtf8CString.

private String bufferToUtf8CString(int numBytes, ArrayList<ByteBuffer> bufferList) throws IOException {
    String result;
    if (numBytes == 0) {
        result = "";
    } else if (bufferList == null) {
        _decoder.reset();
        // char should be smaller than # of bytes in buffer.
        CharBuffer charBuffer = CharBuffer.allocate(numBytes);
        int limit = _currentBuffer.limit();
        _currentBuffer.limit(_currentBuffer.position() + numBytes);
        checkCoderResult(_decoder.decode(_currentBuffer, charBuffer, true));
        _currentBuffer.limit(limit);
        _decoder.flush(charBuffer);
        charBuffer.flip();
        result = charBuffer.toString();
    } else {
        // char should be smaller than # of bytes in buffer.
        char[] charBuffer = new char[numBytes];
        BufferChain chain = new BufferChain(_order, bufferList, _bufferSize);
        InputStream inputStream = chain.asInputStream();
        Reader reader = new InputStreamReader(inputStream, _charset);
        int offset = 0;
        int remaining = numBytes;
        int charactersRead;
        while ((charactersRead = reader.read(charBuffer, offset, remaining)) > 0) {
            offset += charactersRead;
            remaining -= charactersRead;
        }
        result = new String(charBuffer, 0, offset);
    }
    advanceBufferIfCurrentBufferHasNoRemaining();
    // terminal zero byte
    _currentBuffer.get();
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) CharBuffer(java.nio.CharBuffer) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteString(com.linkedin.data.ByteString)

Example 70 with CharBuffer

use of java.nio.CharBuffer in project hudson-2.x by hudson.

the class Util method rawEncode.

/**
     * Encode a single path component for use in an HTTP URL.
     * Escapes all non-ASCII, general unsafe (space and "#%<>[\]^`{|}~)
     * and HTTP special characters (/;:?) as specified in RFC1738.
     * (so alphanumeric and !@$&*()-_=+',. are not encoded)
     * Note that slash(/) is encoded, so the given string should be a
     * single path component used in constructing a URL.
     * Method name inspired by PHP's rawurlencode.
     */
public static String rawEncode(String s) {
    boolean escaped = false;
    StringBuilder out = null;
    CharsetEncoder enc = null;
    CharBuffer buf = null;
    char c;
    for (int i = 0, m = s.length(); i < m; i++) {
        c = s.charAt(i);
        if (c > 122 || uriMap[c]) {
            if (!escaped) {
                out = new StringBuilder(i + (m - i) * 3);
                out.append(s.substring(0, i));
                enc = Charset.forName("UTF-8").newEncoder();
                buf = CharBuffer.allocate(1);
                escaped = true;
            }
            // 1 char -> UTF8
            buf.put(0, c);
            buf.rewind();
            try {
                ByteBuffer bytes = enc.encode(buf);
                while (bytes.hasRemaining()) {
                    byte b = bytes.get();
                    out.append('%');
                    out.append(toDigit((b >> 4) & 0xF));
                    out.append(toDigit(b & 0xF));
                }
            } catch (CharacterCodingException ex) {
            }
        } else if (escaped) {
            out.append(c);
        }
    }
    return escaped ? out.toString() : s;
}
Also used : CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharBuffer (java.nio.CharBuffer)401 ByteBuffer (java.nio.ByteBuffer)152 CoderResult (java.nio.charset.CoderResult)83 CharsetDecoder (java.nio.charset.CharsetDecoder)47 IOException (java.io.IOException)45 Charset (java.nio.charset.Charset)33 Test (org.junit.Test)23 CharacterCodingException (java.nio.charset.CharacterCodingException)15 CharsetEncoder (java.nio.charset.CharsetEncoder)15 FileInputStream (java.io.FileInputStream)11 IntBuffer (java.nio.IntBuffer)10 Reader (java.io.Reader)9 BufferOverflowException (java.nio.BufferOverflowException)9 DoubleBuffer (java.nio.DoubleBuffer)9 FloatBuffer (java.nio.FloatBuffer)9 LongBuffer (java.nio.LongBuffer)9 ShortBuffer (java.nio.ShortBuffer)9 BufferUnderflowException (java.nio.BufferUnderflowException)7 ValueWrapper (org.apache.geode.internal.memcached.ValueWrapper)7 InputStream (java.io.InputStream)6