1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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
;
23 * This task decodes MIME encoded files into resources so that the SquirrelJME
24 * source can remain in pure ASCII.
28 public class MimeDecodeResourcesTask
29 extends AbstractResourceTask
31 /** The extension for MIME files. */
32 public static final String EXTENSION
=
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.
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.");
60 protected void doTaskAction(Task __task
)
62 // Anything here can fail for IO operations
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];
85 int rc
= in
.read(buf
);
90 out
.write(buf
, 0, rc
);
97 throw new RuntimeException(e
);