|
| 1 | +package com.annimon.ownlang.modules.gzip; |
| 2 | + |
| 3 | +import com.annimon.ownlang.exceptions.TypeException; |
| 4 | +import com.annimon.ownlang.lib.*; |
| 5 | +import com.annimon.ownlang.modules.Module; |
| 6 | +import java.io.ByteArrayInputStream; |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.FileOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.io.OutputStream; |
| 14 | +import java.util.zip.GZIPInputStream; |
| 15 | +import java.util.zip.GZIPOutputStream; |
| 16 | + |
| 17 | +public class gzip implements Module { |
| 18 | + |
| 19 | + @Override |
| 20 | + public void init() { |
| 21 | + Functions.set("gzip", this::gzipFile); |
| 22 | + Functions.set("gzipBytes", this::gzipBytes); |
| 23 | + Functions.set("ungzip", this::ungzipFile); |
| 24 | + Functions.set("ungzipBytes", this::ungzipBytes); |
| 25 | + } |
| 26 | + |
| 27 | + private Value gzipFile(Value[] args) { |
| 28 | + Arguments.check(2, args.length); |
| 29 | + |
| 30 | + final File input = new File(args[0].asString()); |
| 31 | + if (!input.exists() || !input.canRead() || input.isDirectory()) { |
| 32 | + return NumberValue.MINUS_ONE; |
| 33 | + } |
| 34 | + final File output = new File(args[1].asString()); |
| 35 | + if (output.isDirectory()) { |
| 36 | + return NumberValue.MINUS_ONE; |
| 37 | + } |
| 38 | + |
| 39 | + try (InputStream is = new FileInputStream(input); |
| 40 | + OutputStream os = new FileOutputStream(output); |
| 41 | + GZIPOutputStream gzos = new GZIPOutputStream(os)) { |
| 42 | + copy(is, gzos); |
| 43 | + gzos.finish(); |
| 44 | + return NumberValue.ONE; |
| 45 | + } catch (IOException ex) { |
| 46 | + throw new RuntimeException("gzipFile", ex); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private Value gzipBytes(Value[] args) { |
| 51 | + Arguments.check(1, args.length); |
| 52 | + |
| 53 | + if (args[0].type() != Types.ARRAY) { |
| 54 | + throw new TypeException("Byte array expected at first argument"); |
| 55 | + } |
| 56 | + final byte[] input = ValueUtils.toByteArray(((ArrayValue) args[0])); |
| 57 | + try (InputStream is = new ByteArrayInputStream(input); |
| 58 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 59 | + GZIPOutputStream gzos = new GZIPOutputStream(baos)) { |
| 60 | + copy(is, gzos); |
| 61 | + gzos.finish(); |
| 62 | + return ArrayValue.of(baos.toByteArray()); |
| 63 | + } catch (IOException ex) { |
| 64 | + throw new RuntimeException("gzipBytes", ex); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private Value ungzipFile(Value[] args) { |
| 69 | + Arguments.check(2, args.length); |
| 70 | + |
| 71 | + final File input = new File(args[0].asString()); |
| 72 | + if (!input.exists() || !input.canRead() || input.isDirectory()) { |
| 73 | + return NumberValue.MINUS_ONE; |
| 74 | + } |
| 75 | + final File output = new File(args[1].asString()); |
| 76 | + if (output.isDirectory()) { |
| 77 | + return NumberValue.MINUS_ONE; |
| 78 | + } |
| 79 | + |
| 80 | + try (InputStream is = new FileInputStream(input); |
| 81 | + GZIPInputStream gzis = new GZIPInputStream(is); |
| 82 | + OutputStream os = new FileOutputStream(output)) { |
| 83 | + copy(gzis, os); |
| 84 | + return NumberValue.ONE; |
| 85 | + } catch (IOException ex) { |
| 86 | + throw new RuntimeException("ungzipFile", ex); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private Value ungzipBytes(Value[] args) { |
| 91 | + Arguments.check(1, args.length); |
| 92 | + |
| 93 | + if (args[0].type() != Types.ARRAY) { |
| 94 | + throw new TypeException("Byte array expected at first argument"); |
| 95 | + } |
| 96 | + final byte[] input = ValueUtils.toByteArray(((ArrayValue) args[0])); |
| 97 | + try (InputStream is = new ByteArrayInputStream(input); |
| 98 | + GZIPInputStream gzis = new GZIPInputStream(is); |
| 99 | + ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 100 | + copy(gzis, baos); |
| 101 | + return ArrayValue.of(baos.toByteArray()); |
| 102 | + } catch (IOException ex) { |
| 103 | + throw new RuntimeException("ungzipBytes", ex); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private void copy(InputStream is, OutputStream os) throws IOException { |
| 108 | + final byte[] buffer = new byte[8192]; |
| 109 | + int read; |
| 110 | + while ((read = is.read(buffer)) != -1) { |
| 111 | + os.write(buffer, 0, read); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments