harlan

Untitled

Apr 14th, 2012
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. public boolean needsUpdates() {
  2. URLConnection conn;
  3. InputStream in = null;
  4. try {
  5. /**
  6. * Gets old file
  7. */
  8. byte[] file = null;
  9. try {
  10. file = FileOperations.readFile(signlink.findcachedir() + "sprites.idx");
  11. } catch (RuntimeException e) {
  12. e.printStackTrace();
  13. }
  14. if (file == null)
  15. return true;
  16. ByteBuffer oldIndex = new ByteBuffer(file);
  17. DataInputStream oldindexFile = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(oldIndex.buffer)));
  18. /**
  19. * Download the index file from website, we use this to compare our sprites and check if any are missing
  20. */
  21. ByteBuffer newIndex = new ByteBuffer();
  22. DataInputStream newindexFile = null;
  23. URL url = new URL("http://bsclient.com/update/sprite/sprites.idx");
  24. conn = url.openConnection();
  25. in = conn.getInputStream();
  26. byte[] data = new byte[500];
  27. int numRead;
  28. long numWritten = 0;
  29. int length = conn.getContentLength();
  30. while((numRead = in.read(data)) != -1) {
  31. newIndex.writeBytes(data, 0, numRead);
  32. numWritten += numRead;
  33. int percentage = (int)(((double)numWritten / (double)length) * 100D);
  34. client.drawSmothLoading(percentage, "Fetching Update File ["+(double)numWritten +"/"+ (double)length+"]");
  35. }
  36. newindexFile = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(oldIndex.buffer)));
  37.  
  38. int oldTotalSprites = oldindexFile.readInt();
  39. int newTotalSprites = newindexFile.readInt();
  40. /**
  41. * First we check if the base amount of sprites are different, if they are we get the new cache
  42. */
  43. if (oldTotalSprites != newTotalSprites)
  44. return true;
  45. /**
  46. * Since they match we'll loop over them and check that the contents are the same size
  47. */
  48. for (int i =0 ;i < oldTotalSprites; i++) {
  49. int oldId = oldindexFile.readInt();//skip ids as we shouldn't need them
  50. int newId = newindexFile.readInt();
  51. if (oldId != newId)//if there ids arn't in the same order for whatever reason we'll just update
  52. return true;
  53. int oldFileSize = oldindexFile.readInt();
  54. int newFileSize = newindexFile.readInt();
  55. if (oldFileSize != newFileSize)//if the file sizes don't match we'll update
  56. return true;
  57. }
  58. return false;//update not needed
  59.  
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. return false;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment