aboutsummaryrefslogtreecommitdiff
path: root/java/org/brotli/dec/Decoder.java
blob: 599588ed2b4800f5b73cc47ef9f1937bbf13afb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package org.brotli.dec;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Decoder {
  private static long decodeBytes(InputStream input, OutputStream output, byte[] buffer)
      throws IOException {
    long totalOut = 0;
    int readBytes;
    BrotliInputStream in = new BrotliInputStream(input);
    in.enableLargeWindow();
    try {
      while ((readBytes = in.read(buffer)) >= 0) {
        output.write(buffer, 0, readBytes);
        totalOut += readBytes;
      }
    } finally {
      in.close();
    }
    return totalOut;
  }

  public static void main(String... args) throws IOException {
    if (args.length != 2) {
      System.out.println("Usage: decoder <compressed_in> <decompressed_out>");
      return;
    }

    byte[] buffer = new byte[1024 * 1024];
    long start;
    long bytesDecoded;
    long end;
    InputStream in = null;
    OutputStream out = null;
    try {
      in = new FileInputStream(args[0]);
      out = new FileOutputStream(args[1]);
      start = System.nanoTime();
      bytesDecoded = decodeBytes(in, out, buffer);
      end = System.nanoTime();
    } finally {
      if (in != null) {
        in.close();  // Hopefully, does not throw exception.
      }
      if (out != null) {
        out.close();
      }
    }

    double timeDelta = (end - start) / 1000000000.0;
    if (timeDelta <= 0) {
      return;
    }
    double mbDecoded = bytesDecoded / (1024.0 * 1024.0);
    System.out.println(mbDecoded / timeDelta + " MiB/s");
  }
}