I have an issue with a HTTP client I am making. I have implemented a cache in the form of a HashMap to prevent a re-download of the exact same file if it has been already downloaded. I also want to be able to update the cache if the file is already downloaded. Here is the code:
try
{
long lastMod = getLastModified(url);
Date d = new Date(lastMod);
outputStream.print("HEAD "+ "/" + pathName + " HTTP/1.1\r\n");
outputStream.print("If-Modified-Since: " + ft.format(d)+ "\r\n");
outputStream.print("Host: " + hostString+"\r\n");
outputStream.print("\r\n");
outputStream.flush();
String t;
while ((t = inputStream.readLine()) != null)
dataIn.add(t);
}
catch(NullPointerException e)
{
dataIn.add("Handle Exception: 200");
}
catch(RuntimeException e2)
{
dataIn.add("Handle Exception: 200");
}
if(dataIn.get(0).contains("304"))
{
//Not Modified
System.out.println(dataIn.get(0));
System.out.println(hostString + "/" + pathName + " is already in local directory\nand is up to date");
}
else if(dataIn.get(0).contains("200"))
{
for(String x : dataIn)
System.out.println(x);
dataIn.clear();
outputStream.print("GET "+ "/" + pathName + " HTTP/1.1\r\n");
outputStream.print("Host: " + hostString+"\r\n");
outputStream.print("\r\n");
outputStream.flush();
boolean blankDetected = false;
int blankIndex = 8;
boolean lastModDetected = false;
int lastModIndex = 0;
String t;
int count = 0;
/*
* The issue is here where the dataIn ArrayList is empty after the loop.
*/
while ((t = inputStream.readLine()) != null)
{
dataIn.add(t);
if(t.equals("\r\n") && !blankDetected)
{
blankDetected = true;
blankIndex = count;
}
if(t.contains("Last-Modified:") && !lastModDetected)
{
lastModDetected = true;
lastModIndex = count;
}
count++;
}
I am using socket connections for the outputstream, and using the loop right at the beginning of the 200 else if, I have verified that the very first HEAD request works perfectly fine.
dataIn however, after the loop, still has 0 elements. Can anyone please help?