/** * Serializes the passed object to a byte array, returning a zero byte array if the passed object * is null, or fails serialization * * @param arg The object to serialize * @return the serialized object */ public static byte[] serializeNoCompress(Object arg) { if (arg == null) return new byte[] {}; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(baos); oos.writeObject(arg); oos.flush(); baos.flush(); return baos.toByteArray(); } catch (Exception ex) { return new byte[] {}; } finally { try { baos.close(); } catch (Exception ex) { /* No Op */ } if (oos != null) try { oos.close(); } catch (Exception ex) { /* No Op */ } } }
/** * Serializes an array of invocation arguments to a byte array * * @param args The arguments to marshall * @return a byte array */ public static byte[] getOutput(Object... args) { if (args == null || args.length == 0) return new byte[] {}; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = null; ObjectOutputStream oos = null; try { gzos = new GZIPOutputStream(baos); oos = new ObjectOutputStream(gzos); oos.writeObject(args); oos.flush(); gzos.finish(); gzos.flush(); baos.flush(); return baos.toByteArray(); } catch (Exception ex) { throw new RuntimeException( "Failed to encode MBeanServerConnection Invocation arguments " + Arrays.toString(args), ex); } finally { try { baos.close(); } catch (Exception ex) { /* No Op */ } if (oos != null) try { oos.close(); } catch (Exception ex) { /* No Op */ } if (gzos != null) try { gzos.close(); } catch (Exception ex) { /* No Op */ } } }