java.net.CacheRequest Class in Java
Last Updated :
29 Jun, 2021
CacheRequest class is used in java whenever there is a need for, storage of resources in ResponseCache. More precisely instances of this class provide an advantage for the OutputStream object to store resource data into the cache, in fact, This OutputStream object is invoked by protocol handlers. CacheRequest class belongs to java.net package along with other classes such as Authenticator, CacheResponse, ServerSocket, SocketAddress, and many more.
Authenticator | Inet6Address | ServerSocket |
---|
CacheRequest | InetAddress | Socket |
CacheResponse | InetSocketAddress | SocketAddress |
ContentHandler | InterfaceAddress | SocketImpl |
CookieHandler | JarURLConnection | SocketPermission |
CookieManager | MulticastSocket | URI |
DatagramPacket | NetPermission | URL |
Java.Net Package is a container for powerful classes that provides networking infrastructure for java. Now, dwelling onto the two methods present in this class which are as follows:
- abort() Method
- getBody() Method
Method 1: abort() Method
It allows cache store operation to be interrupted and abandoned. Hence, it is used for aborting cache response, whenever an IOException occurs current cache operation is aborted. Hence, in simpler words, it aborts the attempt to cache the response.
Syntax :
public abstract void abort()
Exceptions: It throws IOException if any input-output error is encountered
Method 2: getBody() Method
It simply returns an InputStream from which the response body can be accessed.
Syntax:
public abstract OutputStream getBody ()
Return Type: OutputStream for which the response should be initiated.
Exceptions: It throws IOException if any input-output error is encountered
Implementation:
Example
Java
// Java Program to illustrate CacheRequest Class
// java.net package
// Importing IOException class from
// java,io package
import java.io.IOException;
// Importing all classes from java.net package package
// to create an applet to run on anetwork
import java.net.*;
// Importing List and Map classes from
// java.util package
import java.util.List;
import java.util.Map;
// Main class
public class GFG {
// Main driver method
public static void main(String args[]) throws Exception
{
// Passing the string uri
String ur = "https://www.geeksforgeeks.org";
// Now, calling the constructor of the URI class
URI ur1 = new URI(ur);
// Passing the url
URL url = new URL(
"https://www.geeksforgeeks.org/category/java-programs/");
// Now, calling the constructor of the URLConnection
URLConnection uc = url.openConnection();
ResponseCache responseCache = new ResponseCache() {
// Calling the abstract methods
public CacheResponse get(
URI ur, String reqMethod,
Map<String, List<String> > rqstHeaders)
throws IOException
{
return null;
}
@Override
public CacheRequest put(URI ur,
URLConnection conn)
throws IOException
{
return null;
}
};
// Display message only
System.out.println(
"The put() method has been initiated and called Successfully!");
// The put() method returns the
// CacheRequest for recording
System.out.println("The put() method returns: "
+ responseCache.put(ur1, uc));
}
}
OutputThe put() method has been initiated and called Successfully!
The put() method returns: null
Finally, we are over with the sheer technical stuff related to this class lets finally do have a peek over the application in the real-world of this class. This class is indirectly used in the daily life of a software person as it provides classes for implementing networking applications in wide versatility.
- Web
- File Transfer
- Electronic Mail.
- Remote terminal access
Similar Reads
java.net.CacheResponse Class in Java CacheResponse is an abstract class that represents channels for retrieving resources from the ResponseCache. The objects of this class provide an InputStream that returns the entity-body and the associated response headers. This class inherits methods from java.lang.Object like as clone, equals, fin
3 min read
Java.lang.Class class in Java | Set 1 Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It
15+ min read
java.net.URL Class in Java URL is an acronym of Uniform resource locator. It is a pointer to locate resource in www (World Wide Web). A resource can be anything from a simple text file to any other like images, file directory etc. The typical URL may look like http://www.example.com:80/index.htmlThe URL has the following part
4 min read
java.net.Proxy Class in Java A proxy is an immutable object and type of tool or application or program or system, which helps to protect the information of its users and computers. It acts as a barrier between computer and internet users. A Proxy Object defines the Proxy settings to be used with a connection. Proxy servers are
3 min read
java.net.CookieStore Class in Java A CookieStore is an interface in Java that is a storage area for cookies. It is used to store and retrieve cookies. A CookieStore is responsible for removing HTTPCookie instances that have expired. The CookieManager adds the cookies to the CookieStore for every incoming HTTP response by calling Cook
4 min read
java.net.CookieHandler Class in Java The object of the CookieHandler Class in Java provides a callback mechanism for hooking up an HTTP state management policy implementation into the HTTP protocol handler. The mechanism of how to make HTTP requests and responses is specified by the HTTP state management mechanism. A system-wide Cookie
2 min read