< prev index next >

src/java.base/share/classes/java/net/URL.java

Print this page
rev 52979 : 8215281: Use String.isEmpty() when applicable in java.base
Reviewed-by: TBD


1496 
1497     /**
1498      * readObject is called to restore the state of the URL from the
1499      * stream.  It reads the components of the URL and finds the local
1500      * stream handler.
1501      */
1502     private synchronized void readObject(java.io.ObjectInputStream s)
1503             throws IOException, ClassNotFoundException {
1504         GetField gf = s.readFields();
1505         String protocol = (String)gf.get("protocol", null);
1506         if (getURLStreamHandler(protocol) == null) {
1507             throw new IOException("unknown protocol: " + protocol);
1508         }
1509         String host = (String)gf.get("host", null);
1510         int port = gf.get("port", -1);
1511         String authority = (String)gf.get("authority", null);
1512         String file = (String)gf.get("file", null);
1513         String ref = (String)gf.get("ref", null);
1514         int hashCode = gf.get("hashCode", -1);
1515         if (authority == null
1516                 && ((host != null && host.length() > 0) || port != -1)) {
1517             if (host == null)
1518                 host = "";
1519             authority = (port == -1) ? host : host + ":" + port;
1520         }
1521         tempState = new UrlDeserializedState(protocol, host, port, authority,
1522                file, ref, hashCode);
1523     }
1524 
1525     /**
1526      * Replaces the de-serialized object with an URL object.
1527      *
1528      * @return a newly created object from deserialized data
1529      *
1530      * @throws ObjectStreamException if a new object replacing this
1531      * object could not be created
1532      */
1533 
1534    private Object readResolve() throws ObjectStreamException {
1535 
1536         URLStreamHandler handler = null;


1543         } else {
1544             replacementURL = setDeserializedFields(handler);
1545         }
1546         return replacementURL;
1547     }
1548 
1549     private URL setDeserializedFields(URLStreamHandler handler) {
1550         URL replacementURL;
1551         String userInfo = null;
1552         String protocol = tempState.getProtocol();
1553         String host = tempState.getHost();
1554         int port = tempState.getPort();
1555         String authority = tempState.getAuthority();
1556         String file = tempState.getFile();
1557         String ref = tempState.getRef();
1558         int hashCode = tempState.getHashCode();
1559 
1560 
1561         // Construct authority part
1562         if (authority == null
1563             && ((host != null && host.length() > 0) || port != -1)) {
1564             if (host == null)
1565                 host = "";
1566             authority = (port == -1) ? host : host + ":" + port;
1567 
1568             // Handle hosts with userInfo in them
1569             int at = host.lastIndexOf('@');
1570             if (at != -1) {
1571                 userInfo = host.substring(0, at);
1572                 host = host.substring(at+1);
1573             }
1574         } else if (authority != null) {
1575             // Construct user info part
1576             int ind = authority.indexOf('@');
1577             if (ind != -1)
1578                 userInfo = authority.substring(0, ind);
1579         }
1580 
1581         // Construct path and query part
1582         String path = null;
1583         String query = null;


1699     int getPort() {
1700         return port;
1701     }
1702 
1703     String getFile () {
1704         return file;
1705     }
1706 
1707     String getRef () {
1708         return ref;
1709     }
1710 
1711     int getHashCode () {
1712         return hashCode;
1713     }
1714 
1715     String reconstituteUrlString() {
1716 
1717         // pre-compute length of StringBuffer
1718         int len = protocol.length() + 1;
1719         if (authority != null && authority.length() > 0)
1720             len += 2 + authority.length();
1721         if (file != null) {
1722             len += file.length();
1723         }
1724         if (ref != null)
1725             len += 1 + ref.length();
1726         StringBuilder result = new StringBuilder(len);
1727         result.append(protocol);
1728         result.append(":");
1729         if (authority != null && authority.length() > 0) {
1730             result.append("//");
1731             result.append(authority);
1732         }
1733         if (file != null) {
1734             result.append(file);
1735         }
1736         if (ref != null) {
1737             result.append("#");
1738             result.append(ref);
1739         }
1740         return result.toString();
1741     }
1742 }


1496 
1497     /**
1498      * readObject is called to restore the state of the URL from the
1499      * stream.  It reads the components of the URL and finds the local
1500      * stream handler.
1501      */
1502     private synchronized void readObject(java.io.ObjectInputStream s)
1503             throws IOException, ClassNotFoundException {
1504         GetField gf = s.readFields();
1505         String protocol = (String)gf.get("protocol", null);
1506         if (getURLStreamHandler(protocol) == null) {
1507             throw new IOException("unknown protocol: " + protocol);
1508         }
1509         String host = (String)gf.get("host", null);
1510         int port = gf.get("port", -1);
1511         String authority = (String)gf.get("authority", null);
1512         String file = (String)gf.get("file", null);
1513         String ref = (String)gf.get("ref", null);
1514         int hashCode = gf.get("hashCode", -1);
1515         if (authority == null
1516                 && ((host != null && !host.isEmpty()) || port != -1)) {
1517             if (host == null)
1518                 host = "";
1519             authority = (port == -1) ? host : host + ":" + port;
1520         }
1521         tempState = new UrlDeserializedState(protocol, host, port, authority,
1522                file, ref, hashCode);
1523     }
1524 
1525     /**
1526      * Replaces the de-serialized object with an URL object.
1527      *
1528      * @return a newly created object from deserialized data
1529      *
1530      * @throws ObjectStreamException if a new object replacing this
1531      * object could not be created
1532      */
1533 
1534    private Object readResolve() throws ObjectStreamException {
1535 
1536         URLStreamHandler handler = null;


1543         } else {
1544             replacementURL = setDeserializedFields(handler);
1545         }
1546         return replacementURL;
1547     }
1548 
1549     private URL setDeserializedFields(URLStreamHandler handler) {
1550         URL replacementURL;
1551         String userInfo = null;
1552         String protocol = tempState.getProtocol();
1553         String host = tempState.getHost();
1554         int port = tempState.getPort();
1555         String authority = tempState.getAuthority();
1556         String file = tempState.getFile();
1557         String ref = tempState.getRef();
1558         int hashCode = tempState.getHashCode();
1559 
1560 
1561         // Construct authority part
1562         if (authority == null
1563             && ((host != null && !host.isEmpty()) || port != -1)) {
1564             if (host == null)
1565                 host = "";
1566             authority = (port == -1) ? host : host + ":" + port;
1567 
1568             // Handle hosts with userInfo in them
1569             int at = host.lastIndexOf('@');
1570             if (at != -1) {
1571                 userInfo = host.substring(0, at);
1572                 host = host.substring(at+1);
1573             }
1574         } else if (authority != null) {
1575             // Construct user info part
1576             int ind = authority.indexOf('@');
1577             if (ind != -1)
1578                 userInfo = authority.substring(0, ind);
1579         }
1580 
1581         // Construct path and query part
1582         String path = null;
1583         String query = null;


1699     int getPort() {
1700         return port;
1701     }
1702 
1703     String getFile () {
1704         return file;
1705     }
1706 
1707     String getRef () {
1708         return ref;
1709     }
1710 
1711     int getHashCode () {
1712         return hashCode;
1713     }
1714 
1715     String reconstituteUrlString() {
1716 
1717         // pre-compute length of StringBuffer
1718         int len = protocol.length() + 1;
1719         if (authority != null && !authority.isEmpty())
1720             len += 2 + authority.length();
1721         if (file != null) {
1722             len += file.length();
1723         }
1724         if (ref != null)
1725             len += 1 + ref.length();
1726         StringBuilder result = new StringBuilder(len);
1727         result.append(protocol);
1728         result.append(":");
1729         if (authority != null && !authority.isEmpty()) {
1730             result.append("//");
1731             result.append(authority);
1732         }
1733         if (file != null) {
1734             result.append(file);
1735         }
1736         if (ref != null) {
1737             result.append("#");
1738             result.append(ref);
1739         }
1740         return result.toString();
1741     }
1742 }
< prev index next >