View Javadoc
1   /*
2    * Copyright 2023 Adrian Herscu
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }