Here you can find the source of getBytes(Object o)
public static byte[] getBytes(Object o)
//package com.java2s; //License from project: Apache License import java.io.*; public class Main { public static byte[] getBytes(Object o) { if (o instanceof String) { return ((String) o).getBytes(); } else if (o instanceof byte[]) { return ((byte[]) o); } else if (o instanceof Serializable) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null; try { out = new ObjectOutputStream(bos); out.writeObject(o);//from w w w .ja v a2 s . c o m return bos.toByteArray(); } catch (Exception e) { return null; } finally { try { if (out != null) { out.close(); } } catch (IOException e) {/**/ } try { bos.close(); } catch (IOException e) {/**/ } } } throw new RuntimeException("Fail to convert object to bytes"); } }