Search in sources :

Example 71 with UncheckedIOException

use of java.io.UncheckedIOException in project Bytecoder by mirkosertic.

the class FileChannelLinesSpliterator method readLine.

private String readLine() {
    if (reader == null) {
        reader = getBufferedReader();
        buffer = null;
    }
    try {
        return reader.readLine();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 72 with UncheckedIOException

use of java.io.UncheckedIOException in project Bytecoder by mirkosertic.

the class AbstractResourceBundleProvider method getBundle.

/**
 * Returns a {@code ResourceBundle} for the given {@code baseName} and
 * {@code locale}.
 *
 * @implNote
 * The default implementation of this method calls the
 * {@link #toBundleName(String, Locale) toBundleName} method to get the
 * bundle name for the {@code baseName} and {@code locale} and finds the
 * resource bundle of the bundle name local in the module of this provider.
 * It will only search the formats specified when this provider was
 * constructed.
 *
 * @param baseName the base bundle name of the resource bundle, a fully
 *                 qualified class name.
 * @param locale the locale for which the resource bundle should be instantiated
 * @return {@code ResourceBundle} of the given {@code baseName} and
 *         {@code locale}, or {@code null} if no resource bundle is found
 * @throws NullPointerException if {@code baseName} or {@code locale} is
 *         {@code null}
 * @throws UncheckedIOException if any IO exception occurred during resource
 *         bundle loading
 */
@Override
public ResourceBundle getBundle(String baseName, Locale locale) {
    Module module = this.getClass().getModule();
    String bundleName = toBundleName(baseName, locale);
    ResourceBundle bundle = null;
    for (String format : formats) {
        try {
            if (FORMAT_CLASS.equals(format)) {
                bundle = loadResourceBundle(module, bundleName);
            } else if (FORMAT_PROPERTIES.equals(format)) {
                bundle = loadPropertyResourceBundle(module, bundleName);
            }
            if (bundle != null) {
                break;
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    return bundle;
}
Also used : ResourceBundle(java.util.ResourceBundle) PropertyResourceBundle(java.util.PropertyResourceBundle) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 73 with UncheckedIOException

use of java.io.UncheckedIOException in project Bytecoder by mirkosertic.

the class AbstractResourceBundleProvider method loadPropertyResourceBundle.

/*
     * Returns the ResourceBundle of .property format if found in the module
     * of this provider.
     */
private static ResourceBundle loadPropertyResourceBundle(Module module, String bundleName) throws IOException {
    String resourceName = toResourceName(bundleName, "properties");
    if (resourceName == null) {
        return null;
    }
    PrivilegedAction<InputStream> pa = () -> {
        try {
            return module.getResourceAsStream(resourceName);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    };
    try (InputStream stream = AccessController.doPrivileged(pa)) {
        if (stream != null) {
            return new PropertyResourceBundle(stream);
        } else {
            return null;
        }
    } catch (UncheckedIOException e) {
        throw e.getCause();
    }
}
Also used : InputStream(java.io.InputStream) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 74 with UncheckedIOException

use of java.io.UncheckedIOException in project Bytecoder by mirkosertic.

the class ModuleHashes method computeHash.

/**
 * Computes the hash for the given file with the given message digest
 * algorithm.
 *
 * @throws UncheckedIOException if an I/O error occurs
 * @throws RuntimeException if the algorithm is not available
 */
public static byte[] computeHash(Path file, String algorithm) {
    try {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        // memory when jlink is running concurrently on very large jmods
        try (FileChannel fc = FileChannel.open(file)) {
            ByteBuffer bb = ByteBuffer.allocate(32 * 1024);
            while (fc.read(bb) > 0) {
                bb.flip();
                md.update(bb);
                assert bb.remaining() == 0;
                bb.clear();
            }
        }
        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) UncheckedIOException(java.io.UncheckedIOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) ByteBuffer(java.nio.ByteBuffer)

Example 75 with UncheckedIOException

use of java.io.UncheckedIOException in project Bytecoder by mirkosertic.

the class ModuleInfo method doRead.

/**
 * Reads the input as a module-info class file.
 *
 * @throws IOException
 * @throws InvalidModuleDescriptorException
 * @throws IllegalArgumentException if thrown by the ModuleDescriptor.Builder
 *         because an identifier is not a legal Java identifier, duplicate
 *         exports, and many other reasons
 */
private Attributes doRead(DataInput in) throws IOException {
    int magic = in.readInt();
    if (magic != 0xCAFEBABE)
        throw invalidModuleDescriptor("Bad magic number");
    int minor_version = in.readUnsignedShort();
    int major_version = in.readUnsignedShort();
    if (major_version < 53) {
        throw invalidModuleDescriptor("Must be >= 53.0");
    }
    ConstantPool cpool = new ConstantPool(in);
    int access_flags = in.readUnsignedShort();
    if (access_flags != ACC_MODULE)
        throw invalidModuleDescriptor("access_flags should be ACC_MODULE");
    int this_class = in.readUnsignedShort();
    String mn = cpool.getClassName(this_class);
    if (!"module-info".equals(mn))
        throw invalidModuleDescriptor("this_class should be module-info");
    int super_class = in.readUnsignedShort();
    if (super_class > 0)
        throw invalidModuleDescriptor("bad #super_class");
    int interfaces_count = in.readUnsignedShort();
    if (interfaces_count > 0)
        throw invalidModuleDescriptor("Bad #interfaces");
    int fields_count = in.readUnsignedShort();
    if (fields_count > 0)
        throw invalidModuleDescriptor("Bad #fields");
    int methods_count = in.readUnsignedShort();
    if (methods_count > 0)
        throw invalidModuleDescriptor("Bad #methods");
    int attributes_count = in.readUnsignedShort();
    // the names of the attributes found in the class file
    Set<String> attributes = new HashSet<>();
    Builder builder = null;
    Set<String> allPackages = null;
    String mainClass = null;
    ModuleTarget moduleTarget = null;
    ModuleHashes moduelHashes = null;
    ModuleResolution moduleResolution = null;
    for (int i = 0; i < attributes_count; i++) {
        int name_index = in.readUnsignedShort();
        String attribute_name = cpool.getUtf8(name_index);
        int length = in.readInt();
        boolean added = attributes.add(attribute_name);
        if (!added && isAttributeAtMostOnce(attribute_name)) {
            throw invalidModuleDescriptor("More than one " + attribute_name + " attribute");
        }
        switch(attribute_name) {
            case MODULE:
                builder = readModuleAttribute(in, cpool);
                break;
            case MODULE_PACKAGES:
                allPackages = readModulePackagesAttribute(in, cpool);
                break;
            case MODULE_MAIN_CLASS:
                mainClass = readModuleMainClassAttribute(in, cpool);
                break;
            case MODULE_TARGET:
                moduleTarget = readModuleTargetAttribute(in, cpool);
                break;
            case MODULE_HASHES:
                if (parseHashes) {
                    moduelHashes = readModuleHashesAttribute(in, cpool);
                } else {
                    in.skipBytes(length);
                }
                break;
            case MODULE_RESOLUTION:
                moduleResolution = readModuleResolution(in, cpool);
                break;
            default:
                if (isAttributeDisallowed(attribute_name)) {
                    throw invalidModuleDescriptor(attribute_name + " attribute not allowed");
                } else {
                    in.skipBytes(length);
                }
        }
    }
    // the Module attribute is required
    if (builder == null) {
        throw invalidModuleDescriptor(MODULE + " attribute not found");
    }
    // ModuleMainClass  attribute
    if (mainClass != null) {
        builder.mainClass(mainClass);
    }
    // If the ModulePackages attribute is not present then the packageFinder
    // is used to find the set of packages
    boolean usedPackageFinder = false;
    if (allPackages == null && packageFinder != null) {
        try {
            allPackages = packageFinder.get();
        } catch (UncheckedIOException x) {
            throw x.getCause();
        }
        usedPackageFinder = true;
    }
    if (allPackages != null) {
        Set<String> knownPackages = JLMA.packages(builder);
        if (!allPackages.containsAll(knownPackages)) {
            Set<String> missingPackages = new HashSet<>(knownPackages);
            missingPackages.removeAll(allPackages);
            assert !missingPackages.isEmpty();
            String missingPackage = missingPackages.iterator().next();
            String tail;
            if (usedPackageFinder) {
                tail = " not found in module";
            } else {
                tail = " missing from ModulePackages class file attribute";
            }
            throw invalidModuleDescriptor("Package " + missingPackage + tail);
        }
        builder.packages(allPackages);
    }
    ModuleDescriptor descriptor = builder.build();
    return new Attributes(descriptor, moduleTarget, moduelHashes, moduleResolution);
}
Also used : Builder(java.lang.module.ModuleDescriptor.Builder) UncheckedIOException(java.io.UncheckedIOException) ModuleDescriptor(java.lang.module.ModuleDescriptor) HashSet(java.util.HashSet)

Aggregations

UncheckedIOException (java.io.UncheckedIOException)826 IOException (java.io.IOException)786 File (java.io.File)109 Path (java.nio.file.Path)106 ArrayList (java.util.ArrayList)70 InputStream (java.io.InputStream)58 Map (java.util.Map)58 List (java.util.List)55 HashMap (java.util.HashMap)44 Test (org.junit.Test)38 Files (java.nio.file.Files)37 Collectors (java.util.stream.Collectors)37 Stream (java.util.stream.Stream)31 URL (java.net.URL)29 StringWriter (java.io.StringWriter)27 CursorContext (org.neo4j.io.pagecache.context.CursorContext)25 FileInputStream (java.io.FileInputStream)23 HashSet (java.util.HashSet)23 PageCursor (org.neo4j.io.pagecache.PageCursor)22 Optional (java.util.Optional)21