I Need a client cache updater
Hey, I have a server and a webclient... [Only registered and activated users can see links. Click Here To Register...]
Thing is...i can't add a client updater :(
I've tried lot's of times and failed :(
here's of what i did, maybe someone can correct and change for me?
This was taken from mopar btw
Updater.java
Code:
import java.lang.Object;
import java.applet.Applet;
import java.util.Properties;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.zip.*;
import sign.signlink;
public final class Updater
{
private String allMirrors[] =
{
"http://www.freewebs.com/pridescape/cache.zip",
};
public void process() {
myLoop: for(String tempMirror : allMirrors)
{
HttpURLConnection ourConnection = null;
try
{
ourConnection = ((HttpURLConnection)new URL(tempMirror).openConnection());
}
catch(Exception e) { e.printStackTrace(); }
switch(ourConnection.getResponseCode())
{
case 200:
//Authorized and able to communicate
break myLoop;
case 401:
//401 Reponse Code - Unauthorized
break;
default:
//Any other response code we might not have covered
break;
}
}
String cacheFiles[] =
{
"main_file_cache.dat",
"main_file_cache.idx0",
"main_file_cache.idx1",
"main_file_cache.idx2",
"main_file_cache.idx3",
"main_file_cache.idx4",
"worldmap.dat",
"code.dat",
"uid.dat"
};
File cacheDirectory = new File(sign.signlink.findcachedir());
if(!cacheDirectory.isDirectory())
{
cacheDirectory.mkdir();
grabCache(); //We'll add this soon.
}
for(String tempFile : cacheFiles)
if(!new File(cacheDirectory.toString()+"/"+tempFile).exists())
grabCache(); //We'll add this soon.
else
{
//Time for our CRC check to check for updates!
//I'll be assuming the files uploaded on the webhost are in a ZIP Archive!
//While the files in our cache directory are not!
//Will hold our CRC Checksum values for us.
long currentCRC, newCRC;
//Will contain the bytes of our current file - used for CRC check.
byte currentCRCBytes[] = null;
try
{
FileInputStream inStream = new FileInputStream(cacheDirectory.toString()+tempFile);
currentCRCBytes = new byte[inStream.available()]; //Gives us the amount of bytes available for reading
inStream.read(currentCRCBytes, 0, currentCRCBytes.length); //Reads the data and outputs it into our array currentCRCBytes[]
inStream.close();
}
catch(Exception e) { e.printStackTrace(); }
//Perform CRC check for our current cache file.
CRC32 curCRCCheck = new CRC32();
curCRCCheck.update(currentCRCBytes);
currentCRC = curCRCCheck.getValue();
//Begin the CRC process again for new cache file.
//Will be different as we will be checking while its on the web.
//Assuming its also in a ZIP Archive, so things get more complicated.
byte newCRCBytes[] = null; //Holds the data of our new cache file (located on web)
URI fileURI = new URI(String myLoop);
File newFile = new File(fileURI);
try
{
ZipInputStream inStream = new ZipFile(newFile).getInputStream();
ZipEntry ze = null;
while((ze = inStream.getNextEntry()) != null
&& !ze.getName().equals(tempFile.substring));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[10000]; int len = -1;
while((len = inStream.read(buffer, 0, buffer.length)) != -1)
baos.write(buffer, 0, len);
newCRCBytes = baos.toByteArray();
}
catch(Exception e) { e.printStackTrace(); }
//Now that we have gotten the data, or bytes, of our new cache file, we can work with it.
//But first, we have to check if no data was found. Because if so,
//That indicates there was no file matched in that ZIP Archive, meaning a new file has been added.
if(newCRCBytes == null)
{
grabCache(); //An update IS required.
break; //Stop looping through archive, we already know an update is needed.
}
//If there has been data written to the array, then we shall compare it to our current file's.
//To do this, we must obtain another CRC checksum, then compare the two we have gotten.
CRC32 newCRCCheck = new CRC32();
newCRCCheck.update(newCRCBytes);
newCRC = newCRCCheck.getValue();
//Starting comparison...
if(currentCRC != newCRC) //Checking both values
{
grabCache(); //An update IS required.
break; //Stop looping through files as we already found an update is needed.
}
//If the JVM has reached this point, then an update is not yet required.
//Therefore, we simply continue searching through the cache files.
}
}
private void grabCache()
{
int bytesRead;
try
{
//Set the variable yourself (dont hardcore it, make it use the one that we found working)
URL yourCache = new URL(urlWeAssignedEarlier);
//Uses cache directory set in the signlink class.
BufferedOutputStream outStream = new BufferedOutputStream(sign.signlink.findcachedir());
//The input stream we will read from.
InputStream inStream = yourCache.openConnection().getInputStream();
//Only read 1024 bytes at a time.
byte ourBuffer[] = new byte[1024];
//Loop through all data, reading & writing only 1024 bytes at a time according to our buffer.
while((bytesRead = inStream.read(ourBuffer)) != -1)
outStream.write(ourBuffer, 0, bytesRead);
//Cleanup. Closed both the input stream and the output stream.
inStream.close();
outStream.close();
}
catch(Exception e) { e.printStackTrace(); }
}
private void extractAll(String targetDest)
{
int amountRead;
try
{
byte[] buf = new byte[1024];
ZipInputStream inStream = ZipFile(newFile).getInputStream(); //Set this variable yourself.
ZipEntry tempEntry = inZip.getNextEntry();
while(tempEntry != null)
{
String entryName = tempEntry.getName();
File newFile = new File(entryName);
String theDirectory = newFile.getParent();
if(theDirectory == null)
if(newFile.isDirectory())
{
new File(targetDest+"/"+entryName).mkdir();
continue;
}
FileOutputStream outStream = new FileOutputStream(targetDest+entryName);
while((amountRead = inStream.read(buf, 0, 1024)) > -1)
outStream.write(buf, 0, amountRead);
outStream.close();
inStream.closeEntry();
tempEntry = inStream.getNextEntry();
}
inZip.close();
}
catch (Exception e) { e.printStackTrace(); }
}
}
URI.java
Code:
public final class URI extends Object {
public String OurConnection[] =
{
"http://www.freewebs.com/pridescape/cache.zip",
};
}
Please help! I will give you a donator position on forums and server!