Search in sources :

Example 56 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project cassandra by apache.

the class CBUtil method readString.

private static String readString(ByteBuf cb, int length) {
    if (length == 0)
        return "";
    ByteBuffer buffer = cb.nioBuffer(cb.readerIndex(), length);
    try {
        String str = decodeString(buffer);
        cb.readerIndex(cb.readerIndex() + length);
        return str;
    } catch (IllegalStateException | CharacterCodingException e) {
        throw new ProtocolException("Cannot decode string as UTF8: '" + ByteBufferUtil.bytesToHex(buffer) + "'; " + e);
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 57 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project cassandra by apache.

the class SettingsSchema method createCounter1StatementCQL3.

String createCounter1StatementCQL3(StressSettings settings) {
    StringBuilder b = new StringBuilder();
    b.append("CREATE TABLE IF NOT EXISTS ").append("counter1 (key blob PRIMARY KEY,");
    try {
        for (ByteBuffer name : settings.columns.names) b.append("\n, \"").append(ByteBufferUtil.string(name)).append("\" counter");
    } catch (CharacterCodingException e) {
        throw new RuntimeException(e);
    }
    //Compression
    b.append(") WITH COMPACT STORAGE AND compression = {");
    if (compression != null)
        b.append("'sstable_compression' : '").append(compression).append("'");
    b.append("}");
    //Compaction
    if (compactionStrategy != null) {
        b.append(" AND compaction = { 'class' : '").append(compactionStrategy).append("'");
        for (Map.Entry<String, String> entry : compactionStrategyOptions.entrySet()) b.append(", '").append(entry.getKey()).append("' : '").append(entry.getValue()).append("'");
        b.append("}");
    }
    b.append(";\n");
    return b.toString();
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 58 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project japid42 by branaway.

the class StringUtils method encodeUTF8.

/**
	 * try to encode a char[] as fast as possible for later use in outputstream
	 * 
	 * @param ca
	 * @param off
	 * @param len
	 * @return
	 */
public static ByteBuffer encodeUTF8(String src) {
    // char[] ca = src.toCharArray();
    int len = src.length();
    int off = 0;
    int en = (int) (len * ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return null;
    ce.reset();
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(src, off, len);
    try {
        CoderResult cr = ce.encode(cb, bb, true);
        if (!cr.isUnderflow())
            cr.throwException();
        cr = ce.flush(bb);
        if (!cr.isUnderflow())
            cr.throwException();
        return bb;
    } catch (CharacterCodingException x) {
        // so this shouldn't happen
        throw new Error(x);
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 59 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project hive by apache.

the class LazyHiveVarchar method init.

@Override
public void init(ByteArrayRef bytes, int start, int length) {
    if (oi.isEscaped()) {
        Text textData = data.getTextValue();
        // This is doing a lot of copying here, this could be improved by enforcing length
        // at the same time as escaping rather than as separate steps.
        LazyUtils.copyAndEscapeStringDataToText(bytes.getData(), start, length, oi.getEscapeChar(), textData);
        data.set(textData.toString(), maxLength);
        isNull = false;
    } else {
        try {
            String byteData = null;
            byteData = Text.decode(bytes.getData(), start, length);
            data.set(byteData, maxLength);
            isNull = false;
        } catch (CharacterCodingException e) {
            isNull = true;
            LOG.debug("Data not in the HiveVarchar data type range so converted to null.", e);
        }
    }
}
Also used : Text(org.apache.hadoop.io.Text) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 60 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project hive by apache.

the class LazyPrimitive method logExceptionMessage.

public void logExceptionMessage(ByteArrayRef bytes, int start, int length, String dataType) {
    try {
        if (LOG.isDebugEnabled()) {
            String byteData = Text.decode(bytes.getData(), start, length);
            LOG.debug("Data not in the " + dataType + " data type range so converted to null. Given data is :" + byteData, new Exception("For debugging purposes"));
        }
    } catch (CharacterCodingException e1) {
        LOG.debug("Data not in the " + dataType + " data type range so converted to null.", e1);
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CharacterCodingException(java.nio.charset.CharacterCodingException)

Aggregations

CharacterCodingException (java.nio.charset.CharacterCodingException)90 ByteBuffer (java.nio.ByteBuffer)46 CharsetEncoder (java.nio.charset.CharsetEncoder)16 CharBuffer (java.nio.CharBuffer)15 IOException (java.io.IOException)14 CoderResult (java.nio.charset.CoderResult)13 CharsetDecoder (java.nio.charset.CharsetDecoder)11 Charset (java.nio.charset.Charset)10 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)8 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)6 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 CoreException (org.eclipse.core.runtime.CoreException)5 IStatus (org.eclipse.core.runtime.IStatus)5 Status (org.eclipse.core.runtime.Status)5 HumanReadableException (com.facebook.buck.util.HumanReadableException)4 SequenceInputStream (java.io.SequenceInputStream)4 Path (java.nio.file.Path)4 Date (java.util.Date)4