java.nio.charset.CharacterCodingException Java Examples

The following examples show how to use java.nio.charset.CharacterCodingException. 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: EdgeKeyDecoder.java    From datawave with Apache License 2.0 6 votes vote down vote up
public static STATS_TYPE determineStatsType(Text colFam) {
    int offset = STATS_BYTES.length + 1;
    int secondSlashIndex = 0;
    int numCharsToCheck = Math.min(offset + 1 + STATS_TYPE.getMaxLength(), colFam.getLength());
    
    // faster to just compare bytes with STATS/statsType/ for each statsType
    
    for (int i = offset; i < numCharsToCheck; i++) {
        secondSlashIndex = i;
        if (colFam.getBytes()[i] == COL_SEPARATOR_BYTE) {
            break;
        }
    }
    int count = (secondSlashIndex > offset ? secondSlashIndex - offset : offset);
    try {
        return STATS_TYPE.getStatsType(Text.decode(colFam.getBytes(), offset, count));
    } catch (CharacterCodingException e) {
        // same behavior as EdgeKey.getParts
        throw new RuntimeException("Edge key column encoding exception", e);
    }
}
 
Example #2
Source File: _Private_Utils.java    From ion-java with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes a byte sequence into a string, given a {@link Charset}.
 * <p>
 * This method is preferred to {@link Charset#decode(ByteBuffer)} and
 * {@link String#String(byte[], Charset)} (<em>etc.</em>)
 * since those methods will replace or ignore bad input, and here we throw
 * an exception.
 *
 * @param bytes the data to decode.
 *
 * @return the decoded string, not null.
 *
 * @throws IonException if there's a {@link CharacterCodingException}.
 */
public static String decode(byte[] bytes, Charset charset)
{
    CharsetDecoder decoder = charset.newDecoder();
    try
    {
        CharBuffer buffer = decoder.decode(ByteBuffer.wrap(bytes));
        return buffer.toString();
    }
    catch (CharacterCodingException e)
    {
        String message =
            "Input is not valid " + charset.displayName() + " data";
        throw new IonException(message, e);
    }
}
 
Example #3
Source File: BlockingWriterSenderImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private boolean writeBuffer(final ByteBuffer buffer, final IoCallback callback) {
    StringBuilder builder = new StringBuilder();
    try {
        builder.append(charsetDecoder.decode(buffer));
    } catch (CharacterCodingException e) {
        callback.onException(exchange, this, e);
        return false;
    }
    String data = builder.toString();
    writer.write(data);
    if (writer.checkError()) {
        callback.onException(exchange, this, new IOException());
        return false;
    }
    return true;
}
 
Example #4
Source File: DatabaseConnectionConvertor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static String decodePassword(byte[] bytes) throws CharacterCodingException {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder(); // NOI18N
    ByteBuffer input = ByteBuffer.wrap(bytes);
    int outputLength = (int)(bytes.length * (double)decoder.maxCharsPerByte());
    if (outputLength == 0) {
        return ""; // NOI18N
    }
    char[] chars = new char[outputLength];
    CharBuffer output = CharBuffer.wrap(chars);
    CoderResult result = decoder.decode(input, output, true);
    if (!result.isError() && !result.isOverflow()) {
        result = decoder.flush(output);
    }
    if (result.isError() || result.isOverflow()) {
        throw new CharacterCodingException();
    } else {
        return new String(chars, 0, output.position());
    }
}
 
Example #5
Source File: CompositeReadableBufferTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testEqualsOnSameBackingBuffer() throws CharacterCodingException {
    CompositeReadableBuffer buffer1 = new CompositeReadableBuffer();
    CompositeReadableBuffer buffer2 = new CompositeReadableBuffer();
    CompositeReadableBuffer buffer3 = new CompositeReadableBuffer();

    byte[] data = new byte[] {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

    buffer1.append(data);
    buffer2.append(data);
    buffer3.append(data);

    assertEquals(buffer1, buffer2);
    assertEquals(buffer2, buffer3);
    assertEquals(buffer3, buffer1);

    assertEquals(0, buffer1.position());
    assertEquals(0, buffer2.position());
    assertEquals(0, buffer3.position());
}
 
Example #6
Source File: OldCharset_AbstractTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_CodecDynamic () throws CharacterCodingException {
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    CharBuffer inputCB = CharBuffer.allocate(65536);
    for (int code = 32; code <= 65533; ++code) {
        // icu4c seems to accept any surrogate as a sign that "more is coming",
        // even for charsets like US-ASCII. http://b/10310751
        if (code >= 0xd800 && code <= 0xdfff) {
            continue;
        }
        if (encoder.canEncode((char) code)) {
            inputCB.put((char) code);
        }
    }
    inputCB.rewind();
    ByteBuffer intermediateBB = encoder.encode(inputCB);
    inputCB.rewind();
    intermediateBB.rewind();
    CharBuffer outputCB = decoder.decode(intermediateBB);
    outputCB.rewind();
    assertEqualCBs("decode(encode(A)) must be identical with A!",
            inputCB, outputCB);
}
 
Example #7
Source File: ParseFilter.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
* Checks if the current index of filter string we are on is the beginning of the keyword 'SKIP'
* <p>
* @param filterStringAsByteArray filter string given by the user
* @param indexOfSkip index at which an 'S' was read
* @return true if the keyword 'SKIP' is at the current index
*/
 public static boolean checkForSkip (byte [] filterStringAsByteArray, int indexOfSkip)
   throws CharacterCodingException {

   try {
     if (filterStringAsByteArray[indexOfSkip] == ParseConstants.S &&
         filterStringAsByteArray[indexOfSkip+1] == ParseConstants.K &&
         filterStringAsByteArray[indexOfSkip+2] == ParseConstants.I &&
         filterStringAsByteArray[indexOfSkip+3] == ParseConstants.P &&
         (indexOfSkip == 0 ||
          filterStringAsByteArray[indexOfSkip-1] == ParseConstants.WHITESPACE ||
          filterStringAsByteArray[indexOfSkip-1] == ParseConstants.RPAREN ||
          filterStringAsByteArray[indexOfSkip-1] == ParseConstants.LPAREN) &&
         (filterStringAsByteArray[indexOfSkip+4] == ParseConstants.WHITESPACE ||
          filterStringAsByteArray[indexOfSkip+4] == ParseConstants.LPAREN)) {
       return true;
     } else {
       return false;
     }
   } catch (ArrayIndexOutOfBoundsException e) {
     return false;
   }
 }
 
Example #8
Source File: Message.java    From secor with Apache License 2.0 6 votes vote down vote up
/**
 * Message key and payload may be arbitrary binary strings, so we should make sure we don't throw
 * when logging them by using a CharsetDecoder which replaces bad data. (While in practice `new String(bytes)`
 * does the same thing, the documentation for that method leaves that behavior unspecified.)
 * Additionally, in contexts where Message.toString() will be logged at a high level (including exception
 * messages), we truncate long keys and payloads, which may be very long binary data.
 */
private String bytesToString(byte[] bytes, boolean truncate) {
    CharsetDecoder decoder = Charset.defaultCharset()
            .newDecoder()
            .onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
    ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
    CharBuffer charBuffer;
    try {
        charBuffer = decoder.decode(byteBuffer);
    } catch (CharacterCodingException e) {
        // Shouldn't happen due to choosing REPLACE above, but Java makes us catch it anyway.
        throw new RuntimeException(e);
    }
    String s = charBuffer.toString();
    if (truncate && s.length() > TRUNCATED_STRING_MAX_LEN) {
        return new StringBuilder().append(s, 0, TRUNCATED_STRING_MAX_LEN).append("[...]").toString();
    } else {
        return s;
    }
}
 
Example #9
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 #10
Source File: GremlinAPI.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public String name() {
    // Get the first line of script as the name
    String firstLine = this.gremlin.split("\r\n|\r|\n", 2)[0];
    final Charset charset = Charset.forName(CHARSET);
    final byte[] bytes = firstLine.getBytes(charset);
    if (bytes.length <= MAX_NAME_LENGTH) {
        return firstLine;
    }

    /*
     * Reference https://stackoverflow.com/questions/3576754/truncating-strings-by-bytes
     */
    CharsetDecoder decoder = charset.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.IGNORE);
    decoder.reset();

    ByteBuffer buffer = ByteBuffer.wrap(bytes, 0, MAX_NAME_LENGTH);
    try {
        return decoder.decode(buffer).toString();
    } catch (CharacterCodingException e) {
        throw new HugeException("Failed to decode truncated bytes of " +
                                "gremlin first line", e);
    }
}
 
Example #11
Source File: NTGVisitorDecode.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(String fieldName, LocalDateTime value, IFieldStructure fldStruct, boolean isDefault) {
    validateAttributesMap(fieldName, LocalDateTime.class, fldStruct);
    
    int length = getAttributeValue(fldStruct, NTGProtocolAttribute.Length.toString());
    int offset = getAttributeValue(fldStruct, NTGProtocolAttribute.Offset.toString());
    String dateTimeFormat = getAttributeValue(fldStruct, NTGProtocolAttribute.DateTimeFormat.toString());
    
    validateOffset(fieldName, accumulatedLength, offset);
    
    DateTimeFormatter dateTimeFormatter = DateTimeUtility.createFormatter(dateTimeFormat);
    byte[] array = new byte[length];
    buffer.get(array);
    try {
        String asciiDate = DECODER.get().decode(ByteBuffer.wrap(array)).toString().trim();
        TemporalAccessor dateTime = dateTimeFormatter.parse(asciiDate);
        message.addField(fieldName, DateTimeUtility.toLocalDateTime(dateTime));
        accumulatedLength += length;
    } catch (CharacterCodingException e) {
        throw new EPSCommonException("Problem with decoding the asciiDate = \"" + Arrays.toString(array) + "\"", e);
    }
}
 
Example #12
Source File: Slices.java    From aion with MIT License 6 votes vote down vote up
public static String decodeString(ByteBuffer src, Charset charset) {
    CharsetDecoder decoder = getDecoder(charset);
    CharBuffer dst =
            CharBuffer.allocate((int) ((double) src.remaining() * decoder.maxCharsPerByte()));
    try {
        CoderResult cr = decoder.decode(src, dst, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = decoder.flush(dst);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        throw new IllegalStateException(x);
    }
    return dst.flip().toString();
}
 
Example #13
Source File: Utf8CcsidManager.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
public byte[] convertFromJavaString(String sourceString, Agent agent)
        throws SqlException {
    try {
        ByteBuffer buf = encoder.encode(CharBuffer.wrap(sourceString));

        if (buf.limit() == buf.capacity()) {
            // The length of the encoded representation of the string
            // matches the length of the returned buffer, so just return
            // the backing array.
            return buf.array();
        }

        // Otherwise, copy the interesting bytes into an array with the
        // correct length.
        byte[] bytes = new byte[buf.limit()];
        buf.get(bytes);
        return bytes;
    } catch (CharacterCodingException cce) {
        throw new SqlException(agent.logWriter_,
                new ClientMessageId(SQLState.CANT_CONVERT_UNICODE_TO_UTF8),
                cce);
    }
}
 
Example #14
Source File: ParseReferrerBolt.java    From aws-big-data-blog with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple input,  BasicOutputCollector collector) {
    Record record = (Record)input.getValueByField(DefaultKinesisRecordScheme.FIELD_RECORD);
    ByteBuffer buffer = record.getData();
    String data = null; 
    try {
        data = decoder.decode(buffer).toString();
        JSONObject jsonObject = new JSONObject(data);

        String referrer = jsonObject.getString("referrer");

        int firstIndex = referrer.indexOf('.');
        int nextIndex = referrer.indexOf('.',firstIndex+1);
        collector.emit(new Values(referrer.substring(firstIndex+1,nextIndex)));

    } catch (CharacterCodingException|JSONException|IllegalStateException e) {
        LOG.error("Exception when decoding record ", e);
    }
}
 
Example #15
Source File: Text.java    From hadoop-gpu with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the provided String to bytes using the
 * UTF-8 encoding. If <code>replace</code> is true, then
 * malformed input is replaced with the
 * substitution character, which is U+FFFD. Otherwise the
 * method throws a MalformedInputException.
 * @return ByteBuffer: bytes stores at ByteBuffer.array() 
 *                     and length is ByteBuffer.limit()
 */
public static ByteBuffer encode(String string, boolean replace)
  throws CharacterCodingException {
  CharsetEncoder encoder = ENCODER_FACTORY.get();
  if (replace) {
    encoder.onMalformedInput(CodingErrorAction.REPLACE);
    encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
  }
  ByteBuffer bytes = 
    encoder.encode(CharBuffer.wrap(string.toCharArray()));
  if (replace) {
    encoder.onMalformedInput(CodingErrorAction.REPORT);
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
  }
  return bytes;
}
 
Example #16
Source File: AggregateFunctionOld.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Adds record to data structure for unsorted input data.
 * @throws CharacterCodingException 
 */
public void addUnsortedRecord(DataRecord record) throws CharacterCodingException {
	String keyString;
	for (int i = 0; i < aggregateItems.length; i++) {
		aggregateItems[i].updateUnsorted(record);
		
		keyString = recordKey.getKeyString(record);
		if(!recordMap.containsKey(keyString)) {
			recordMap.put(keyString, record.duplicate());
		}
	}
}
 
Example #17
Source File: NumericDataFieldTest.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Test for {@link org.jetel.data.NumericDataField#toByteBuffer(ByteBuffer, java.nio.charset.CharsetDecoder)}
 * 
 * Only float and double (big endian, little endian) binary formats are tested.
 */
public void test_toByteBuffer() throws CharacterCodingException {
	final byte[] floatBytes1 = {0x43, (byte) 0x9A, (byte) 0x84, 0x00};
	final byte[] floatBytes2 = {(byte) 0xC2, (byte) 0x9E, 0x08, 0x00};
	final byte[] floatBytes3 = {0x00, 0x20, 0x10, (byte) 0xC1};
	final byte[] floatBytes4 = {0x00, 0x00, (byte) 0x80, 0x3B};
	
	final byte[] doubleBytes1 = {0x40, 0x09, 0x21, (byte) 0xFB, 0x53, (byte) 0xC8, (byte) 0xD4, (byte) 0xF1};
	final byte[] doubleBytes2 = {0x3F, (byte) 0xF3, (byte) 0xC0, (byte) 0xCA, 0x42, (byte) 0x8C, 0x59, (byte) 0xFB};
	final byte[] doubleBytes3 = {0x13, 0x72, (byte) 0x91, 0x45, (byte) 0xCA, (byte) 0xC0, 0x23, (byte) 0xC0};
	final byte[] doubleBytes4 = {0x47, 0x0F, (byte) 0xC6, (byte) 0xA6, (byte) 0x83, (byte) 0x98, 0x0C, (byte) 0xC1};
	
	DataFieldMetadata bigEndianFloatMetadata = new DataFieldMetadata("Field", 'i', (short) 4);
	bigEndianFloatMetadata.setFormatStr("BINARY:FLOAT_BIG_ENDIAN");
	DataFieldMetadata littleEndianFloatMetadata = new DataFieldMetadata("Field", 'i', (short) 4);
	littleEndianFloatMetadata.setFormatStr("BINARY:FLOAT_LITTLE_ENDIAN");

	DataFieldMetadata bigEndianDoubleMetadata = new DataFieldMetadata("Field", 'i', (short) 8);
	bigEndianDoubleMetadata.setFormatStr("BINARY:DOUBLE_BIG_ENDIAN");
	DataFieldMetadata littleEndianDoubleMetadata = new DataFieldMetadata("Field", 'i', (short) 8);
	littleEndianDoubleMetadata.setFormatStr("BINARY:DOUBLE_LITTLE_ENDIAN");
	
	// big endian float
	checkToByteBuffer(bigEndianFloatMetadata, 309.03125, floatBytes1);
	checkToByteBuffer(bigEndianFloatMetadata, -79.015625, floatBytes2);
	
	// little endian float
	checkToByteBuffer(littleEndianFloatMetadata, -9.0078125, floatBytes3);
	checkToByteBuffer(littleEndianFloatMetadata, 0.00390625, floatBytes4);
	
	// big endian double
	checkToByteBuffer(bigEndianDoubleMetadata, 3.14159265, doubleBytes1);
	checkToByteBuffer(bigEndianDoubleMetadata, 1.2345678901234567, doubleBytes2);
	
	// little endian double
	checkToByteBuffer(littleEndianDoubleMetadata, -9.876543210987654, doubleBytes3);
	checkToByteBuffer(littleEndianDoubleMetadata, -234256.45643245635, doubleBytes4);
}
 
Example #18
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 #19
Source File: BufferToCharsKt.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void decodeString(CharsetDecoder decoder, ByteBuffer src, CharBuffer dst) {
  try {
    CoderResult cr = decoder.decode(src, dst, true);
    if (!cr.isUnderflow()) {
      cr.throwException();
    }
    cr = decoder.flush(dst);
    if (!cr.isUnderflow()) {
      cr.throwException();
    }
  }
  catch (CharacterCodingException x) {
    throw new IllegalStateException(x);
  }
}
 
Example #20
Source File: StringCoding.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
char[] decode(byte[] ba, int off, int len) {
    int en = scale(len, cd.maxCharsPerByte());
    char[] ca = new char[en];
    if (len == 0)
        return ca;
    if (cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
        return safeTrim(ca, clen, cs, isTrusted);
    } else {
        cd.reset();
        ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
        CharBuffer cb = CharBuffer.wrap(ca);
        try {
            CoderResult cr = cd.decode(bb, cb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = cd.flush(cb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            // Substitution is always enabled,
            // so this shouldn't happen
            throw new Error(x);
        }
        return safeTrim(ca, cb.position(), cs, isTrusted);
    }
}
 
Example #21
Source File: StringCodec.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
protected String decode(Buffer buffer, CharsetDecoder charsetDecoder) {
  try {
    return charsetDecoder.decode(buffer.byteBuffer()).toString();
  } catch (CharacterCodingException e) {
    throw new IllegalStateException(e);
  }
}
 
Example #22
Source File: PropertiesEncoding.java    From netbeans with Apache License 2.0 5 votes vote down vote up
byte[] encodeStringForTests(final String s) throws CharacterCodingException {
    ByteBuffer resultBuf = encode(CharBuffer.wrap(s));
    byte[] resultBufArray = resultBuf.array();
    int resultBufPos = resultBuf.limit();
    if (resultBufPos == resultBufArray.length) {
        return resultBufArray;
    } else {
        byte[] result = new byte[resultBufPos];
        System.arraycopy(resultBufArray, 0, result, 0, resultBufPos);
        return result;
    }
}
 
Example #23
Source File: KeyHelper.java    From vertexium with Apache License 2.0 5 votes vote down vote up
public static Text getColumnQualifierFromPropertyMetadataColumnQualifier(String propertyName, String propertyKey, String visibilityString, String metadataKey, NameSubstitutionStrategy nameSubstitutionStrategy) {
    String name = nameSubstitutionStrategy.deflate(propertyName);
    String key = nameSubstitutionStrategy.deflate(propertyKey);
    metadataKey = nameSubstitutionStrategy.deflate(metadataKey);
    KeyBase.assertNoValueSeparator(name);
    KeyBase.assertNoValueSeparator(key);
    KeyBase.assertNoValueSeparator(visibilityString);
    KeyBase.assertNoValueSeparator(metadataKey);

    int charCount = name.length() + key.length() + visibilityString.length() + metadataKey.length() + 3;
    CharBuffer qualifierChars = (CharBuffer) CharBuffer.allocate(charCount)
        .put(name).put(KeyBase.VALUE_SEPARATOR)
        .put(key).put(KeyBase.VALUE_SEPARATOR)
        .put(visibilityString).put(KeyBase.VALUE_SEPARATOR)
        .put(metadataKey).flip();

    CharsetEncoder encoder = ENCODER_FACTORY.get();
    encoder.reset();

    try {
        ByteBuffer encodedQualifier = encoder.encode(qualifierChars);
        Text result = new Text();
        result.set(encodedQualifier.array(), 0, encodedQualifier.limit());
        return result;
    } catch (CharacterCodingException cce) {
        throw new RuntimeException("This should never happen", cce);
    }
}
 
Example #24
Source File: ITCHVisitorDecode.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(String fieldName, LocalTime value, IFieldStructure fldStruct, boolean isDefault) {
    ProtocolType type = ProtocolType.getEnum(getAttributeValue(fldStruct, TYPE_ATTRIBUTE));
    logger.trace("Visit fieldname = [{}]; fieldType [{}]", fieldName, type);

    int length = getAttributeValue(fldStruct, LENGTH_ATTRIBUTE);
    int pos1 = buffer.position();

    if (type == ProtocolType.TIME) {
        String dateTimePattern = getAttributeValue(fldStruct, DATE_TIME_FORMAT);
        DateTimeFormatter dateTimeFormatter = DateTimeUtility.createFormatter(dateTimePattern);
    
        byte[] array = new byte[length];
        buffer.get(array);
        try {
            String asciiDate = decoder.get().decode(ByteBuffer.wrap(array)).toString().trim();

            TemporalAccessor time = dateTimeFormatter.parse(asciiDate);
        
            msg.addField(fieldName, DateTimeUtility.toLocalTime(time));
        } catch (CharacterCodingException | DateTimeParseException e) {
            throw new EPSCommonException(e);
        }
    } else {
        throw new EPSCommonException("Incorrect type = " + type + " for " + fieldName + " field");
    }

    int pos2 = buffer.position();
    int read = pos2 - pos1;
    if (read != length) {
        throw new EPSCommonException("Read " + read + " bytes, but length = " + length + " for " + fieldName + " field");
    }
}
 
Example #25
Source File: ProtonReadableBufferImplTest.java    From vertx-proton with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadString() throws CharacterCodingException {
  String testString = "test-string-1";
  byte[] asUtf8bytes = testString.getBytes(StandardCharsets.UTF_8);
  ByteBuf byteBuffer = Unpooled.wrappedBuffer(asUtf8bytes);

  ProtonReadableBufferImpl buffer = new ProtonReadableBufferImpl(byteBuffer);

  assertEquals(testString, buffer.readString(StandardCharsets.UTF_8.newDecoder()));
}
 
Example #26
Source File: StringCoding.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example #27
Source File: BytesAndLines.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private byte[] encodeAsUTF8(String s) throws CharacterCodingException {
    // not using s.getBytes here so as to catch unmappable characters
    ByteBuffer bb = UTF_8.newEncoder().encode(CharBuffer.wrap(s));
    byte[] result = new byte[bb.limit()];
    bb.get(result);
    assertTrue(bb.remaining() == 0);
    return result;
}
 
Example #28
Source File: StringCoding.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example #29
Source File: StringCoding.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
byte[] encode(byte coder, byte[] val) {
    // fastpath for ascii compatible
    if (coder == LATIN1 && isASCIICompatible &&
        !hasNegatives(val, 0, val.length)) {
        return Arrays.copyOf(val, val.length);
    }
    int len = val.length >> coder;  // assume LATIN1=0/UTF16=1;
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0) {
        return ba;
    }
    if (ce instanceof ArrayEncoder) {
        int blen = (coder == LATIN1 ) ? ((ArrayEncoder)ce).encodeFromLatin1(val, 0, len, ba)
                                      : ((ArrayEncoder)ce).encodeFromUTF16(val, 0, len, ba);
        if (blen != -1) {
            return safeTrim(ba, blen, isTrusted);
        }
    }
    char[] ca = (coder == LATIN1 ) ? StringLatin1.toChars(val)
                                   : StringUTF16.toChars(val);
    ce.reset();
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca, 0, len);
    try {
        CoderResult cr = ce.encode(cb, bb, true);
        if (!cr.isUnderflow())
            cr.throwException();
        cr = ce.flush(bb);
        if (!cr.isUnderflow())
            cr.throwException();
    } catch (CharacterCodingException x) {
        // Substitution is always enabled,
        // so this shouldn't happen
        throw new Error(x);
    }
    return safeTrim(ba, bb.position(), isTrusted);
}
 
Example #30
Source File: IntegerDataField.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * If the binary format is set, interpret the data accordingly.
 * 
 * Call super otherwise.
 */
@Override
public void fromByteBuffer(CloverBuffer dataBuffer, CharsetDecoder decoder) throws CharacterCodingException {
	if (binaryFormat != null) {
		BigInteger tmpValue;
		
		switch (binaryFormat) {
		case BIG_ENDIAN: case LITTLE_ENDIAN:
			tmpValue = ByteBufferUtils.decodeValue(dataBuffer, binaryFormat.byteOrder);
			break;
		case PACKED_DECIMAL:
			tmpValue = PackedDecimal.parseBigInteger(dataBuffer);
			break;
		default: 
			throw new JetelRuntimeException("Invalid binary format: " + binaryFormat);
		}

		if ((tmpValue.compareTo(INTEGER_MIN_VALUE) < 0)
				|| (tmpValue.compareTo(INTEGER_MAX_VALUE) > 0)) {
			throw new BadDataFormatException("The packed decimal value does not fit into Integer range");
		} else {
			setValue(tmpValue.intValue());
		}
	} else {
		super.fromByteBuffer(dataBuffer, decoder);
	}
}