@Override public String toString() { try { return decode(bytes, 0, length); } catch (CharacterCodingException e) { throw new RuntimeException("Should not have happened ", e); } }
/** * Converts the provided byte array to a String using the UTF-8 encoding. If the input is * malformed, replace by a default value. * * @param utf8 bytes to decode * @return the decoded string * @throws CharacterCodingException if this is not valid UTF-8 */ public static String decode(byte[] utf8) throws CharacterCodingException { return decode(ByteBuffer.wrap(utf8), true); }
public static String decode(byte[] utf8, int start, int length) throws CharacterCodingException { return decode(ByteBuffer.wrap(utf8, start, length), true); }
/** * Converts the provided byte array to a String 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. * * @param utf8 the bytes to decode * @param start where to start from * @param length length of the bytes to decode * @param replace whether to replace malformed characters with U+FFFD * @return the decoded string * @throws CharacterCodingException if the input could not be decoded */ public static String decode(byte[] utf8, int start, int length, boolean replace) throws CharacterCodingException { return decode(ByteBuffer.wrap(utf8, start, length), replace); }
} else if (o instanceof Text) { try { String s = Text.decode(((Text) o).getBytes(), 0, ((Text) o).getLength()); if((s == null) || (s.length() == 0)
public static boolean castToFloat(FieldReader reader, NullableFloat8Holder out) { Object o = reader.readObject(); if (o instanceof Number) { out.value = ((Number) o).doubleValue(); return true; } else if (o instanceof Boolean) { out.value = ((Boolean) o).booleanValue() ? 1 : 0; return true; } else if (o instanceof LocalDateTime) { out.value = toMillis((LocalDateTime) o); return true; } else if (o instanceof Text) { String s; try { s = Text.decode(((Text) o).getBytes(), 0, ((Text) o).getLength()); if (!isNumeric(s)) { return false; } out.value = Double.parseDouble(s); return true; } catch (Exception e) { // TODO: is this the best way? logger.warn("Can't decode text to FLOAT", e); return false; } } else if (o instanceof byte[]) { return false; // TODO } return false; }
public static boolean castToInteger(FieldReader reader, NullableBigIntHolder out) { Object o = reader.readObject(); if (o instanceof Number) { out.value = ((Number) o).longValue(); return true; } else if (o instanceof Boolean) { out.value = ((Boolean) o).booleanValue() ? 1 : 0; return true; } else if (o instanceof LocalDateTime) { out.value = toMillis((LocalDateTime) o); return true; } else if (o instanceof Text) { try { String s = Text.decode(((Text) o).getBytes(), 0, ((Text) o).getLength()); return parseLong(s, out); } catch (CharacterCodingException e) { // TODO: is this the best way? logger.warn("Can't decode text", e); return false; } } else if (o instanceof byte[]) { return false; // TODO } return false; }