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 exceptionimport java.io.IOException;// Importing superclass of all IO classesimport java.io.InputStream;// Importing Cacheresponse class from java.net package// to create an appletimport java.net.CacheResponse;// Importing List and Map classes// from java.util packageimport java.util.List;import java.util.Map;Â
// Main classpublic 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 exceptionimport java.io.IOException;// Importing superclass of all IO classes import java.io.InputStream; // Importing Cacheresponse class from java.net package// to create an appletimport 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]}
Â

… [Trackback]
[…] Find More Info here on that Topic: geeksforgeeks.org/java-net-cacheresponse-class-in-java/ […]