Package dev.gmitch215.bytebox.net
Class URLConnection
java.lang.Object
dev.gmitch215.bytebox.net.URLConnection
- Direct Known Subclasses:
HttpURLConnection
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 Summary
FieldsModifier and TypeFieldDescriptionprotected booleanWhetherconnect()has run.protected intHow long to allow for the connection, in milliseconds, or zero for no limit.protected booleanWhether the caller intends to read a body.protected booleanWhether the caller intends to write a body.protected intHow long to allow for reading, in milliseconds, or zero for no limit.protected final URLThe URL this connection was opened for. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidaddRequestProperty(String name, String value) Adds a request header, keeping any value already set.abstract voidconnect()Sends the request, if it has not been sent.intReturns the connection limit in milliseconds.Returns the declared body encoding, or null when the response does not declare one.intReturns the declared body length, or -1 when the response does not declare one.longReturns the declared body length, or -1 when the response does not declare one.Returns the declared media type, or null when the response does not declare one.booleanReturns whether the caller said it would read a body.booleanReturns whether the caller said it would write a body.abstract StringgetHeaderField(String name) One response header.intgetHeaderFieldInt(String name, int fallback) The value of a response header read as a number.longgetHeaderFieldLong(String name, long fallback) The value of a response header read as a number.abstract InputStreamThe body.abstract OutputStreamWhere to write the request body.intReturns the read limit in milliseconds.abstract StringgetRequestProperty(String name) One request header, as set.getURL()Returns the URL this connection was opened for.booleanReturns true, because the platform's cache is not something a caller turns off here.voidsetConnectTimeout(int millis) How long to allow for the connection.voidsetDoInput(boolean doInput) Whether the caller intends to read a body.voidsetDoOutput(boolean doOutput) Whether the caller intends to write a body.voidsetReadTimeout(int millis) How long to allow for reading.abstract voidsetRequestProperty(String name, String value) Sets a request header, replacing any value already set.voidsetUseCaches(boolean useCaches) Whether to use a cached copy, which is ignored: the platform's own cache decides.
-
Field Details
-
url
The URL this connection was opened for. -
connected
protected boolean connectedWhetherconnect()has run. -
doOutput
protected boolean doOutputWhether the caller intends to write a body. -
doInput
protected boolean doInputWhether the caller intends to read a body. -
connectTimeout
protected int connectTimeoutHow long to allow for the connection, in milliseconds, or zero for no limit. -
readTimeout
protected int readTimeoutHow long to allow for reading, in milliseconds, or zero for no limit.
-
-
Constructor Details
-
URLConnection
- Parameters:
url- the URL to connect to
-
-
Method Details
-
connect
Sends the request, if it has not been sent.- Throws:
IOException- when the request fails
-
getURL
Returns the URL this connection was opened for.- Returns:
- the URL this connection was opened for
-
getInputStream
The body.- Returns:
- the body, which is read in full before the stream is returned
- Throws:
IOException- when the request fails
-
getOutputStream
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
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
The value of a response header read as a number.- Parameters:
name- the header namefallback- what to answer when the header is absent or is not a number- Returns:
- the number
-
getHeaderFieldLong
The value of a response header read as a number.- Parameters:
name- the header namefallback- 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
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
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
Sets a request header, replacing any value already set.- Parameters:
name- the header namevalue- the value
-
addRequestProperty
Adds a request header, keeping any value already set.- Parameters:
name- the header namevalue- the value
-
getRequestProperty
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
-