Java Code Examples for java.nio.charset.CharacterCodingException#getMessage()

The following examples show how to use java.nio.charset.CharacterCodingException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IOUtils.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public static void decode(CharsetDecoder charsetDecoder, ByteBuffer byteBuf, CharBuffer charByte) {
    try {
        CoderResult cr = charsetDecoder.decode(byteBuf, charByte, true);

        if (!cr.isUnderflow()) {
            cr.throwException();
        }

        cr = charsetDecoder.flush(charByte);

        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        // Substitution is always enabled,
        // so this shouldn't happen
        throw new JSONException("utf8 decode error, " + x.getMessage(), x);
    }
}
 
Example 2
Source File: SerialWriterStringEncoder.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public byte[] encode(char[] chars, int off, int len, byte[] bytes) {
	ByteBuffer byteBuf = ByteBuffer.wrap(bytes);

	CharBuffer charBuf = CharBuffer.wrap(chars, off, len);
	try {
		CoderResult cr = encoder.encode(charBuf, byteBuf, true);
		if (!cr.isUnderflow()) {
			cr.throwException();
		}
		cr = encoder.flush(byteBuf);
		if (!cr.isUnderflow()) {
			cr.throwException();
		}
	} catch (CharacterCodingException x) {
		// Substitution is always enabled,
		// so this shouldn't happen
		throw new JSONException(x.getMessage(), x);
	}

	int bytesLength = byteBuf.position();
	byte[] copy = new byte[bytesLength];
	System.arraycopy(bytes, 0, copy, 0, bytesLength);
	return copy;
}
 
Example 3
Source File: IOUtils.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public static void decode(CharsetDecoder charsetDecoder, ByteBuffer byteBuf, CharBuffer charByte) {
    try {
        CoderResult cr = charsetDecoder.decode(byteBuf, charByte, true);

        if (!cr.isUnderflow()) {
            cr.throwException();
        }

        cr = charsetDecoder.flush(charByte);

        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        // Substitution is always enabled,
        // so this shouldn't happen
        throw new JSONException(x.getMessage(), x);
    }
}
 
Example 4
Source File: ScannerImpl.java    From sofa-acts with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Scan a sequence of %-escaped URI escape codes and convert them into a
 * String representing the unescaped values.
 * </p>
 * 
 * FIXME This method fails for more than 256 bytes' worth of URI-encoded
 * characters in a row. Is this possible? Is this a use-case?
 * 
 * @see http://www.ietf.org/rfc/rfc2396.txt, section 2.4, Escaped Encoding.
 */
private String scanUriEscapes(String name, Mark startMark) {
    // First, look ahead to see how many URI-escaped characters we should
    // expect, so we can use the correct buffer size.
    int length = 1;
    while (reader.peek(length * 3) == '%') {
        length++;
    }
    // See the specification for details.
    // URIs containing 16 and 32 bit Unicode characters are
    // encoded in UTF-8, and then each octet is written as a
    // separate character.
    Mark beginningMark = reader.getMark();
    ByteBuffer buff = ByteBuffer.allocate(length);
    while (reader.peek() == '%') {
        reader.forward();
        try {
            byte code = (byte) Integer.parseInt(reader.prefix(2), 16);
            buff.put(code);
        } catch (NumberFormatException nfe) {
            throw new ScannerException("while scanning a " + name, startMark,
                "expected URI escape sequence of 2 hexadecimal numbers, but found "
                        + reader.peek() + "(" + ((int) reader.peek()) + ") and "
                        + reader.peek(1) + "(" + ((int) reader.peek(1)) + ")", reader.getMark());
        }
        reader.forward(2);
    }
    buff.flip();
    try {
        return UriEncoder.decode(buff);
    } catch (CharacterCodingException e) {
        throw new ScannerException("while scanning a " + name, startMark,
            "expected URI in UTF-8: " + e.getMessage(), beginningMark);
    }
}
 
Example 5
Source File: ScannerImpl.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * <p>
 * Scan a sequence of %-escaped URI escape codes and convert them into a
 * String representing the unescaped values.
 * </p>
 * 
 * FIXME This method fails for more than 256 bytes' worth of URI-encoded
 * characters in a row. Is this possible? Is this a use-case?
 * 
 * @see http://www.ietf.org/rfc/rfc2396.txt, section 2.4, Escaped Encoding.
 */
private String scanUriEscapes(String name, Mark startMark) {
    // First, look ahead to see how many URI-escaped characters we should
    // expect, so we can use the correct buffer size.
    int length = 1;
    while (reader.peek(length * 3) == '%') {
        length++;
    }
    // See the specification for details.
    // URIs containing 16 and 32 bit Unicode characters are
    // encoded in UTF-8, and then each octet is written as a
    // separate character.
    Mark beginningMark = reader.getMark();
    ByteBuffer buff = ByteBuffer.allocate(length);
    while (reader.peek() == '%') {
        reader.forward();
        try {
            byte code = (byte) Integer.parseInt(reader.prefix(2), 16);
            buff.put(code);
        } catch (NumberFormatException nfe) {
            throw new ScannerException("while scanning a " + name, startMark,
                    "expected URI escape sequence of 2 hexadecimal numbers, but found "
                            + reader.peek() + "(" + ((int) reader.peek()) + ") and "
                            + reader.peek(1) + "(" + ((int) reader.peek(1)) + ")",
                    reader.getMark());
        }
        reader.forward(2);
    }
    buff.flip();
    try {
        return UriEncoder.decode(buff);
    } catch (CharacterCodingException e) {
        throw new ScannerException("while scanning a " + name, startMark,
                "expected URI in UTF-8: " + e.getMessage(), beginningMark);
    }
}
 
Example 6
Source File: ScannerImpl.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Scan a sequence of %-escaped URI escape codes and convert them into a
 * String representing the unescaped values.
 * </p>
 * 
 * FIXME This method fails for more than 256 bytes' worth of URI-encoded
 * characters in a row. Is this possible? Is this a use-case?
 * 
 * @see <a href="http://www.ietf.org/rfc/rfc2396.txt"></a>, section 2.4, Escaped Encoding.
 */
private String scanUriEscapes(String name, Mark startMark) {
    // First, look ahead to see how many URI-escaped characters we should
    // expect, so we can use the correct buffer size.
    int length = 1;
    while (reader.peek(length * 3) == '%') {
        length++;
    }
    // See the specification for details.
    // URIs containing 16 and 32 bit Unicode characters are
    // encoded in UTF-8, and then each octet is written as a
    // separate character.
    Mark beginningMark = reader.getMark();
    ByteBuffer buff = ByteBuffer.allocate(length);
    while (reader.peek() == '%') {
        reader.forward();
        try {
            byte code = (byte) Integer.parseInt(reader.prefix(2), 16);
            buff.put(code);
        } catch (NumberFormatException nfe) {
            throw new ScannerException("while scanning a " + name, startMark,
                    "expected URI escape sequence of 2 hexadecimal numbers, but found "
                            + reader.peek() + "(" + ((int) reader.peek()) + ") and "
                            + reader.peek(1) + "(" + ((int) reader.peek(1)) + ")",
                    reader.getMark());
        }
        reader.forward(2);
    }
    buff.flip();
    try {
        return UriEncoder.decode(buff);
    } catch (CharacterCodingException e) {
        throw new ScannerException("while scanning a " + name, startMark,
                "expected URI in UTF-8: " + e.getMessage(), beginningMark);
    }
}
 
Example 7
Source File: ScannerImpl.java    From pipeline-utility-steps-plugin with MIT License 5 votes vote down vote up
/**
 * <p>
 * Scan a sequence of %-escaped URI escape codes and convert them into a
 * String representing the unescaped values.
 * </p>
 * 
 * FIXME This method fails for more than 256 bytes' worth of URI-encoded
 * characters in a row. Is this possible? Is this a use-case?
 * 
 * @see <a href="http://www.ietf.org/rfc/rfc2396.txt"></a>, section 2.4, Escaped Encoding.
 */
private String scanUriEscapes(String name, Mark startMark) {
    // First, look ahead to see how many URI-escaped characters we should
    // expect, so we can use the correct buffer size.
    int length = 1;
    while (reader.peek(length * 3) == '%') {
        length++;
    }
    // See the specification for details.
    // URIs containing 16 and 32 bit Unicode characters are
    // encoded in UTF-8, and then each octet is written as a
    // separate character.
    Mark beginningMark = reader.getMark();
    ByteBuffer buff = ByteBuffer.allocate(length);
    while (reader.peek() == '%') {
        reader.forward();
        try {
            byte code = (byte) Integer.parseInt(reader.prefix(2), 16);
            buff.put(code);
        } catch (NumberFormatException nfe) {
            throw new ScannerException("while scanning a " + name, startMark,
                    "expected URI escape sequence of 2 hexadecimal numbers, but found "
                            + reader.peek() + "(" + ((int) reader.peek()) + ") and "
                            + reader.peek(1) + "(" + ((int) reader.peek(1)) + ")",
                    reader.getMark());
        }
        reader.forward(2);
    }
    buff.flip();
    try {
        return UriEncoder.decode(buff);
    } catch (CharacterCodingException e) {
        throw new ScannerException("while scanning a " + name, startMark,
                "expected URI in UTF-8: " + e.getMessage(), beginningMark);
    }
}
 
Example 8
Source File: DbxRequestUtil.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
public static String parseErrorBody(String requestId, int statusCode, byte[] body)
    throws BadResponseException {
    // Read the error message from the body.
    // TODO: Get charset from the HTTP Content-Type header.  It's wrong to just assume UTF-8.
    // TODO: Maybe try parsing the message as JSON and do something more structured?
    try {
        return StringUtil.utf8ToString(body);
    } catch (CharacterCodingException e) {
        throw new BadResponseException(requestId, "Got non-UTF8 response body: " + statusCode + ": " + e.getMessage());
    }
}
 
Example 9
Source File: MessageUtil.java    From CXTouch with GNU General Public License v3.0 4 votes vote down vote up
public static String readString(IoBuffer buffer, LengthType lenType, CharsetDecoder decoder) throws NoArrayException {
    int length;
    int remain = buffer.remaining();
    buffer.mark();
    if (lenType == LengthType.INT) {
        if (remain < 4) {
            buffer.reset();
            throw new NoArrayException();
        }
        length = buffer.getInt();
    } else if(lenType == LengthType.SHORT) {
        if (remain < 2) {
            buffer.reset();
            throw new NoArrayException();
        }
        length = buffer.getShort();
    } else if (lenType == LengthType.BYTE) {
        if (remain < 1) {
            buffer.reset();
            throw new NoArrayException();
        }
        length = buffer.get();
    } else {
        buffer.reset();
        throw new IllegalArgumentException("Illegal type:" + lenType);
    }

    if (length < 0) {
        return null;
    } if (length == 0) {
        return "";
    } else {
        if (buffer.remaining() < length) {
            buffer.reset();
            throw new NoArrayException();
        }
        try {
            return buffer.getString(length, decoder);
        } catch (CharacterCodingException e) {
            throw new RuntimeException("Reading string failed:" + e.getMessage(), e);
        }
    }
}
 
Example 10
Source File: CassandraServer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private static String uncompress(ByteBuffer query, Compression compression) throws InvalidRequestException
{
    String queryString = null;

    // Decompress the query string.
    try
    {
        switch (compression)
        {
            case GZIP:
                DataOutputBuffer decompressed = new DataOutputBuffer();
                byte[] outBuffer = new byte[1024], inBuffer = new byte[1024];

                Inflater decompressor = new Inflater();

                int lenRead = 0;
                while (true)
                {
                    if (decompressor.needsInput())
                        lenRead = query.remaining() < 1024 ? query.remaining() : 1024;
                    query.get(inBuffer, 0, lenRead);
                    decompressor.setInput(inBuffer, 0, lenRead);

                    int lenWrite = 0;
                    while ((lenWrite = decompressor.inflate(outBuffer)) != 0)
                        decompressed.write(outBuffer, 0, lenWrite);

                    if (decompressor.finished())
                        break;
                }

                decompressor.end();

                queryString = new String(decompressed.getData(), 0, decompressed.getLength(), StandardCharsets.UTF_8);
                break;
            case NONE:
                try
                {
                    queryString = ByteBufferUtil.string(query);
                }
                catch (CharacterCodingException ex)
                {
                    throw new InvalidRequestException(ex.getMessage());
                }
                break;
        }
    }
    catch (DataFormatException e)
    {
        throw new InvalidRequestException("Error deflating query string.");
    }
    return queryString;
}