Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean needsUpdates() {
- URLConnection conn;
- InputStream in = null;
- try {
- /**
- * Gets old file
- */
- byte[] file = null;
- try {
- file = FileOperations.readFile(signlink.findcachedir() + "sprites.idx");
- } catch (RuntimeException e) {
- e.printStackTrace();
- }
- if (file == null)
- return true;
- ByteBuffer oldIndex = new ByteBuffer(file);
- DataInputStream oldindexFile = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(oldIndex.buffer)));
- /**
- * Download the index file from website, we use this to compare our sprites and check if any are missing
- */
- ByteBuffer newIndex = new ByteBuffer();
- DataInputStream newindexFile = null;
- URL url = new URL("http://bsclient.com/update/sprite/sprites.idx");
- conn = url.openConnection();
- in = conn.getInputStream();
- byte[] data = new byte[500];
- int numRead;
- long numWritten = 0;
- int length = conn.getContentLength();
- while((numRead = in.read(data)) != -1) {
- newIndex.writeBytes(data, 0, numRead);
- numWritten += numRead;
- int percentage = (int)(((double)numWritten / (double)length) * 100D);
- client.drawSmothLoading(percentage, "Fetching Update File ["+(double)numWritten +"/"+ (double)length+"]");
- }
- newindexFile = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(oldIndex.buffer)));
- int oldTotalSprites = oldindexFile.readInt();
- int newTotalSprites = newindexFile.readInt();
- /**
- * First we check if the base amount of sprites are different, if they are we get the new cache
- */
- if (oldTotalSprites != newTotalSprites)
- return true;
- /**
- * Since they match we'll loop over them and check that the contents are the same size
- */
- for (int i =0 ;i < oldTotalSprites; i++) {
- int oldId = oldindexFile.readInt();//skip ids as we shouldn't need them
- int newId = newindexFile.readInt();
- if (oldId != newId)//if there ids arn't in the same order for whatever reason we'll just update
- return true;
- int oldFileSize = oldindexFile.readInt();
- int newFileSize = newindexFile.readInt();
- if (oldFileSize != newFileSize)//if the file sizes don't match we'll update
- return true;
- }
- return false;//update not needed
- } catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment