@Override
  public int run(InputStream in, PrintStream out, PrintStream err, List<String> args) throws Exception {

    OptionParser optParser = new OptionParser();
    OptionSpec<String> codecOpt = Util.compressionCodecOptionWithDefault(optParser, DataFileConstants.NULL_CODEC);
    OptionSpec<Integer> levelOpt = Util.compressionLevelOption(optParser);
    OptionSet opts = optParser.parse(args.toArray(new String[0]));

    List<String> nargs = (List<String>) opts.nonOptionArguments();
    if (nargs.size() > 2) {
      err.println("Expected at most an input file and output file.");
      optParser.printHelpOn(err);
      return 1;
    }
    InputStream input = in;
    boolean inputNeedsClosing = false;
    if (nargs.size() > 0 && !nargs.get(0).equals("-")) {
      input = Util.openFromFS(nargs.get(0));
      inputNeedsClosing = true;
    }
    OutputStream output = out;
    boolean outputNeedsClosing = false;
    if (nargs.size() > 1 && !nargs.get(1).equals("-")) {
      output = Util.createFromFS(nargs.get(1));
      outputNeedsClosing = true;
    }

    DataFileStream<GenericRecord> reader = new DataFileStream<>(input, new GenericDatumReader<>());
    Schema schema = reader.getSchema();
    DataFileWriter<GenericRecord> writer = new DataFileWriter<>(new GenericDatumWriter<>());
    // unlike the other Avro tools, we default to a null codec, not deflate
    CodecFactory codec = Util.codecFactory(opts, codecOpt, levelOpt, DataFileConstants.NULL_CODEC);
    writer.setCodec(codec);
    for (String key : reader.getMetaKeys()) {
      if (!DataFileWriter.isReservedMeta(key)) {
        writer.setMeta(key, reader.getMeta(key));
      }
    }
    writer.create(schema, output);

    writer.appendAllFrom(reader, true);
    writer.flush();

    if (inputNeedsClosing) {
      input.close();
    }
    if (outputNeedsClosing) {
      output.close();
    }
    writer.close();
    return 0;
  }
@Test
  void recodec() throws Exception {
    String metaKey = "myMetaKey";
    String metaValue = "myMetaValue";

    File inputFile = new File(DIR, "input.avro");

    Schema schema = Schema.create(Type.STRING);
    DataFileWriter<String> writer = new DataFileWriter<>(new GenericDatumWriter<String>(schema));
    writer.setMeta(metaKey, metaValue).create(schema, inputFile);
    // We write some garbage which should be quite compressible by deflate,
    // but is complicated enough that deflate-9 will work better than deflate-1.
    // These values were plucked from thin air and worked on the first try, so
    // don't read too much into them.
    for (int i = 0; i < 100000; i++) {
      writer.append("" + i % 100);
    }
    writer.close();

    File defaultOutputFile = new File(DIR, "default-output.avro");
    File nullOutputFile = new File(DIR, "null-output.avro");
    File deflateDefaultOutputFile = new File(DIR, "deflate-default-output.avro");
    File deflate1OutputFile = new File(DIR, "deflate-1-output.avro");
    File deflate9OutputFile = new File(DIR, "deflate-9-output.avro");

    new RecodecTool().run(new FileInputStream(inputFile), new PrintStream(defaultOutputFile), null, new ArrayList<>());
    new RecodecTool().run(new FileInputStream(inputFile), new PrintStream(nullOutputFile), null,
        Collections.singletonList("--codec=null"));
    new RecodecTool().run(new FileInputStream(inputFile), new PrintStream(deflateDefaultOutputFile), null,
        Collections.singletonList("--codec=deflate"));
    new RecodecTool().run(new FileInputStream(inputFile), new PrintStream(deflate1OutputFile), null,
        asList("--codec=deflate", "--level=1"));
    new RecodecTool().run(new FileInputStream(inputFile), new PrintStream(deflate9OutputFile), null,
        asList("--codec=deflate", "--level=9"));

    // We assume that metadata copying is orthogonal to codec selection, and
    // so only test it for a single file.
    try (DataFileReader<Void> reader = new DataFileReader<Void>(defaultOutputFile, new GenericDatumReader<>())) {
      assertEquals(metaValue, reader.getMetaString(metaKey));
    }

    // The "default" codec should be the same as null.
    assertEquals(defaultOutputFile.length(), nullOutputFile.length());

    // All of the deflated files should be smaller than the null file.
    assertLessThan(deflateDefaultOutputFile.length(), nullOutputFile.length());
    assertLessThan(deflate1OutputFile.length(), nullOutputFile.length());
    assertLessThan(deflate9OutputFile.length(), nullOutputFile.length());

    // The "level 9" file should be smaller than the "level 1" file.
    assertLessThan(deflate9OutputFile.length(), deflate1OutputFile.length());
  }