All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.reandroid.apk.FileMagic Maven / Gradle / Ivy

The newest version!
 /*
  *  Copyright (C) 2022 github.com/REAndroid
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
  *  You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
package com.reandroid.apk;

import com.reandroid.archive.InputSource;

import java.io.IOException;
import java.io.InputStream;

public class FileMagic {

    public static String getExtensionFromMagic(InputSource inputSource) throws IOException {
        byte[] magic=readFileMagic(inputSource);
        if(magic==null){
            return null;
        }
        if(isPng(magic)){
            return ".png";
        }
        if(isJpeg(magic)){
            return ".jpg";
        }
        if(isWebp(magic)){
            return ".webp";
        }
        if(isTtf(magic)){
            return ".ttf";
        }
        return null;
    }

    private static boolean isJpeg(byte[] magic){
        return compareMagic(MAGIC_JPG, magic);
    }
    private static boolean isPng(byte[] magic){
        return compareMagic(MAGIC_PNG, magic);
    }
    private static boolean isWebp(byte[] magic){
        return compareMagic(MAGIC_WEBP, magic);
    }
    private static boolean isTtf(byte[] magic){
        return compareMagic(MAGIC_TTF, magic);
    }
    private static boolean compareMagic(byte[] magic, byte[] readMagic){
        if(magic==null || readMagic==null){
            return false;
        }
        int max=magic.length;
        if(max>readMagic.length){
            max=readMagic.length;
        }
        if(max==0){
            return false;
        }
        for(int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy