Class URL

java.lang.Object
dev.gmitch215.bytebox.net.URL

public final class URL extends Object
A URL, standing in for java.net.URL.

The compiler substitutes java.net.URL for this one, so a library that opens a connection the ordinary way works unchanged. What it replaces is a version wired to XMLHttpRequest, which this platform does not have: the class library's own java.net.URL compiles and then fails on the first request. Underneath this one is fetch.

Parsing is the platform's, which is the WHATWG algorithm rather than the one the JDK uses. The difference shows on inputs neither is asked for in practice, and it is what makes a relative spec against a base resolve the way a browser resolves it.

 HttpURLConnection connection = (HttpURLConnection) new URL("https://example.com").openConnection();
 connection.setRequestProperty("Accept", "application/json");
 try (InputStream body = connection.getInputStream()) {
 	System.out.println(new String(body.readAllBytes()));
 }

What is not here, because the platform has nothing to back it: registering a stream handler factory, and the getContent family, which resolves a MIME type to a Java object through a content handler. A program reaching for one fails to compile, which is where it should fail.

Since:
1.0.0
  • Constructor Details

  • Method Details

    • getProtocol

      public String getProtocol()
      Returns the scheme, without the colon.
      Returns:
      the scheme, without the colon
    • getHost

      public String getHost()
      Returns the host, without the port.
      Returns:
      the host, without the port
    • getAuthority

      public String getAuthority()
      Returns the host and port as written, which is the host alone on a default port.
      Returns:
      the host and port as written, which is the host alone on a default port
    • getPort

      public int getPort()
      Returns the port, or -1 when the URL leaves the scheme's own port implied.
      Returns:
      the port, or -1 when the URL leaves the scheme's own port implied
    • getDefaultPort

      public int getDefaultPort()
      Returns the port the scheme uses when none is written, or -1 for a scheme with none.
      Returns:
      the port the scheme uses when none is written, or -1 for a scheme with none
    • getPath

      public String getPath()
      Returns the path.
      Returns:
      the path
    • getQuery

      public String getQuery()
      Returns the query, without the question mark, or null when there is none.
      Returns:
      the query, without the question mark, or null when there is none
    • getFile

      public String getFile()
      Returns the path with any query, which is what the request line carries.
      Returns:
      the path with any query, which is what the request line carries
    • getRef

      public String getRef()
      Returns the fragment, without the hash, or null when there is none.
      Returns:
      the fragment, without the hash, or null when there is none
    • getUserInfo

      public String getUserInfo()
      Returns the user information before the host, or null when there is none.
      Returns:
      the user information before the host, or null when there is none
    • openConnection

      public URLConnection openConnection() throws IOException
      Opens a connection to this URL, without sending anything yet.
      Returns:
      the connection, an HttpURLConnection for an HTTP or HTTPS URL
      Throws:
      IOException - when the scheme is one the platform cannot reach
    • openStream

      public InputStream openStream() throws IOException
      Sends a GET and returns the body.
      Returns:
      the body
      Throws:
      IOException - when the request fails
    • toURI

      public URI toURI() throws URISyntaxException
      This URL as a URI.
      Returns:
      the URI
      Throws:
      URISyntaxException - when the URI parser is stricter about it than the URL parser was
    • toExternalForm

      public String toExternalForm()
      Returns the URL as text.
      Returns:
      the URL as text
    • sameFile

      public boolean sameFile(URL other)
      Whether two URLs name the same thing apart from the fragment.
      Parameters:
      other - the other URL
      Returns:
      whether they do
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object