001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.imaging.internal;
018
019import org.apache.commons.imaging.ImageFormat;
020import org.apache.commons.imaging.ImageFormats;
021import org.apache.commons.imaging.ImageParser;
022import org.apache.commons.imaging.Imaging;
023import org.apache.commons.imaging.common.bytesource.ByteSource;
024
025import java.io.IOException;
026import java.util.function.Predicate;
027import java.util.function.Supplier;
028
029/**
030 * Internal utilities.
031 *
032 * @since 1.0-alpha3
033 */
034public class Util {
035
036    private Util() {}
037
038    public static ImageParser<?> getImageParser(ImageFormat format) {
039        return getImageParser((parser) -> parser.canAcceptType(format), () -> new IllegalArgumentException("Unknown Format: " + format));
040    }
041
042    public static ImageParser<?> getImageParser(String fileExtension) {
043        return getImageParser((parser) -> parser.canAcceptExtension(fileExtension), () -> new IllegalArgumentException("Unknown Extension: " + fileExtension));
044    }
045
046    // This generics suppression is as good as the predicate given. If the predicate violates a generics design,
047    // then there will be an error during runtime.
048    private static ImageParser<?> getImageParser(Predicate<ImageParser<?>> pred, Supplier<? extends RuntimeException> supplier) {
049        return (ImageParser<?>) ImageParser
050                .getAllImageParsers()
051                .stream()
052                .filter(pred)
053                .findFirst()
054                .orElseThrow(supplier);
055    }
056
057    public static ImageParser<?> getImageParser(final ByteSource byteSource) throws IOException {
058        // TODO: circular dependency between Imaging and internal Util class below.
059        final ImageFormat format = Imaging.guessFormat(byteSource);
060        if (!format.equals(ImageFormats.UNKNOWN)) {
061            return Util.getImageParser(format);
062        }
063
064        final String fileName = byteSource.getFileName();
065        if (fileName != null) {
066            return Util.getImageParser(fileName);
067        }
068
069        throw new IllegalArgumentException("Can't parse this format.");
070    }
071}