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 Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read