Guest User

Untitled

a guest
Jan 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. package org.hannes.archive;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.nio.ByteBuffer;
  6.  
  7. public class Archive {
  8.  
  9. /**
  10. * The file buffer
  11. */
  12. private final ByteBuffer buffer;
  13.  
  14. /**
  15. * The file stuff
  16. */
  17. private File[] files;
  18.  
  19. /**
  20. * Indicates the individual files are compressed
  21. */
  22. private final boolean compressed;
  23.  
  24. public Archive(ByteBuffer buffer, File[] files, boolean compressed) {
  25. this.buffer = buffer;
  26. this.files = files;
  27. this.compressed = compressed;
  28. }
  29.  
  30. /**
  31. * Creates an archive from a given bufferarray stuff thingy
  32. *
  33. * @param data
  34. * @return
  35. * @throws IOException
  36. */
  37. public static Archive create(byte[] data) throws IOException {
  38. return create(ByteBuffer.wrap(data));
  39. }
  40.  
  41. /**
  42. * Creates an archive from a given buffer
  43. *
  44. * @param buffer
  45. * @return
  46. * @throws IOException
  47. */
  48. public static Archive create(ByteBuffer buffer) throws IOException {
  49. int size_decompressed = Utils.get24BitInt(buffer);
  50. int size_compressed = Utils.get24BitInt(buffer);
  51.  
  52. if (size_compressed != size_decompressed) {
  53. byte[] data = new byte[size_compressed];
  54. buffer.get(data);
  55. buffer = ByteBuffer.wrap(Utils.decompress(data));
  56. }
  57.  
  58. int size = buffer.getShort() & 0xFFFF;
  59. int offset = buffer.position() + size * 10;
  60.  
  61. File[] files = new File[size];
  62. for (int i = 0; i < size; i++) {
  63. int identifier = buffer.getInt();
  64. int sizeDecompressed = Utils.get24BitInt(buffer);
  65. int sizeCompressed = Utils.get24BitInt(buffer);
  66. files[i] = new File(identifier, sizeDecompressed, sizeCompressed, offset);
  67. offset += sizeCompressed;
  68. }
  69. return new Archive(buffer, files, size_compressed != size_decompressed);
  70. }
  71.  
  72. public void write() {
  73. ByteBuffer header = ByteBuffer.allocate(files.length * 10);
  74. for (int i = 0; i < files.length; i++) {
  75. buffer.putInt(files[i].identifier);
  76. Utils.put24BitInt(files[i].sizeDecompressed, header);
  77. Utils.put24BitInt(files[i].sizeCompressed, header);
  78. }
  79.  
  80. }
  81.  
  82. public void remove() {
  83.  
  84. }
  85.  
  86. public ByteBuffer getFile(int identifier) throws IOException {
  87. for (File file : files) {
  88. if (file.identifier == identifier) {
  89. byte[] array = new byte[file.sizeCompressed];
  90. buffer.position(file.offset);
  91. buffer.get(array, 0, file.sizeCompressed);
  92. System.out.println(file.sizeCompressed + "," + file.sizeDecompressed);
  93. if (compressed) {
  94. return ByteBuffer.wrap(Utils.decompress(array));
  95. } else {
  96. return ByteBuffer.wrap(array);
  97. }
  98. }
  99. }
  100. throw new FileNotFoundException("identifier (" + identifier + ") not found");
  101. }
  102.  
  103. public int[] getIdentifiers() {
  104. int[] identifiers = new int[files.length];
  105. for (int i = 0; i < files.length; i++) {
  106. identifiers[i] = files[i].identifier;
  107. }
  108. return identifiers;
  109. }
  110.  
  111. @Override
  112. public String toString() {
  113. StringBuilder builder = new StringBuilder("archive with ");
  114. builder.append(files.length).append(" files; buffer capacity: ").append(buffer.capacity());
  115. builder.append("; compressed = ").append(compressed);
  116. return builder.toString();
  117. }
  118.  
  119. private static class File {
  120.  
  121. /**
  122. * The identifier of the file (filename)
  123. */
  124. private final int identifier;
  125.  
  126. /**
  127. * The size when decompressed
  128. */
  129. @SuppressWarnings("unused")
  130. private final int sizeDecompressed;
  131.  
  132. /**
  133. * The size of the file when it is compressed
  134. */
  135. private final int sizeCompressed;
  136.  
  137. /**
  138. * The offset of the file in the archive
  139. */
  140. private final int offset;
  141.  
  142. public File(int identifier, int sizeDecompressed, int sizeCompressed, int offset) {
  143. this.identifier = identifier;
  144. this.sizeDecompressed = sizeDecompressed;
  145. this.sizeCompressed = sizeCompressed;
  146. this.offset = offset;
  147. }
  148.  
  149. }
  150.  
  151. }
Add Comment
Please, Sign In to add comment