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, 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
import java.io.IOException;
import java.io.InputStream;
import java.net.CacheResponse;
import java.util.List;
import java.util.Map;
public class GFG {
public static void main(String[] args)
throws IOException
{
CacheResponse cr = new CacheResponse() {
public Map<String, List<String> > getHeaders()
throws IOException
{
return null ;
}
public InputStream getBody() throws IOException
{
System.out.println(
"getbody() has been tested" );
return null ;
}
};
cr.getBody();
}
}
|
Output
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
import java.io.IOException;
import java.io.InputStream;
import java.net.CacheResponse;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class GFG {
public static void main(String[] args) throws IOException {
CacheResponse cr = new CacheResponse() {
public Map<String, List<String>> getHeaders() throws IOException {
Map<Integer, List<String>> map = new TreeMap<Integer, List<String>>();
List<String> list= new LinkedList<String>();
list.add( "GFG" );
map.put( 1 ,list);
System.out.println(map);
return null ;
}
public InputStream getBody() throws IOException {
return null ;
}
};
cr.getHeaders();
}
}
|
Last Updated :
01 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...