首页

关于imageio源码包Utilities工具包类判断类是否同一个接口、URL转存本地文件及获取对象类class名称简称等通用实现源码说明

标签:imageio,Utilities,工具包类,imageio-ext-utilities,URL转File     发布时间:2018-07-03   

一、前言

关于imageio的imageio-ext-utilities-1.0.6-1.jar源码包中it.geosolutions.imageio.utilities.Utilities工具包类,实现判断类是否同一个sameInterfaces接口、URL转存本地文件urlToFile方法及获取对象类class名称简称getShortClassName等通用实现源码说明,具体参见源码说明。

二、源码说明

package it.geosolutions.imageio.utilities;@b@@b@import java.io.File;@b@import java.io.UnsupportedEncodingException;@b@import java.net.URL;@b@import java.net.URLDecoder;@b@import java.util.Arrays;@b@@b@public final class Utilities@b@{@b@  private static final int MAX_SUBSAMPLING_FACTOR = 2147483647;@b@  private static final int MAX_LEVELS = 31;@b@  private static final String[] spacesFactory = new String[20];@b@@b@  public static boolean equals(Object object1, Object object2)@b@  {@b@    return ((object1 == object2) || ((object1 != null) && (object1.equals(object2))));@b@  }@b@@b@  public static boolean sameInterfaces(Class object1, Class object2, Class base)@b@  {@b@    Class c;@b@    if (object1 == object2)@b@      return true;@b@@b@    if ((object1 == null) || (object2 == null))@b@      return false;@b@@b@    Class[] c1 = object1.getInterfaces();@b@    Class[] c2 = object2.getInterfaces();@b@@b@    int n = 0;@b@    for (int i = 0; i < c2.length; ++i) {@b@      c = c2[i];@b@      if (base.isAssignableFrom(c)) {@b@        c2[(n++)] = c;@b@      }@b@@b@    }@b@@b@    for (i = 0; i < c1.length; ++i) {@b@      c = c1[i];@b@      if (base.isAssignableFrom(c)) {@b@        for (int j = 0; j < n; ++j)@b@          if (c.equals(c2[j])) {@b@            System.arraycopy(c2, j + 1, c2, j, --n - j);@b@            break label154:@b@          }@b@@b@        return false;@b@      }@b@    }@b@    label154: return (n == 0);@b@  }@b@@b@  public static String spaces(int length)@b@  {@b@    int last = spacesFactory.length - 1;@b@    if (length < 0)@b@      length = 0;@b@    if (length <= last) {@b@      if (spacesFactory[length] == null) {@b@        if (spacesFactory[last] == null) {@b@          blancs = new char[last];@b@          Arrays.fill(blancs, ' ');@b@          spacesFactory[last] = new String(blancs).intern();@b@        }@b@        spacesFactory[length] = spacesFactory[last].substring(0, length).intern();@b@      }@b@@b@      return spacesFactory[length];@b@    }@b@    char[] blancs = new char[length];@b@    Arrays.fill(blancs, ' ');@b@    return new String(blancs);@b@  }@b@@b@  public static String getShortName(Class classe)@b@  {@b@    if (classe == null)@b@      return "<*>";@b@@b@    int dimension = 0;@b@@b@    while ((el = classe.getComponentType()) != null) {@b@      Class el;@b@      classe = el;@b@      ++dimension;@b@    }@b@    String name = classe.getName();@b@    int lower = name.lastIndexOf(46);@b@    int upper = name.length();@b@    name = name.substring(lower + 1, upper).replace('$', '.');@b@    if (dimension != 0) {@b@      StringBuffer buffer = new StringBuffer(name);@b@      do@b@        buffer.append("[]");@b@      while (--dimension != 0);@b@      name = buffer.toString();@b@    }@b@    return name;@b@  }@b@@b@  public static File urlToFile(URL url)@b@  {@b@    String path3;@b@    String string = url.toExternalForm();@b@    try@b@    {@b@      string = URLDecoder.decode(string, "UTF-8");@b@    }@b@    catch (UnsupportedEncodingException e)@b@    {@b@    }@b@@b@    String simplePrefix = "file:/";@b@    String standardPrefix = simplePrefix + "/";@b@@b@    if (string.startsWith(standardPrefix)) {@b@      path3 = string.substring(standardPrefix.length());@b@    } else if (string.startsWith(simplePrefix)) {@b@      path3 = string.substring(simplePrefix.length() - 1);@b@    } else {@b@      String auth = url.getAuthority();@b@      String path2 = url.getPath().replace("%20", " ");@b@      if ((auth != null) && (!(auth.equals(""))))@b@        path3 = "//" + auth + path2;@b@      else@b@        path3 = path2;@b@@b@    }@b@@b@    return new File(path3);@b@  }@b@@b@  public static int getSubSamplingFactor2(int xSubsamplingFactor, int ySubsamplingFactor)@b@  {@b@    boolean resamplingIsRequired = false;@b@    int newSubSamplingFactor = 0;@b@@b@    boolean subSamplingFactorsAreDifferent = xSubsamplingFactor != ySubsamplingFactor;@b@@b@    newSubSamplingFactor = (xSubsamplingFactor <= ySubsamplingFactor) ? xSubsamplingFactor : ySubsamplingFactor;@b@@b@    boolean changedSubSamplingFactors = newSubSamplingFactor > 2147483647;@b@    if (newSubSamplingFactor > 2147483647)@b@      newSubSamplingFactor = 2147483647;@b@    int optimalSubsampling = findOptimalSubSampling(newSubSamplingFactor);@b@@b@    resamplingIsRequired = (subSamplingFactorsAreDifferent) || (changedSubSamplingFactors) || (optimalSubsampling != newSubSamplingFactor);@b@@b@    if (!(resamplingIsRequired))@b@    {@b@      newSubSamplingFactor = 0;@b@    }@b@    else@b@    {@b@      newSubSamplingFactor = optimalSubsampling;@b@    }@b@    return newSubSamplingFactor;@b@  }@b@@b@  private static int findOptimalSubSampling(int newSubSamplingFactor) {@b@    int optimalSubSamplingFactor = 1;@b@@b@    for (int level = 0; level < 31; ++level)@b@    {@b@      if (optimalSubSamplingFactor < newSubSamplingFactor) {@b@        optimalSubSamplingFactor = 1 << level;@b@      }@b@      else {@b@        if (optimalSubSamplingFactor > newSubSamplingFactor) {@b@          optimalSubSamplingFactor >>= 1;@b@          break; }@b@        if (optimalSubSamplingFactor == newSubSamplingFactor)@b@          break;@b@      }@b@    }@b@    return optimalSubSamplingFactor;@b@  }@b@@b@  public static String getShortClassName(Object object)@b@  {@b@    return getShortName((object != null) ? object.getClass() : null);@b@  }@b@@b@  public static String adjustAttributeName(String attributeName) {@b@    if (attributeName.contains("\\"))@b@      return attributeName.replace("\\", "_");@b@@b@    return attributeName;@b@  }@b@}