/** * Convert a UTF-8 byte array to String. * * @param bytes * The byte[] containing the UTF-8 String. * @param start * The start position inside the bytes. * @param length * The length of the data, starting from "start" * @return The unicode String */ public static String convertToString(byte[] bytes, int start, int length) { try { return Text.decode(bytes, start, length); } catch (CharacterCodingException e) { return null; } }
/** * Convert text back to string * @see java.lang.Object#toString() */ @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. */ public static String decode(byte[] utf8) throws CharacterCodingException { return decode(ByteBuffer.wrap(utf8), 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. */ public static String decode(byte[] utf8, int start, int length, boolean replace) throws CharacterCodingException { return decode(ByteBuffer.wrap(utf8, start, length), replace); }
public static String decode(byte[] utf8, int start, int length) throws CharacterCodingException { return decode(ByteBuffer.wrap(utf8, start, length), true); }
public void logExceptionMessage(byte[] bytes, int bytesStart, int bytesLength, String dataType) { try { if (LOG.isDebugEnabled()) { String byteData = Text.decode(bytes, bytesStart, bytesLength); 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); } }
@Override public byte[] getBytes(Text writable) { //@TODO There is no reason to decode then encode the string to bytes really //@FIXME this issue with CTRL-CHAR ^0 added by Text at the end of string and Json serd does not like that. try { return Text.decode(writable.getBytes(), 0, writable.getLength()).getBytes(Charset.forName("UTF-8")); } catch (CharacterCodingException e) { throw new RuntimeException(e); } }
@Override protected long doGetField(byte[] bytes, int start, int length) throws ParseException { Date date = null; try { String decoded = Text.decode(bytes, start, length); date = format.parse(decoded); } catch (CharacterCodingException e) { throw new ParseException(e.getMessage(), 0); } calendar.setTime(date); return calendar.get(Calendar.DAY_OF_WEEK); }
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); } }
@Override protected long doGetField(byte[] bytes, int start, int length) throws ParseException { Date date = null; try { date = format.parse(Text.decode(bytes, start, length)); } catch (CharacterCodingException e) { throw new ParseException(e.getMessage(), 0); } calendar.setTime(date); return calendar.getTimeInMillis() / 1000; }
@Override protected long doGetField(byte[] bytes, int start, int length) throws ParseException { Date date = null; try { String decoded = Text.decode(bytes, start, length); date = format.parse(decoded); } catch (CharacterCodingException e) { throw new ParseException(e.getMessage(), 0); } calendar.setTime(date); return calendar.get(Calendar.WEEK_OF_YEAR); } }
/** Read a UTF8 encoded string with a maximum size */ public static String readString(DataInput in, int maxLength) throws IOException { int length = WritableUtils.readVIntInRange(in, 0, maxLength); byte [] bytes = new byte[length]; in.readFully(bytes, 0, length); return decode(bytes); }
@Override protected long doGetField(byte[] bytes, int start, int length) throws ParseException { Date date = null; try { String decoded = Text.decode(bytes, start, length); date = format.parse(decoded); } catch (CharacterCodingException e) { throw new ParseException(e.getMessage(), 0); } calendar.setTime(date); return calendar.get(Calendar.WEEK_OF_YEAR); }
@Override protected long doGetField(byte[] bytes, int start, int length) throws ParseException { Date date = null; try { date = format.parse(Text.decode(bytes, start, length)); } catch (CharacterCodingException e) { throw new ParseException(e.getMessage(), 0); } calendar.setTime(date); return calendar.getTimeInMillis() / 1000; } }
@Override protected long doGetField(byte[] bytes, int start, int length) throws ParseException { Date date = null; try { String decoded = Text.decode(bytes, start, length); date = format.parse(decoded); } catch (CharacterCodingException e) { throw new ParseException(e.getMessage(), 0); } calendar.setTime(date); return calendar.get(Calendar.DAY_OF_WEEK); }
/** * Read a String as a VInt n, followed by n Bytes in Text format. * * @param in * The input stream. * @return The string * @throws IOException */ public static String readString(DataInput in) throws IOException { int length = readVInt(in); if (length == -1) return null; byte[] buffer = new byte[length]; in.readFully(buffer); return Text.decode(buffer); }
private Timestamp decodeTime(byte[] time) { try { return new Timestamp(dateFormat.parse(Text.decode(time)).getTime()); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void init(ByteArrayRef bytes, int start, int length) { String byteData = null; if (!LazyUtils.isNumberMaybe(bytes.getData(), start, length)) { isNull = true; return; } try { byteData = Text.decode(bytes.getData(), start, length); data.set(Double.parseDouble(byteData)); isNull = false; } catch (NumberFormatException e) { isNull = true; LOG.debug("Data not in the Double data type range so converted to null. Given data is :" + byteData, e); } catch (CharacterCodingException e) { isNull = true; LOG.debug("Data not in the Double data type range so converted to null.", e); } }
@Override public void init(ByteArrayRef bytes, int start, int length) { String s = null; try { s = Text.decode(bytes.getData(), start, length); data.set(HiveIntervalDayTime.valueOf(s)); isNull = false; } catch (Exception e) { isNull = true; logExceptionMessage(bytes, start, length, "INTERVAL_DAY_TIME"); } }
@Override public void init(ByteArrayRef bytes, int start, int length) { String s = null; try { s = Text.decode(bytes.getData(), start, length); data.set(HiveIntervalYearMonth.valueOf(s)); isNull = false; } catch (Exception e) { isNull = true; logExceptionMessage(bytes, start, length, "INTERVAL_YEAR_MONTH"); } }