< prev index next >

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

Print this page
rev 51919 : 8215281: Use String.isEmpty() when applicable in java.base
Reviewed-by: dfuchs, alanb


1473 
1474     /**
1475      * readObject is called to restore the state of the URL from the
1476      * stream.  It reads the components of the URL and finds the local
1477      * stream handler.
1478      */
1479     private synchronized void readObject(java.io.ObjectInputStream s)
1480             throws IOException, ClassNotFoundException {
1481         GetField gf = s.readFields();
1482         String protocol = (String)gf.get("protocol", null);
1483         if (getURLStreamHandler(protocol) == null) {
1484             throw new IOException("unknown protocol: " + protocol);
1485         }
1486         String host = (String)gf.get("host", null);
1487         int port = gf.get("port", -1);
1488         String authority = (String)gf.get("authority", null);
1489         String file = (String)gf.get("file", null);
1490         String ref = (String)gf.get("ref", null);
1491         int hashCode = gf.get("hashCode", -1);
1492         if (authority == null
1493                 && ((host != null && host.length() > 0) || port != -1)) {
1494             if (host == null)
1495                 host = "";
1496             authority = (port == -1) ? host : host + ":" + port;
1497         }
1498         tempState = new UrlDeserializedState(protocol, host, port, authority,
1499                file, ref, hashCode);
1500     }
1501 
1502     /**
1503      * Replaces the de-serialized object with an URL object.
1504      *
1505      * @return a newly created object from deserialized data
1506      *
1507      * @throws ObjectStreamException if a new object replacing this
1508      * object could not be created
1509      */
1510 
1511    private Object readResolve() throws ObjectStreamException {
1512 
1513         URLStreamHandler handler = null;


1520         } else {
1521             replacementURL = setDeserializedFields(handler);
1522         }
1523         return replacementURL;
1524     }
1525 
1526     private URL setDeserializedFields(URLStreamHandler handler) {
1527         URL replacementURL;
1528         String userInfo = null;
1529         String protocol = tempState.getProtocol();
1530         String host = tempState.getHost();
1531         int port = tempState.getPort();
1532         String authority = tempState.getAuthority();
1533         String file = tempState.getFile();
1534         String ref = tempState.getRef();
1535         int hashCode = tempState.getHashCode();
1536 
1537 
1538         // Construct authority part
1539         if (authority == null
1540             && ((host != null && host.length() > 0) || port != -1)) {
1541             if (host == null)
1542                 host = "";
1543             authority = (port == -1) ? host : host + ":" + port;
1544 
1545             // Handle hosts with userInfo in them
1546             int at = host.lastIndexOf('@');
1547             if (at != -1) {
1548                 userInfo = host.substring(0, at);
1549                 host = host.substring(at+1);
1550             }
1551         } else if (authority != null) {
1552             // Construct user info part
1553             int ind = authority.indexOf('@');
1554             if (ind != -1)
1555                 userInfo = authority.substring(0, ind);
1556         }
1557 
1558         // Construct path and query part
1559         String path = null;
1560         String query = null;


1676     int getPort() {
1677         return port;
1678     }
1679 
1680     String getFile () {
1681         return file;
1682     }
1683 
1684     String getRef () {
1685         return ref;
1686     }
1687 
1688     int getHashCode () {
1689         return hashCode;
1690     }
1691 
1692     String reconstituteUrlString() {
1693 
1694         // pre-compute length of StringBuffer
1695         int len = protocol.length() + 1;
1696         if (authority != null && authority.length() > 0)
1697             len += 2 + authority.length();
1698         if (file != null) {
1699             len += file.length();
1700         }
1701         if (ref != null)
1702             len += 1 + ref.length();
1703         StringBuilder result = new StringBuilder(len);
1704         result.append(protocol);
1705         result.append(":");
1706         if (authority != null && authority.length() > 0) {
1707             result.append("//");
1708             result.append(authority);
1709         }
1710         if (file != null) {
1711             result.append(file);
1712         }
1713         if (ref != null) {
1714             result.append("#");
1715             result.append(ref);
1716         }
1717         return result.toString();
1718     }
1719 }


1473 
1474     /**
1475      * readObject is called to restore the state of the URL from the
1476      * stream.  It reads the components of the URL and finds the local
1477      * stream handler.
1478      */
1479     private synchronized void readObject(java.io.ObjectInputStream s)
1480             throws IOException, ClassNotFoundException {
1481         GetField gf = s.readFields();
1482         String protocol = (String)gf.get("protocol", null);
1483         if (getURLStreamHandler(protocol) == null) {
1484             throw new IOException("unknown protocol: " + protocol);
1485         }
1486         String host = (String)gf.get("host", null);
1487         int port = gf.get("port", -1);
1488         String authority = (String)gf.get("authority", null);
1489         String file = (String)gf.get("file", null);
1490         String ref = (String)gf.get("ref", null);
1491         int hashCode = gf.get("hashCode", -1);
1492         if (authority == null
1493                 && ((host != null && !host.isEmpty()) || port != -1)) {
1494             if (host == null)
1495                 host = "";
1496             authority = (port == -1) ? host : host + ":" + port;
1497         }
1498         tempState = new UrlDeserializedState(protocol, host, port, authority,
1499                file, ref, hashCode);
1500     }
1501 
1502     /**
1503      * Replaces the de-serialized object with an URL object.
1504      *
1505      * @return a newly created object from deserialized data
1506      *
1507      * @throws ObjectStreamException if a new object replacing this
1508      * object could not be created
1509      */
1510 
1511    private Object readResolve() throws ObjectStreamException {
1512 
1513         URLStreamHandler handler = null;


1520         } else {
1521             replacementURL = setDeserializedFields(handler);
1522         }
1523         return replacementURL;
1524     }
1525 
1526     private URL setDeserializedFields(URLStreamHandler handler) {
1527         URL replacementURL;
1528         String userInfo = null;
1529         String protocol = tempState.getProtocol();
1530         String host = tempState.getHost();
1531         int port = tempState.getPort();
1532         String authority = tempState.getAuthority();
1533         String file = tempState.getFile();
1534         String ref = tempState.getRef();
1535         int hashCode = tempState.getHashCode();
1536 
1537 
1538         // Construct authority part
1539         if (authority == null
1540             && ((host != null && !host.isEmpty()) || port != -1)) {
1541             if (host == null)
1542                 host = "";
1543             authority = (port == -1) ? host : host + ":" + port;
1544 
1545             // Handle hosts with userInfo in them
1546             int at = host.lastIndexOf('@');
1547             if (at != -1) {
1548                 userInfo = host.substring(0, at);
1549                 host = host.substring(at+1);
1550             }
1551         } else if (authority != null) {
1552             // Construct user info part
1553             int ind = authority.indexOf('@');
1554             if (ind != -1)
1555                 userInfo = authority.substring(0, ind);
1556         }
1557 
1558         // Construct path and query part
1559         String path = null;
1560         String query = null;


1676     int getPort() {
1677         return port;
1678     }
1679 
1680     String getFile () {
1681         return file;
1682     }
1683 
1684     String getRef () {
1685         return ref;
1686     }
1687 
1688     int getHashCode () {
1689         return hashCode;
1690     }
1691 
1692     String reconstituteUrlString() {
1693 
1694         // pre-compute length of StringBuffer
1695         int len = protocol.length() + 1;
1696         if (authority != null && !authority.isEmpty())
1697             len += 2 + authority.length();
1698         if (file != null) {
1699             len += file.length();
1700         }
1701         if (ref != null)
1702             len += 1 + ref.length();
1703         StringBuilder result = new StringBuilder(len);
1704         result.append(protocol);
1705         result.append(":");
1706         if (authority != null && !authority.isEmpty()) {
1707             result.append("//");
1708             result.append(authority);
1709         }
1710         if (file != null) {
1711             result.append(file);
1712         }
1713         if (ref != null) {
1714             result.append("#");
1715             result.append(ref);
1716         }
1717         return result.toString();
1718     }
1719 }
< prev index next >