|
| 1 | +package org.byteskript.skript.api.resource; |
| 2 | + |
| 3 | +import mx.kenzie.foundation.language.PostCompileClass; |
| 4 | + |
| 5 | +import java.io.*; |
| 6 | +import java.nio.charset.Charset; |
| 7 | +import java.util.jar.JarEntry; |
| 8 | +import java.util.zip.ZipOutputStream; |
| 9 | + |
| 10 | +/** |
| 11 | + * Represents a resource, such as a file, that may be copied to a ZIP archive. |
| 12 | + * */ |
| 13 | +public interface Resource { |
| 14 | + /** |
| 15 | + * Opens the source of this resource for reading. |
| 16 | + * @return A new input stream from which bytes are read when the resource is being copied. |
| 17 | + * @throws IOException If an I/O error occurs when opening the stream. |
| 18 | + * */ |
| 19 | + InputStream open() throws IOException; |
| 20 | + |
| 21 | + /** |
| 22 | + * The name that the resource should have in the final ZIP archive. |
| 23 | + * @return A text with the name of the resource. |
| 24 | + * */ |
| 25 | + String getEntryName(); |
| 26 | + |
| 27 | + /** |
| 28 | + * Writes a resource to a ZIP output stream, closing any entries that were previously being written. |
| 29 | + * The resource's stream is opened, read, and closed. |
| 30 | + * @param outputStream The ZIP output stream to write to |
| 31 | + * @param resource The resource to copy |
| 32 | + * @throws IOException If an I/O error occurs when opening the stream or writing to the ZIP output stream. |
| 33 | + * */ |
| 34 | + static void write(final ZipOutputStream outputStream, final Resource resource) throws IOException { |
| 35 | + try (final InputStream inputStream = resource.open()) { |
| 36 | + outputStream.putNextEntry(new JarEntry(resource.getEntryName())); |
| 37 | + inputStream.transferTo(outputStream); |
| 38 | + outputStream.closeEntry(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a new resource containing some bytes. The resource will maintain a reference to the byte array, so |
| 44 | + * modifications to the byte array between calls to {@link Resource#write(ZipOutputStream, Resource)} will result |
| 45 | + * in different contents being written. |
| 46 | + * @param name The name that the resource should have, were it to be added to a ZIP archive |
| 47 | + * @param bytes The byte contents of the resource. |
| 48 | + * @return A resource containing the given bytes as its contents. |
| 49 | + * */ |
| 50 | + static Resource ofBytes(final String name, final byte[] bytes) { |
| 51 | + return new Resource() { |
| 52 | + @Override |
| 53 | + public InputStream open() { |
| 54 | + return new ByteArrayInputStream(bytes); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getEntryName() { |
| 59 | + return name; |
| 60 | + } |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Creates a new resource with the given ZIP entry name, containing the given text encoded using the JVM default character set. |
| 66 | + * Equivalent to {@link Resource#ofString(String, String, Charset)} with {@link Charset#defaultCharset()}. |
| 67 | + * @param name The name that the resource should have, were it to be added to a ZIP archive |
| 68 | + * @param text The text that the resource should contain |
| 69 | + * @return The newly created resource. |
| 70 | + * */ |
| 71 | + static Resource ofString(final String name, final String text) { |
| 72 | + return ofString(name, text, Charset.defaultCharset()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Creates a new resource containing the given text encoded using the given character set. |
| 77 | + * @param name The name that the resource should have, were it to be added to a ZIP archive |
| 78 | + * @param text The text that the resource should contain |
| 79 | + * @param charset The character set that should be used to encode the text |
| 80 | + * @return The newly created resource. |
| 81 | + * */ |
| 82 | + static Resource ofString(final String name, final String text, final Charset charset) { |
| 83 | + return ofBytes(name, text.getBytes(charset)); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Creates a new resource with the internal file name of the compiled class (<code>com/example/Main.class</code>) containing |
| 88 | + * the compiled code of the class. |
| 89 | + * @param compiledClass The compiled class to use as the source of the resource. |
| 90 | + * @return The newly created resource. |
| 91 | + * */ |
| 92 | + static ClassResource ofCompiledClass(final PostCompileClass compiledClass) { |
| 93 | + return new ClassResource(compiledClass); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Creates a new resource that will contain the contents of the given {@link File}. The file is accessed lazily, i.e., |
| 98 | + * if the file is deleted before the resource is written to an archive, the resource will no longer be readable. To |
| 99 | + * load a file in memory and create a resource from its bytes, use {@link Resource#ofImmediateFile} |
| 100 | + * @param name The name that the resource should have, were it to be added to a ZIP archive |
| 101 | + * @param file The file that will act as the source for the contents of the resource |
| 102 | + * @return The newly created resource. |
| 103 | + * */ |
| 104 | + static Resource ofFile(final String name, final File file) { |
| 105 | + return new Resource() { |
| 106 | + @Override |
| 107 | + public InputStream open() throws IOException { |
| 108 | + return new FileInputStream(file); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String getEntryName() { |
| 113 | + return name; |
| 114 | + } |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Immediately reads all bytes in the given input stream and creates a new resource containing the read bytes. |
| 120 | + * This stores the entire stream contents in memory, so the stream may be closed and the resource |
| 121 | + * will remain readable. Equivalent to {@link Resource#ofBytes(String, byte[])} with {@link InputStream#readAllBytes()}. |
| 122 | + * @param name The name that the resource should have, were it to be added to a ZIP archive |
| 123 | + * @param stream The stream that will be read immediately. |
| 124 | + * @return The newly created resource with the contents of the stream. |
| 125 | + * */ |
| 126 | + static Resource ofImmediate(final String name, final InputStream stream) throws IOException { |
| 127 | + return Resource.ofBytes(name, stream.readAllBytes()); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Immediately reads all bytes in the given file and creates a new resource containing the read bytes. |
| 132 | + * This stores the entire file in memory, so the file may be deleted and the resource will remain readable. |
| 133 | + * Equivalent to {@link Resource#ofImmediate(String, InputStream)} with {@link FileInputStream}. |
| 134 | + * @param name The name that the resource should have, were it to be added to a ZIP archive |
| 135 | + * @param file The file that will be read immediately. |
| 136 | + * @return The newly created resource with the contents of the file. |
| 137 | + * */ |
| 138 | + static Resource ofImmediateFile(final String name, final File file) throws IOException { |
| 139 | + return Resource.ofImmediate(name, new FileInputStream(file)); |
| 140 | + } |
| 141 | +} |
0 commit comments