Cherry pick the banglets and such from wip-l1summercoat, this will be the basis for...
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / tasks / MimeDecodeResourcesTask.java
blob4a0e1b88fd59eda32797747c82572cce78529661
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.plugin.tasks;
12 import cc.squirreljme.plugin.util.MIMEFileDecoder;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.nio.file.Files;
16 import java.nio.file.Path;
17 import java.nio.file.StandardOpenOption;
18 import javax.inject.Inject;
19 import org.gradle.api.Task;
20 import org.gradle.language.jvm.tasks.ProcessResources;
22 /**
23 * This task decodes MIME encoded files into resources so that the SquirrelJME
24 * source can remain in pure ASCII.
26 * @since 2020/02/28
28 public class MimeDecodeResourcesTask
29 extends AbstractResourceTask
31 /** The extension for MIME files. */
32 public static final String EXTENSION =
33 ".__mime";
35 /**
36 * Initializes the task.
38 * @param __sourceSet The source set to adjust.
39 * @param __prTask The processing task.
40 * @param __cleanTask The task for cleaning.
41 * @since 2020/02/28
43 @Inject
44 public MimeDecodeResourcesTask(String __sourceSet,
45 ProcessResources __prTask, Task __cleanTask)
47 super(MimeDecodeResourcesTask.EXTENSION, "",
48 __sourceSet, __prTask, __cleanTask);
50 // Set details of this task
51 this.setGroup("squirreljme");
52 this.setDescription("Decodes MIME encoded resource files.");
55 /**
56 * {@inheritDoc}
57 * @since 2020/04/04
59 @Override
60 protected void doTaskAction(Task __task)
62 // Anything here can fail for IO operations
63 try
65 // Process all outputs
66 for (__Output__ output : this.taskOutputs())
68 Path outPath = output.output;
70 // Make sure the output directory exists!
71 Files.createDirectories(outPath.getParent());
73 // Copy decoded MIME data to the output file
74 try (MIMEFileDecoder in = new MIMEFileDecoder(
75 Files.newInputStream(output.input.getAbsolute(),
76 StandardOpenOption.READ));
77 OutputStream out = Files.newOutputStream(outPath,
78 StandardOpenOption.CREATE,
79 StandardOpenOption.TRUNCATE_EXISTING,
80 StandardOpenOption.WRITE))
82 byte[] buf = new byte[4096];
83 for (;;)
85 int rc = in.read(buf);
87 if (rc < 0)
88 break;
90 out.write(buf, 0, rc);
95 catch (IOException e)
97 throw new RuntimeException(e);