📄 javautils.java
字号:
in = (InputStream) arg; } else { in = handler.getInputStream(); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); int byte1 = -1; while((byte1 = in.read())!=-1) baos.write(byte1); return new OctetStream(baos.toByteArray()); } else if (destClass == DataHandler.class) { return handler; } else { return handler.getContent(); } } } catch (IOException ioe) { } catch (SOAPException se) { } } // If the destination is an array and the source // is a suitable component, return an array with // the single item. if (arg != null && destClass.isArray() && !destClass.getComponentType().equals(Object.class) && destClass.getComponentType().isAssignableFrom(arg.getClass())) { Object array = Array.newInstance(destClass.getComponentType(), 1); Array.set(array, 0, arg); return array; } // in case destClass is array and arg is ArrayOfT class. (ArrayOfT -> T[]) if (arg != null && destClass.isArray()) { Object newArg = ArrayUtil.convertObjectToArray(arg, destClass); if (newArg == null || (newArg != ArrayUtil.NON_CONVERTABLE && newArg != arg)) { return newArg; } } // in case arg is ArrayOfT and destClass is an array. (T[] -> ArrayOfT) if (arg != null && arg.getClass().isArray()) { Object newArg = ArrayUtil.convertArrayToObject(arg, destClass); if (newArg != null) return newArg; } // Return if no conversion is available if (!(arg instanceof Collection || (arg != null && arg.getClass().isArray())) && ((destHeldType == null && argHeldType == null) || (destHeldType != null && argHeldType != null))) { return arg; } // Take care of Holder conversion if (destHeldType != null) { // Convert arg into Holder holding arg. Object newArg = convert(arg, destHeldType); Object argHolder = null; try { argHolder = destClass.newInstance(); setHolderValue(argHolder, newArg); return argHolder; } catch (Exception e) { return arg; } } else if (argHeldType != null) { // Convert arg into the held type try { Object newArg = getHolderValue(arg); return convert(newArg, destClass); } catch (HolderException e) { return arg; } } // Flow to here indicates that neither arg or destClass is a Holder // Check to see if the argument has a prefered destination class. if (arg instanceof ConvertCache && (( ConvertCache) arg).getDestClass() != destClass) { Class hintClass = ((ConvertCache) arg).getDestClass(); if (hintClass != null && hintClass.isArray() && destClass.isArray() && destClass.isAssignableFrom(hintClass)) { destClass = hintClass; destValue = ((ConvertCache) arg).getConvertedValue(destClass); if (destValue != null) return destValue; } } if (arg == null) { return arg; } // The arg may be an array or List int length = 0; if (arg.getClass().isArray()) { length = Array.getLength(arg); } else { length = ((Collection) arg).size(); } if (destClass.isArray()) { if (destClass.getComponentType().isPrimitive()) { Object array = Array.newInstance(destClass.getComponentType(), length); // Assign array elements if (arg.getClass().isArray()) { for (int i = 0; i < length; i++) { Array.set(array, i, Array.get(arg, i)); } } else { int idx = 0; for (Iterator i = ((Collection)arg).iterator(); i.hasNext();) { Array.set(array, idx++, i.next()); } } destValue = array; } else { Object [] array; try { array = (Object [])Array.newInstance(destClass.getComponentType(), length); } catch (Exception e) { return arg; } // Use convert to assign array elements. if (arg.getClass().isArray()) { for (int i = 0; i < length; i++) { array[i] = convert(Array.get(arg, i), destClass.getComponentType()); } } else { int idx = 0; for (Iterator i = ((Collection)arg).iterator(); i.hasNext();) { array[idx++] = convert(i.next(), destClass.getComponentType()); } } destValue = array; } } else if (Collection.class.isAssignableFrom(destClass)) { Collection newList = null; try { // if we are trying to create an interface, build something // that implements the interface if (destClass == Collection.class || destClass == List.class) { newList = new ArrayList(); } else if (destClass == Set.class) { newList = new HashSet(); } else { newList = (Collection)destClass.newInstance(); } } catch (Exception e) { // Couldn't build one for some reason... so forget it. return arg; } if (arg.getClass().isArray()) { for (int j = 0; j < length; j++) { newList.add(Array.get(arg, j)); } } else { for (Iterator j = ((Collection)arg).iterator(); j.hasNext();) { newList.add(j.next()); } } destValue = newList; } else { destValue = arg; } // Store the converted value in the argument if possible. if (arg instanceof ConvertCache) { (( ConvertCache) arg).setConvertedValue(destClass, destValue); } return destValue; } public static boolean isConvertable(Object obj, Class dest) { return isConvertable(obj, dest, false); } public static boolean isConvertable(Object obj, Class dest, boolean isEncoded) { Class src = null; if (obj != null) { if (obj instanceof Class) { src = (Class)obj; } else { src = obj.getClass(); } } else { if(!dest.isPrimitive()) return true; } if (dest == null) return false; if (src != null) { // If we're directly assignable, we're good. if (dest.isAssignableFrom(src)) return true; //Allow mapping of Map's to Map's if (java.util.Map.class.isAssignableFrom(dest) && java.util.Map.class.isAssignableFrom(src)) { return true; } // If it's a wrapping conversion, we're good. if (getWrapperClass(src) == dest) return true; if (getWrapperClass(dest) == src) return true; // If it's List -> Array or vice versa, we're good. if ((Collection.class.isAssignableFrom(src) || src.isArray()) && (Collection.class.isAssignableFrom(dest) || dest.isArray()) && (src.getComponentType() == Object.class || src.getComponentType() == null || dest.getComponentType() == Object.class || dest.getComponentType() == null || isConvertable(src.getComponentType(), dest.getComponentType()))) return true; // If destination is an array, and src is a component, we're good // if we're not encoded! if (!isEncoded && dest.isArray() &&// !dest.getComponentType().equals(Object.class) && dest.getComponentType().isAssignableFrom(src)) return true; if ((src == HexBinary.class && dest == byte[].class) || (src == byte[].class && dest == HexBinary.class)) return true; // Allow mapping of Calendar to Date if (Calendar.class.isAssignableFrom(src) && dest == Date.class) return true; // Allow mapping of Date to Calendar if (Date.class.isAssignableFrom(src) && dest == Calendar.class) return true; // Allow mapping of Calendar to java.sql.Date if (Calendar.class.isAssignableFrom(src) && dest == java.sql.Date.class) return true; } Class destHeld = JavaUtils.getHolderValueType(dest); // Can always convert a null to an empty holder if (src == null) return (destHeld != null); if (destHeld != null) { if (destHeld.isAssignableFrom(src) || isConvertable(src, destHeld)) return true; } // If it's holder -> held or held -> holder, we're good Class srcHeld = JavaUtils.getHolderValueType(src); if (srcHeld != null) { if (dest.isAssignableFrom(srcHeld) || isConvertable(srcHeld, dest)) return true; } // If it's a MIME type mapping and we want a DataHandler, // then we're good. if (dest.getName().equals("javax.activation.DataHandler")) { String name = src.getName(); if (src == String.class || src == java.awt.Image.class || src == OctetStream.class || name.equals("javax.mail.internet.MimeMultipart") || name.equals("javax.xml.transform.Source")) return true; } if (src.getName().equals("javax.activation.DataHandler")) { if (dest == byte[].class) return true; if (dest.isArray() && dest.getComponentType() == byte[].class) return true; } if (dest.getName().equals("javax.activation.DataHandler")) { if (src == Object[].class) return true; if (src.isArray() && src.getComponentType() == Object[].class) return true; } if (obj instanceof java.io.InputStream) { if (dest == OctetStream.class) return true; } if (src.isPrimitive()) { return isConvertable(getWrapperClass(src),dest); } // ArrayOfT -> T[] ? if (dest.isArray()) { if (ArrayUtil.isConvertable(src, dest) == true) return true; } // T[] -> ArrayOfT ? if (src.isArray()) { if (ArrayUtil.isConvertable(src, dest) == true) return true; } return false; } public static Image getImageFromStream(InputStream is) { try { return ImageIOFactory.getImageIO().loadImage(is); } catch (Throwable t) { return null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -