static <T> byte[] toBytes(final T avroObject, final Class<T> theClass) {
    final DatumWriter<T> datumWriter = new SpecificDatumWriter<>(theClass);
    final byte[] theBytes;
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
      final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
      datumWriter.write(avroObject, encoder);
      encoder.flush();
      out.flush();
      theBytes = out.toByteArray();
    } catch (final IOException e) {
      throw new RuntimeException("Unable to serialize an avro object", e);
    }
    return theBytes;
  }
static < T > byte [ ] toBytes ( final T avroObject , final Class < T > theClass ) { final DatumWriter < T > datumWriter = new SpecificDatumWriter < > ( theClass ) ; final byte [ ] theBytes ; try ( final ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ) { final BinaryEncoder encoder = EncoderFactory . get ( ) . binaryEncoder ( out , null ) ; datumWriter . write ( avroObject , encoder ) ; encoder . flush ( ) ; out . flush ( ) ; theBytes = out . toByteArray ( ) ; } catch ( final IOException e ) { throw new RuntimeException ( "Unable to serialize an avro object" , e ) ; } return theBytes ; }
Serializes the given avro object to a byte[].

@param avroObject
@param theClass
@param <T>
@return