?? magicnumber.java
字號:
/** * File and FTP Explorer * Copyright 2002 * BOESCH Vincent * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package javaexplorer.ressource;import java.io.*;import javaexplorer.model.XFile;import javaexplorer.util.registry.*;import javax.swing.ImageIcon;public class MagicNumber { public final static String BMP_DEFAULT_EXTENT = "bmp"; public final static int DESCRIPTION = 100; public final static int HELP = 101; public final static int BMP = 8; /** * Description of the Field */ public final static int DIR = 7; /** * Description of the Field */ public final static int DIR_ROOT = 20; /** * Description of the Field */ public final static int DIR_ROOT_FTP = 21; /** * Description of the Field */ public final static int EXE = 3; private final static String EXE_DEFAULT_EXTENT = "exe"; private final static byte[] EXE_MAGIC = { 0x4D, 0x5A }; private final static byte[] BMP_MAGIC = { 0x42, 0x4d }; /** * Description of the Field */ public final static int EXTERNAL = 6; /** * Description of the Field */ public final static int GIF = 1; private final static String GIF_DEFAULT_EXTENT = "gif"; private final static byte[] GIF_MAGIC = { 71, 73, 70 }; /** * Description of the Field */ public final static int HTML = 5; /** * Description of the Field */ public final static int IMAGE = 10;/* private final static String[] IMG_EXTENT = { "JPEG", "JPG", "GIF", "TIFF", "TIF", "PPM", "PGM", "PNM", "PCX", "PNG", "BMP", "PSD", "TGA", "PICT", "PIC", "DIB", "DDB", "XBM", "XPM", "ICO", "RAST", "ICO", "CUR" };*/ /** * Description of the Field */ public final static int JPG = 2; private final static String JPG_DEFAULT_EXTENT = "jpg"; private final static byte[] JPG_MAGIC = { -1, -40, -1, -32, 0, 16, 74, 70, 73, 70 }; /** * Description of the Field */ public final static int OTHER = -1; /** * Description of the Field */ public final static int ZIP = 4; private final static byte[] ZIP_MAGIC = { 0x50, 0x4B }; /** * Gets the defaultExtentForType attribute * of the MagicNumber class * *@param type Description of the Parameter *@return The defaultExtentForType * value */ public static String getDefaultExtentForType(int type) { switch (type) { case EXE: return EXE_DEFAULT_EXTENT; case GIF: return GIF_DEFAULT_EXTENT; case JPG: return JPG_DEFAULT_EXTENT; case BMP: return BMP_DEFAULT_EXTENT; default: return null; } } /** * Gets the Extension attribute of the * ExplorerUtil class * *@param e_fileName Description of Parameter *@return The Extension value */ public static String getExtension(String e_fileName) { //Recherche du dernier point de separation int index = e_fileName.lastIndexOf("."); if (index != -1) { return e_fileName.substring(index + 1); } return e_fileName; } /** * Determination du type de fichier par * son extensiopn * *@param e_fileName Description of the * Parameter *@return The extensionType * value */ private static int getExtensionType(String e_fileName) { if (e_fileName == null) { return OTHER; } String ext = getExtension(e_fileName); if (Registry.getRegistry().containsExtension(ext)) { return EXTERNAL; } if (ext.equalsIgnoreCase("HTML") || ext.equalsIgnoreCase("HTM")) { return HTML; } if (ext.equalsIgnoreCase("BMP") || ext.equalsIgnoreCase("GIF") || ext.equalsIgnoreCase("PNG") || ext.equalsIgnoreCase("JPG") || ext.equalsIgnoreCase("JPEG")) { return IMAGE; } if (ext.equalsIgnoreCase("ZIP") || ext.equalsIgnoreCase("ARJ") || ext.equalsIgnoreCase("JAR")) { return ZIP; } if (ext.equalsIgnoreCase("EXE")) { return EXE; } return OTHER; } /** * Gets the iconForFile attribute of the * MagicNumber class * *@param f Description of the Parameter *@return The iconForFile value */ public static ImageIcon getIconForXFile(XFile f) { return getIconForMagicType(f.getMagicNumberType()); } /** * Gets the iconForMagicType attribute * of the MagicNumber class * *@param magicType Description of the * Parameter *@return The iconForMagicType * value */ private static ImageIcon getIconForMagicType(int magicType) { switch (magicType) { case DESCRIPTION: return ImageRessource.iiWaitThb; case DIR: return ImageRessource.iiFolderThb; case DIR_ROOT: return ImageRessource.iiDiskThb; case DIR_ROOT_FTP: return ImageRessource.iiDiskFtpThb; case EXE: return ImageRessource.iiExeThb; case EXTERNAL: return ImageRessource.iiExtThb; case IMAGE: return ImageRessource.iiImgThb; case ZIP: return ImageRessource.iiZipThb; default: return ImageRessource.iiFileThb; } } /** * Gets the iconForString attribute of * the MagicNumber class * *@param path Description of the Parameter *@return The iconForString value */ /* public static ImageIcon getIconForString(String path) { int type = MagicNumber.getMagicNumberType(new File(path)); return getIconForMagicType(type); } */ /** * Gets the magicNumberType attribute * of the MagicNumber class * *@param buf Description of the Parameter *@param name Description of the Parameter *@return The magicNumberType value */ public static int getMagicNumberType(byte[] buf, String name) { if (buf == null) { return getExtensionType(name); } else { if (matchesBytes(buf, GIF_MAGIC) || matchesBytes(buf, BMP_MAGIC) || matchesBytes(buf, JPG_MAGIC)) { return IMAGE; } if (matchesBytes(buf, EXE_MAGIC)) { return EXE; } if (matchesBytes(buf, ZIP_MAGIC)) { return ZIP; } return getExtensionType(name); } } /** * Gets the magicNumberType attribute * of the MagicNumber class * *@param f Description of the Parameter *@return The magicNumberType value */ public static int getMagicNumberType(XFile f) { if (f == null) { return OTHER; } if (!(f.isLocal() && f.exists() && f.canRead())) { return getExtensionType(f.getName()); } byte[] b = new byte[10]; try { InputStream is = f.getInputStream(0); is.read(b); is.close(); } catch (Exception e) { //Lecture impossible pour une raison quelconque, on utilise l'extension //pour d閠emriner l'icone } return getMagicNumberType(b, f.getName()); } /** *@param buf Description of the Parameter *@param magic Description of the Parameter *@return Description of the Return * Value */ private static boolean matchesBytes(byte[] buf, byte[] magic) { boolean matches = true; if (buf.length < magic.length) { return false; } for (int i = 0; (i < magic.length) && matches; i++) { matches = (buf[i] == magic[i]); } return matches; } public static int getImageType(XFile xf) { if (xf == null) { return OTHER; } if (!xf.exists()) { return getImageExtensionType(xf.getName()); } byte[] b = new byte[10]; try { InputStream is = is = xf.getInputStream(0); is.read(b); is.close(); } catch (Exception e) { } return getImageType(b, xf.getName()); } /* public static int getImageType( File f ){ if (f == null) { return OTHER; } if (!f.exists()) { return getImageExtensionType(f.getName()); } byte[] b = new byte[10]; try { FileInputStream fis = new FileInputStream(f); fis.read(b); fis.close(); } catch (Exception e) {} return getImageType(b, f.getName()); } */ public static int getImageType(byte[] buf, String fileName) { if (buf == null) { return getImageExtensionType(fileName); } else { if (matchesBytes(buf, GIF_MAGIC)) { return GIF; } if (matchesBytes(buf, BMP_MAGIC)) { return BMP; } if (matchesBytes(buf, JPG_MAGIC)) { return JPG; } return getImageExtensionType(fileName); } } private static int getImageExtensionType(String e_fileName) { if (e_fileName == null) { return OTHER; } String ext = getExtension(e_fileName); if (ext.equalsIgnoreCase("GIF")) { return GIF; } if (ext.equalsIgnoreCase("BMP")) { return BMP; } if (ext.equalsIgnoreCase("JPG") || ext.equalsIgnoreCase("JPEG")) { return JPG; } return OTHER; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -