1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package dev.aherscu.qa.s3.publisher.maven.plugin.util;
18
19 import java.io.*;
20 import java.util.*;
21 import java.util.zip.*;
22
23 import dev.aherscu.qa.s3.publisher.maven.plugin.config.*;
24
25 public class ManagedFileContentEncoderGZipImpl
26 implements ManagedFileContentEncoder {
27
28 private static final int BUFFER_SIZE = 4096;
29 private static final String CONTENT_ENCODING_GZIP = "gzip";
30 private final File tmpDirectory;
31 private final List<String> supportedContentEncodings;
32
33 public ManagedFileContentEncoderGZipImpl(final File tmpDirectory) {
34 this.tmpDirectory = tmpDirectory;
35 supportedContentEncodings = new ArrayList<>();
36 supportedContentEncodings.add(CONTENT_ENCODING_GZIP);
37 if (!tmpDirectory.exists() && !tmpDirectory.mkdirs())
38 throw new IllegalStateException(
39 "cannot create directory " + tmpDirectory);
40 }
41
42 @Override
43 public File encode(final ManagedFile managedFile) throws Exception {
44 final File file = new File(managedFile.getFilename());
45 File encodedFile;
46 FileInputStream fis = null;
47 GZIPOutputStream gzipos = null;
48 try {
49 final byte[] buffer = new byte[BUFFER_SIZE];
50 encodedFile =
51 File.createTempFile(file.getName() + "-", ".tmp", tmpDirectory);
52 fis = new FileInputStream(file);
53 gzipos = new GZIPOutputStream(new FileOutputStream(encodedFile));
54 int read;
55 do {
56 read = fis.read(buffer, 0, buffer.length);
57 if (read > 0) {
58 gzipos.write(buffer, 0, read);
59 }
60 } while (read >= 0);
61 } finally {
62 if (fis != null) {
63 fis.close();
64 }
65 if (gzipos != null) {
66 gzipos.close();
67 }
68 }
69 return encodedFile;
70 }
71
72 @Override
73 public boolean isContentEncodingSupported(final String contentEncoding) {
74 return supportedContentEncodings
75 .contains(contentEncoding.toLowerCase(Locale.ENGLISH));
76 }
77 }