ResourcePoolEntry.java revision 15156:a066fe7b1b42
1/* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25package jdk.tools.jlink.plugin; 26 27import java.io.ByteArrayInputStream; 28import java.io.InputStream; 29import java.io.IOException; 30import java.io.OutputStream; 31import java.io.UncheckedIOException; 32import java.nio.file.Files; 33import java.nio.file.Path; 34import jdk.tools.jlink.internal.ResourcePoolEntryFactory; 35 36/** 37 * A ResourcePoolEntry is the elementary unit of data inside an image. It is 38 * generally a file. e.g.: a java class file, a resource file, a shared library. 39 * <br> 40 * A ResourcePoolEntry is identified by a path of the form: 41 * <ul> 42 * <li>For jimage content: /{module name}/{package1}/.../{packageN}/{file 43 * name}</li> 44 * <li>For other files (shared lib, launchers, config, ...):/{module name}/ 45 * {@literal bin|conf|native}/{dir1}/.../{dirN}/{file name}</li> 46 * </ul> 47 */ 48public interface ResourcePoolEntry { 49 50 /** 51 * Type of module data. 52 * <li> 53 * <ul>CLASS_OR_RESOURCE: A java class or resource file.</ul> 54 * <ul>CONFIG: A configuration file.</ul> 55 * <ul>NATIVE_CMD: A native process launcher.</ul> 56 * <ul>NATIVE_LIB: A native library.</ul> 57 * <ul>OTHER: Other kind of file.</ul> 58 * </li> 59 */ 60 public enum Type { 61 CLASS_OR_RESOURCE, 62 CONFIG, 63 NATIVE_CMD, 64 NATIVE_LIB, 65 OTHER 66 } 67 68 /** 69 * The module name of this ResourcePoolEntry. 70 * 71 * @return The module name. 72 */ 73 public String moduleName(); 74 75 /** 76 * The path of this ResourcePoolEntry. 77 * 78 * @return The module path. 79 */ 80 public String path(); 81 82 /** 83 * The ResourcePoolEntry's type. 84 * 85 * @return The data type. 86 */ 87 public Type type(); 88 89 /** 90 * The ResourcePoolEntry content length. 91 * 92 * @return The content length. 93 */ 94 public long contentLength(); 95 96 /** 97 * The ResourcePoolEntry content as an InputStream. 98 * 99 * @return The resource content as an InputStream. 100 */ 101 public InputStream content(); 102 103 /** 104 * The ResourcePoolEntry content as an array of bytes. 105 * 106 * @return An Array of bytes. 107 */ 108 public default byte[] contentBytes() { 109 try (InputStream is = content()) { 110 return is.readAllBytes(); 111 } catch (IOException ex) { 112 throw new UncheckedIOException(ex); 113 } 114 } 115 116 /** 117 * Write the content of this ResourcePoolEntry to an OutputStream. 118 * 119 * @param out the output stream 120 */ 121 public default void write(OutputStream out) { 122 try { 123 out.write(contentBytes()); 124 } catch (IOException ex) { 125 throw new UncheckedIOException(ex); 126 } 127 } 128 129 /** 130 * Create a ResourcePoolEntry with new content but other information 131 * copied from this ResourcePoolEntry. 132 * 133 * @param content The new resource content. 134 * @return A new ResourcePoolEntry. 135 */ 136 public default ResourcePoolEntry copyWithContent(byte[] content) { 137 return ResourcePoolEntryFactory.create(this, content); 138 } 139 140 /** 141 * Create a ResourcePoolEntry with new content but other information 142 * copied from this ResourcePoolEntry. 143 * 144 * @param file The new resource content. 145 * @return A new ResourcePoolEntry. 146 */ 147 public default ResourcePoolEntry copyWithContent(Path file) { 148 return ResourcePoolEntryFactory.create(this, file); 149 } 150 151 /** 152 * Create a ResourcePoolEntry for a resource of the given type. 153 * 154 * @param path The resource path. 155 * @param type The ResourcePoolEntry type. 156 * @param content The resource content. 157 * @return A new ResourcePoolEntry. 158 */ 159 public static ResourcePoolEntry create(String path, 160 ResourcePoolEntry.Type type, byte[] content) { 161 return ResourcePoolEntryFactory.create(path, type, content); 162 } 163 164 /** 165 * Create a ResourcePoolEntry for a resource of type {@link Type#CLASS_OR_RESOURCE}. 166 * 167 * @param path The resource path. 168 * @param content The resource content. 169 * @return A new ResourcePoolEntry. 170 */ 171 public static ResourcePoolEntry create(String path, byte[] content) { 172 return create(path, Type.CLASS_OR_RESOURCE, content); 173 } 174 175 /** 176 * Create a ResourcePoolEntry for a resource of the given type. 177 * 178 * @param path The resource path. 179 * @param type The ResourcePoolEntry type. 180 * @param file The resource file. 181 * @return A new ResourcePoolEntry. 182 */ 183 public static ResourcePoolEntry create(String path, 184 ResourcePoolEntry.Type type, Path file) { 185 return ResourcePoolEntryFactory.create(path, type, file); 186 } 187 188 /** 189 * Create a ResourcePoolEntry for a resource of type {@link Type#CLASS_OR_RESOURCE}. 190 * 191 * @param path The resource path. 192 * @param file The resource file. 193 * @return A new ResourcePoolEntry. 194 */ 195 public static ResourcePoolEntry create(String path, Path file) { 196 return create(path, Type.CLASS_OR_RESOURCE, file); 197 } 198} 199