// build the reference coverage into a byte array
GridCoverageFactory factory = new GridCoverageFactory();
BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_4BYTE_ABGR);
ReferencedEnvelope envelope = new ReferencedEnvelope(0, 10, 0, 10, CRS.decode("EPSG:4326"));
GridCoverage2D test = factory.create("test", bi, envelope);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GeoTiffWriter writer = new GeoTiffWriter(bos);
writer.write(test, null);
writer.dispose();