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, finalize, getClass(), hashCode(), notify(), notifyAll(), toString(), wait().
public abstract class CacheResponse extends Object
Methods: CacheResponse class provides two methods which are as follows:
- getBody() method
- getHeaders() method
Method 1: getBody() method returns an InputStream from which the response body can be accessed.
Syntax:
public abstract InputStream getBody() throws IOException
Parameters: NA
Return Type: This method returns the response body as an InputStream.
Exceptions: I/O Exception been thrown while getting response headers.
Implementation:
Example 1
Java
// Java Program to illustrate CacheResponse Class // showcasing getBody() method // Importing general class of exception // produced by Interrupted I/O exception import java.io.IOException; // Importing superclass of all IO classes import java.io.InputStream; // Importing Cacheresponse class from java.net package // to create an applet import java.net.CacheResponse; // 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 IOException { // Creating an object of CacheResponse class CacheResponse cr = new CacheResponse() { // getHeaders() method returns response headers // as Map public Map<String, List<String> > getHeaders() throws IOException { return null ; } // getBody() method returns response body as // InputStream public InputStream getBody() throws IOException { System.out.println( "getbody() has been tested" ); return null ; } }; // Returning an InputStream from which response body // can be accessed cr.getBody(); } } |
getbody() has been tested
Now illustrating another method as discussed earlier in the header
Method 2: getHeaders() method returns an immutable map from response header field names to lists of field values.
Syntax:
public abstract Map<String,List<String>> getHeaders() throws IOException
Parameters: NA
Return Type: The response of the header as a Map.
Exceptions: I/O Exception been thrown while getting response headers.
Implementation:
Example 2
Java
// Java Program to illustrate CacheResponse Class // showcasing getHeaders() method // Importing general class of exception // produced by Interrupted I/O exception import java.io.IOException; // Importing superclass of all IO classes import java.io.InputStream; // Importing Cacheresponse class from java.net package // to create an applet import java.net.CacheResponse; // Importing List, Linkedlist, Map, Tree, TreeMap classes // from java.util package import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.TreeMap; // Main class // To illustrate getHeaders() method public class GFG { // main driver method public static void main(String[] args) throws IOException { // Creating an object of CacheResponse class CacheResponse cr = new CacheResponse() { // getHeaders() method returns response headers as Map public Map<String, List<String>> getHeaders() throws IOException { // Creating an object of Map class // Object is of type- Integer and List<String> Map<Integer, List<String>> map = new TreeMap<Integer, List<String>>(); // Creating an object of List class List<String> list= new LinkedList<String>(); // Adding element to List object created above // using add() method list.add( "GFG" ); // Adding element to map object created above // using put() method map.put( 1 ,list); // Print Map class object element's System.out.println(map); return null ; } // getBody() method returns response body as InputStream public InputStream getBody() throws IOException { return null ; } }; // Returning an immutable Map from response header cr.getHeaders(); } } |
{1=[GFG]}