public static void main(String[] args) throws IOException { Text text = new Text("\u0041"); ObjectWritable writable = new ObjectWritable(text); System.out.println(StringUtils.byteToHexString(serialize(writable))); // 00196f72672e6170616368652e6861646f6f702e696f2e5465787400196f72672e6170616368652e6861646f6f702e696f2e546578740141 // (a)0019 6f72672e6170616368652e6861646f6f702e696f2e54657874, (b)0019 // 6f72672e6170616368652e6861646f6f702e696f2e54657874,(c)0141 /* (1)序列化 ObjectWritable 的声明部分 UTF8.writeString(out, declaredClass.getName()); ==> 0019 6f72672e6170616368652e6861646f6f702e696f2e54657874(第一部分是一个short数值,为该对象class名字的字符串长度,org.apache.hadoop.io.Text,25位=0x0019) (2)序列化 Writable 接口对象的实现类 if (Writable.class.isAssignableFrom(declaredClass)) { // Writable接口实现类 UTF8.writeString(out, instance.getClass().getName()); ((Writable)instance).write(out); } ==> 0019 6f72672e6170616368652e6861646f6f702e696f2e54657874 0141(可变长Text的序列化值,0x01长度,0x41数值内容) */ ObjectWritable srcWritable = new ObjectWritable(Integer.TYPE, 188); ObjectWritable destWritable = new ObjectWritable(); cloneInto(srcWritable, destWritable); System.out.println(serializeToHexString(srcWritable)); // 0003696e74000000bc System.out.println((Integer) destWritable.get()); // 188 }
/** Convert a MD5MD5CRC32FileChecksum to a Json string. */ public static String toJsonString(final MD5MD5CRC32FileChecksum checksum) { if (checksum == null) { return null; } final Map<String, Object> m = new TreeMap<String, Object>(); m.put("algorithm", checksum.getAlgorithmName()); m.put("length", checksum.getLength()); m.put("bytes", StringUtils.byteToHexString(checksum.getBytes())); return toJsonString(MD5MD5CRC32FileChecksum.class, m); }
public String toString() { StringBuilder sb = new StringBuilder(1024); sb.append("id="); String id = StringUtils.byteToHexString(this.getIdentifier()); int idLen = id.length(); sb.append(id.substring(idLen - 6)); sb.append(";k="); sb.append(this.getKind()); sb.append(";s="); sb.append(this.getService()); return sb.toString(); }
/** * Test encoding and decoding of UTF8 outside the basic multilingual plane. * * <p>This is a regression test for HADOOP-9103. */ @Test public void testNonBasicMultilingualPlane() throws Exception { // Test using the "CAT FACE" character (U+1F431) // See http://www.fileformat.info/info/unicode/char/1f431/index.htm String catFace = "\uD83D\uDC31"; // This encodes to 4 bytes in UTF-8: byte[] encoded = catFace.getBytes("UTF-8"); assertEquals(4, encoded.length); assertEquals("f09f90b1", StringUtils.byteToHexString(encoded)); // Decode back to String using our own decoder String roundTrip = UTF8.fromBytes(encoded); assertEquals(catFace, roundTrip); }
public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException { Writable writable = new IntWritable(); ((IntWritable) writable).set(163); byte[] bytes = serialize(writable); assertThat(bytes.length, is(4)); assertThat(StringUtils.byteToHexString(bytes), is("000000a3")); System.out.println("序列化操作完成"); IntWritable newWritable = new IntWritable(); deserialize(newWritable, bytes); assertThat(newWritable.get(), is(163)); System.out.println(newWritable.get()); System.out.println("反序列化操作完成"); }