-2
public RMI post(PrintStream stream, Object object)
{
try
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    ObjectOutputStream oos = new ObjectOutputStream(baos);

    //

    oos.writeObject(System.rmi);

    oos.flush();

    oos.close();

    //

    stream.println("Class size: " + baos.toByteArray().length);

    stream.println("Class data: " + baos.toByteArray());

    stream.flush();

    stream.close();

    //
}
catch (Exception e)
{
    e.printStackTrace();
}

return this;

}

This prints [B@12843fce instead of the expected underlying bytecode structure. The same operation works find with FileOutputStream but here not with ByteArrayOutputStream. We would really need this to work. Can you spot what's wrong or what's happened?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 4
    What makes you think that doing a println on a byte array shows you byte code? – SurfMan Jun 21 '18 at 21:58
  • Of course casting bytes to characters might influence how they are written to the output of the JVM but here we should find almost no reason to stop and investigate further. Whatever the UTF-XX of Java is should be fine for printing the bytecode which is mainly characters in the UTF-XX character series. – M. Rupplin Jun 21 '18 at 22:01
  • 2
    `baos.toByteArray()` this gives you an array. In Java, array objects do not have a specific implementation of `toString`, so is like calling `Object#toString`, despite the type of your array (this is, calling `toString` on a `String[]` would provide a similar output). Use `Arrays.toString(baos.toByteArray())` instead. – Luiggi Mendoza Jun 21 '18 at 22:05

1 Answers1

2

Don't print the reference to the object, you need a proper way to translate the byte[] to a String, and a common way is Arrays.toString(baos.toByteArray()).

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
developer_hatch
  • 15,898
  • 3
  • 42
  • 75