JsonProductHandler.java

  1. /*
  2.  * JsonProductHandler
  3.  */
  4. package gov.usgs.earthquake.product.io;

  5. import gov.usgs.earthquake.product.Product;
  6. import gov.usgs.earthquake.product.ProductId;
  7. import gov.usgs.util.StreamUtils;

  8. import java.io.OutputStream;
  9. import java.nio.charset.StandardCharsets;

  10. /**
  11.  * Store a product as Json.
  12.  */
  13. public class JsonProductHandler extends ObjectProductHandler {

  14.   /** The output stream where zip content is written. */
  15.   private OutputStream out;

  16.   /**
  17.    * Construct a new ZipProductHandler object.
  18.    *
  19.    * @param out
  20.    *            the output stream where zip content is written.
  21.    */
  22.   public JsonProductHandler(final OutputStream out) {
  23.     this.out = out;
  24.   }

  25.   /**
  26.    * Creates and outputs the zip stream.
  27.    */
  28.   public void onEndProduct(ProductId id) throws Exception {
  29.     super.onEndProduct(id);

  30.     // write json format
  31.     Product product = getProduct();
  32.     byte[] json = new JsonProduct().getJsonObject(product)
  33.         .toString().getBytes(StandardCharsets.UTF_8);
  34.     out.write(json);
  35.   }


  36.   /**
  37.    * Free any resources associated with this handler.
  38.    */
  39.   @Override
  40.   public void close() {
  41.     StreamUtils.closeStream(out);
  42.   }

  43. }