Class URLConnection

java.lang.Object
dev.gmitch215.bytebox.net.URLConnection
Direct Known Subclasses:
HttpURLConnection

public abstract class URLConnection extends Object
A connection to a URL, standing in for java.net.URLConnection.

Everything the platform can reach from a URL is HTTP, so the only subclass is HttpURLConnection and this exists for the cast a caller writes rather than for a second implementation. It is replaced along with URL because the class library's version is wired to a connection type built on XMLHttpRequest, which this platform does not have.

Since:
1.0.0
  • Field Details

    • url

      protected final URL url
      The URL this connection was opened for.
    • connected

      protected boolean connected
      Whether connect() has run.
    • doOutput

      protected boolean doOutput
      Whether the caller intends to write a body.
    • doInput

      protected boolean doInput
      Whether the caller intends to read a body.
    • connectTimeout

      protected int connectTimeout
      How long to allow for the connection, in milliseconds, or zero for no limit.
    • readTimeout

      protected int readTimeout
      How long to allow for reading, in milliseconds, or zero for no limit.
  • Constructor Details

    • URLConnection

      protected URLConnection(URL url)
      Parameters:
      url - the URL to connect to
  • Method Details

    • connect

      public abstract void connect() throws IOException
      Sends the request, if it has not been sent.
      Throws:
      IOException - when the request fails
    • getURL

      public URL getURL()
      Returns the URL this connection was opened for.
      Returns:
      the URL this connection was opened for
    • getInputStream

      public abstract InputStream getInputStream() throws IOException
      The body.
      Returns:
      the body, which is read in full before the stream is returned
      Throws:
      IOException - when the request fails
    • getOutputStream

      public abstract OutputStream getOutputStream() throws IOException
      Where to write the request body.
      Returns:
      the stream, which is collected and sent when the response is asked for
      Throws:
      IOException - when the connection does not accept a body
    • getHeaderField

      public abstract String getHeaderField(String name)
      One response header.
      Parameters:
      name - the header name, matched without regard to case
      Returns:
      the value, or null when the response does not carry it
    • getHeaderFieldInt

      public int getHeaderFieldInt(String name, int fallback)
      The value of a response header read as a number.
      Parameters:
      name - the header name
      fallback - what to answer when the header is absent or is not a number
      Returns:
      the number
    • getHeaderFieldLong

      public long getHeaderFieldLong(String name, long fallback)
      The value of a response header read as a number.
      Parameters:
      name - the header name
      fallback - what to answer when the header is absent or is not a number
      Returns:
      the number
    • getContentLength

      public int getContentLength()
      Returns the declared body length, or -1 when the response does not declare one.
      Returns:
      the declared body length, or -1 when the response does not declare one
    • getContentLengthLong

      public long getContentLengthLong()
      Returns the declared body length, or -1 when the response does not declare one.
      Returns:
      the declared body length, or -1 when the response does not declare one
    • getContentType

      public String getContentType()
      Returns the declared media type, or null when the response does not declare one.
      Returns:
      the declared media type, or null when the response does not declare one
    • getContentEncoding

      public String getContentEncoding()
      Returns the declared body encoding, or null when the response does not declare one.
      Returns:
      the declared body encoding, or null when the response does not declare one
    • setRequestProperty

      public abstract void setRequestProperty(String name, String value)
      Sets a request header, replacing any value already set.
      Parameters:
      name - the header name
      value - the value
    • addRequestProperty

      public abstract void addRequestProperty(String name, String value)
      Adds a request header, keeping any value already set.
      Parameters:
      name - the header name
      value - the value
    • getRequestProperty

      public abstract String getRequestProperty(String name)
      One request header, as set.
      Parameters:
      name - the header name
      Returns:
      the value, or null when none was set
    • setDoOutput

      public void setDoOutput(boolean doOutput)
      Whether the caller intends to write a body.
      Parameters:
      doOutput - whether it does
    • getDoOutput

      public boolean getDoOutput()
      Returns whether the caller said it would write a body.
      Returns:
      whether the caller said it would write a body
    • setDoInput

      public void setDoInput(boolean doInput)
      Whether the caller intends to read a body.
      Parameters:
      doInput - whether it does
    • getDoInput

      public boolean getDoInput()
      Returns whether the caller said it would read a body.
      Returns:
      whether the caller said it would read a body
    • setConnectTimeout

      public void setConnectTimeout(int millis)
      How long to allow for the connection.

      The platform gives one deadline for a whole request rather than a separate one for the connection, so this and setReadTimeout(int) are added together.

      Parameters:
      millis - the limit, or zero for no limit
    • getConnectTimeout

      public int getConnectTimeout()
      Returns the connection limit in milliseconds.
      Returns:
      the connection limit in milliseconds
    • setReadTimeout

      public void setReadTimeout(int millis)
      How long to allow for reading.
      Parameters:
      millis - the limit, or zero for no limit
    • getReadTimeout

      public int getReadTimeout()
      Returns the read limit in milliseconds.
      Returns:
      the read limit in milliseconds
    • setUseCaches

      public void setUseCaches(boolean useCaches)
      Whether to use a cached copy, which is ignored: the platform's own cache decides.
      Parameters:
      useCaches - whether to
    • getUseCaches

      public boolean getUseCaches()
      Returns true, because the platform's cache is not something a caller turns off here.
      Returns:
      true, because the platform's cache is not something a caller turns off here